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.
Resolution:
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;
oItem.UpdateOverwriteVersion();
oWeb.AllowUnsafeUpdates = false;
}
}
{
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;
oItem.UpdateOverwriteVersion();
oWeb.AllowUnsafeUpdates = false;
}
}
No comments:
Post a Comment