How to Change Author and Editor Field in Office 365/SharePoint online using CSOM ?

In this blog, I will talk about the changing the author and editor field in a SharePoint list or a library in office 365 environment using Client Side object model code. Here is the sample code given to achieve the functionality.

static void Main(string[] args)
{
var siteURL = “https://mytenant.sharepoint.com/custom”;

var createdBy = “Sethu;

var modifiedBy = “God”;

ClientContext context = new ClientContext(siteURL);

var login = “loginname”;

var password = “your password”;

var securePassword = new SecureString();

foreach (char c in password)
{

securePassword.AppendChar(c);

}

SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(login, securePassword);

context.Credentials = credentials;

List list = context.Web.Lists.GetByTitle(“myTestDocument”);

ListItem item = list.GetItemById(listId);

FieldUserValue author = GetUsersDetails(context, createdBy);

FieldUserValue editor = GetUsersDetails(context, modifiedBy);

item[“Author”] = author;

item[“Editor”] = editor;

item.Update();

context.ExecuteQuery();

}

public static FieldUserValue GetUsersDetails(ClientContext cc, string uName)
{

FieldUserValue userValue = new FieldUserValue();

User newUser = cc.Web.EnsureUser(uName);

cc.Load(newUser);

cc.ExecuteQuery();

userValue.LookupId = newUser.Id;

return userValue;

}

Please ensure that Author and editor are already part of your Office 365 tenant environment. Happy SharePointing Folks 🙂

Leave a Reply

Your email address will not be published.