We have a web part that uploads a document to a document library. The user uploading the document may not have access to the destination location, so the code adding the file executes within a RunWithElevatedPrivileges block. This means the "Modified By" field is always set to System Account. Here is the code:
SPSecurity.RunWithElevatedPrivileges(
delegate
{
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.Url))
using (SPWeb targetWeb = elevatedSite.OpenWeb(webUrl))
{
targetWeb.AllowUnsafeUpdates = true;
SPFile newFile = files.Add(filename, file);
SPListItem item = newFile.Item;
// TODO: Insert code to set Modified By
item.SystemUpdate();
}
}
}
The "Modified By" field needs to be set to the name of the current user (at the TODO line above), but none of the following attempts have worked:
item["Modified By"] = SPContext.Current.Web.CurrentUser;
item["Author"] = SPContext.Current.Web.CurrentUser;
item["Modified By"] = new SPFieldUserValue(
SPContext.Current.Web, SPContext.Current.Web.CurrentUser.ID,
SPContext.Current.Web.CurrentUser.Name);
item["Author"] = new SPFieldUserValue(
SPContext.Current.Web, SPContext.Current.Web.CurrentUser.ID,
SPContext.Current.Web.CurrentUser.Name);
Does anyone know of a solution that allows the "Modified By" value to be changed?