0

I am trying to submit some data from a winform into a SharePoint list. Most of the items can submit fine except for the a field which is a user lookup field. I have done this in the past with a powershell script from the server side, but I am unable to get it done from the client side.

I get the idea that I am to retrieve the userid but the examples out there are a bit confusing maybe somebody can help me with this piece of code:

ClientContext context = new ClientContext(SharePointAddress);
List PANlist = context.Web.Lists.GetByTitle(SharePointList);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = PANlist.AddItem(itemCreateInfo);

newItem[@"First_x0020_Name"] = dr.Cells["First Name"].Value.ToString();
newItem[@"Middle_x0020_Name"] = dr.Cells["Middle Name"].Value.ToString();
newItem[@"Last_x0020_Name"] = dr.Cells["Last Name"].Value.ToString();

newItem.Update();
context.ExecuteQuery();

If I try (where Existing User is a lookup people only field and string is "John Doe"):

newItem[@"Existing_x0020_User"] = dr.Cells["Existing User"].Value.ToString();

it fails with the error:

"Invalid data has been used to update the list item. The field you are trying to update may be read only"

Can you provide an example or help on how do I submit "John Doe" on the lookup field?

Thanks

3

With User/Group field you need to pass 'ID' in place of text to update the list item.

Microsoft.SharePoint.Client.User _newUser = _ctx.Web.EnsureUser("domain\\Existing User");    
_ctx.Load(_newUser);
_ctx.ExecuteQuery();

 Microsoft.SharePoint.Client.FieldUserValue _userValue = new Microsoft.SharePoint.Client.FieldUserValue();
_userValue.LookupId = _newUser.Id;
_listItem["userfield"] = userValue;
_listItem.Update();
_ctx.ExecuteQuery();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.