Sometimes is useful to overwrite the author and/or the editor fields, specially if you are using SPSecurity.RunWithElevatedPrivileges and you don’t want the item to be saved as “System Account”.

For SPListItem

//Add a new item
SPListItem objSPListItem = objSPList.Items.Add();

objSPListItem["Editor"] = SPContext.Current.Web.CurrentUser;
objSPListItem["Author"] = SPContext.Current.Web.CurrentUser;
//Update item values
objSPListItem.Update();

For SPFile

// Upload document
SPFile spfile = myLibrary.Files.Add(FileUpload1.PostedFile.FileName, FileUpload1.PostedFile.InputStream, replaceExistingFiles);

// Commit
myLibrary.Update();

spfile.Item["Editor"] = SPContext.Current.Web.CurrentUser;
spfile.Item["Author"] = SPContext.Current.Web.CurrentUser;

//Update item values
spfile.Item.Update();

Leave a comment