About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Wednesday, 11 December 2013

Programatically update MODIFIED BY feild in SharePoint 2007/2010/2013 list

Brief Issue:

When we are trying to insert/update an item by using SPSecurity.RunWithElevatedPrivileges(delegate(){});” for getting the Administrator privilege we see that the value of "Modified By" feild of that inserted /updated item is SYSTEM ACCOUNT instead of the orginal user who has actually done the modification .
In this case, we need to update the "Modified By" field in the time of inserting/updating. It may not work if we assign a value for the "Modified By" field and call the Update method.
Use "UpdateOverwriteVersion()" method. This method allows for the setting of properties on a SPListItem object without creating a separate version of the item. This method also allows for setting of certain system properties such as Created(date), Modified(date), Author(Created By) and Editor(Modifies By).
Code snippet:
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))

   using (SPWeb oWeb = oSite.OpenWeb())
      //Variable to store the user
      SPUser oUser = oWeb.CurrentUser;
      oWeb.AllowUnsafeUpdates = true;

      SPFieldUserValue userVal = new SPFieldUserValue(oWeb, oUser.ID, oUser.LoginName);

      SPList oList = oWeb.Lists["ListName"];
      SPListItem oItem = oList.GetItemById(Item Id);

      oItem["Modified By"] = userVal;


      oWeb.AllowUnsafeUpdates = false;

No comments:

Post a Comment