Question

I don't think it is possible, but hoping I am wrong. I'm using a CSOM in a C# application to update a SharePoint list (SharePoint Foundation server). All of the items on the list are updated correctly, except for the 'Author' column, which is person or group value. When I check the SharePoint list, it is always updated with the current user executing the application (me).

Is there a way to add other users to SharePoint list? I have the their IDs and all other needed data. I thought I could with EnsureUser, but seem to be wrong. Below is the sample code that I use.

Any help is appreciated, thank you.

ClientContext clientContext = new ClientContext(sharepointContext);
List sharepointList = clientContext.Web.Lists.GetByTitle(listName);


User newUser = clientContext.Web.EnsureUser("domain\\login"); //random user info
clientContext.Load(newUser);
clientContext.ExecuteQuery();

FieldUserValue userValue = new FieldUserValue();
userValue.LookupId = newUser.Id;

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = sharepointList.AddItem(itemCreateInfo);
oListItem["Author"] = userValue;
oListItem.Update();
clientContext.ExecuteQuery();
Was it helpful?

Solution

Slightly modifying your example worked for me. Please note that for "Author" and "Editor", I used "user" type.

public static void UpdateAuthor()
        {
            string sitrUrl = "http://MySPServer:9930/sites/mySiteName";
            using (var ctx = new ClientContext(sitrUrl))
            {
                List list = ctx.Web.Lists.GetByTitle("TestAuthor");

                User newUser = ctx.Web.EnsureUser(@"domain\UserName"); //random user info
                ctx.Load(newUser);
                ctx.ExecuteQuery();

                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem oListItem = list.AddItem(itemCreateInfo);
                oListItem["Title"] = "Hello Author";
                oListItem["Status"] = "Active";
                oListItem["Author"] = newUser;
                oListItem["Editor"] = newUser;
                oListItem.Update();
                ctx.ExecuteQuery();
            }
        }

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top