Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can "Modified By" be changed when using RunWithElevatedPrivileges?
    text
    copied!<p>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 <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx" rel="noreferrer">RunWithElevatedPrivileges</a> block. This means the "Modified By" field is always set to System Account. Here is the code:</p> <pre><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(); } } } </code></pre> <p>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:</p> <pre><code>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); </code></pre> <p>Does anyone know of a solution that allows the "Modified By" value to be changed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload