Few days ago, I was working on a List where the necessity of creating dynamic column raised. Also, the need to initiate the column name inside an event receiver and setting the value of the newly column for the current item were needed. So, I created a custom (demo) List which is named as “DemoProjects”.
I created the following columns:
Title, Description, Comments, RadComments (for checking the event)
I needed the column to be created for an existing List item. So, I created a new item like the following keeping “Comments” and “RadComments” blank.
Now, in order to create the event receiver I created a new project of “Event Receiver” type in Visual Studio 2010.
I set the “ItemUpdated” method to True as I need to create the column while updating the list item.
After that, Visual Studio automatically creates the method name and parameters for ItemUpdated method
public override void ItemUpdated(SPItemEventProperties properties) { base.ItemUpdated(properties); }
Now, this base.ItemUpdated runs will run for each custom list. In order to attain my objective, I have modified this method to the following: ( I have tried to clarify all the steps by commenting each line of code )
- public override void ItemUpdated(SPItemEventProperties properties)
- {
- if (properties.ListTitle == “DemoProjects”)
- {
- string colComments = “”; // Column Display name
- string colCommentsStr = “”; // Internal name of the Column name
- string curUser = “”; // Current user name
- using (SPSite site1 = new SPSite(properties.SiteId)) //Accessing the current site with current user permission
- {
- using (SPWeb web1 = site1.OpenWeb(properties.RelativeWebUrl))
- {
- curUser = web1.CurrentUser.Name.ToString(); // Getting the current user display name
- }
- }
- SPSecurity.RunWithElevatedPrivileges(delegate() // To Elevate the permisson of the current user which runs as a system acc to create the list column
- {
- using (SPSite site = new SPSite(properties.SiteId))
- {
- using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
- {
- EventFiringEnabled = false; // Disabling the events firing to prevent unnecessary actions
- web.AllowUnsafeUpdates = true; // To execute actions which are not permitted by default
- SPList radList = web.Lists[properties.ListId]; // Getting the current List
- SPListItem project = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID); // Getting the Current List Item
- colComments = “Comments: “ + curUser; // Setting the column name with current user name
- if (!radList.Fields.ContainsField(colComments))
- {
- radList.Fields.Add(colComments, SPFieldType.Note, false); // Adding new column in current List
- radList.Update(); // Updating the current List after adding new column
- Guid guidViewID = radList.Views[“All Items”].ID; // Getting the Guid for “All items” view of current list
- SPView vw = radList.GetView(guidViewID);
- vw.ViewFields.Add(colComments); // Adding the new column in All items view
- vw.Update(); // Updating the All items view
- project.Update(); // Updating the current List item
- base.ItemUpdated(properties); // Attaching current event to current list item
- radList.Update(); // Updating the current list
- }
- colCommentsStr = radList.Fields[colComments].InternalName.ToString(); // Getting the internal name of newly created column
- }
- }
- });
- using (SPSite site2 = new SPSite(properties.SiteId)) // Again accessing the site with current user permission
- {
- using (SPWeb web2 = site2.OpenWeb(properties.RelativeWebUrl))
- {
- SPListItem project1 = web2.Lists[properties.ListId].GetItemById(properties.ListItem.ID); // Getting current List item
- project1[colCommentsStr] = project1[“Comments”].ToString(); // Setting current item’s new column’s value to Comments column
- project1[“RadComments”] = “Done!”; // Settting RadComments column’s value to “Done”
- project1.Update(); // Updating the current list item
- web2.AllowUnsafeUpdates = false; // After completing the action setting unsafe updates to false
- }
- }
- }
- EventFiringEnabled = true; // Enabling event firing for user interaction
- base.ItemUpdated(properties); // Attaching current event to current list item
- }
Now, after deploying this Event Receiver, I edited the previously created new item.
Notice that, I kept “RadComment” blank and there are currently no other columns in this list except these four. So, what happened when I clicked “Save”.
See the following window:
So, my dynamic column got created now (Comments: Current User name) and the value is also set by the event receiver which made it possible to know who commented the item. Now, if another user edits this item, another “Comment: Username” will be automatically be created and thus I can track all the comments made by the item editors and find out what he commented for that item. That’s pretty cool, isn’t it? Enjoy!!