Sometimes
we need to insert/update item using
“SPSecurity.RunWithElevatedPrivileges(delegate(){});” for getting the Administrator
privilege..
But as a result of that we see the “System Account” as a value of "Modified By" field of that inserted/updated item, instead of the original user who actually did 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. So we have to use the "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). |
These
are the following code by which we can easily update Modified By field:-
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; OR oItem[SPBuiltInFieldId.Editor] = userVal; OR oItem[“Editor”] = userVal; oItem.UpdateOverwriteVersion(); oWeb.AllowUnsafeUpdates = false; } } |
Saturday, January 12, 2013
How to update Modified By field in a list ?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment