Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying and failing to set the value of a User column via the SharePoint client model:

int userID;
string displayName;

/*
   Look up userID and displayName from Web Service here
*/

Microsoft.SharePoint.Client.File newDocument = documentFiles.Add(creationInformation);
ListItem newItem = newDocument.ListItemAllFields;

item["Selected User"] = String.Format("{0};#{1}", userID, displayName);
item.Update();

Here, we are dealing with a ListItem user column. I was assuming that this behaves in the same way as SPListItem user column: Set with "user ID;#login name". This appears to be correct, although the official documentation doesn't mention this. ("User: When its value is undefined it MUST be CSOM Nothing, otherwise it must be CSOM Object. A lookup to a particular user in the User Info list").

The problem, though, is getting the User ID from the web service. The People service and the Users and Groups service both have fields named ID available, but these IDs don't correspond to the ID you get when looking up the corresponding SPUser object on the server side! In the example above, my user would be "2;#Geir Smestad" on the server side, but "31;#Geir Smestad" on the client side.

How do I find the user ID required to tag a list item?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

It turns out that you can get the required ID through ClientContext. Using web services for fetching the user ID is not necessary:

using (ClientContext client = new ClientContext("http://siteUrl"))
{
    client.Credentials = System.Net.CredentialCache.DefaultCredentials;
    Web thisWeb = client.Web;

    List userList = thisWeb.SiteUserInfoList;

    CamlQuery query = CamlQuery.CreateAllItemsQuery();

    ListItemCollection itemCollection = userList.GetItems(query);
    itemCollection.RetrieveItems().Retrieve();
    client.ExecuteQuery();

    foreach (ListItem i in itemCollection)
    {
        var name = i.FieldValues["Name"];
        var userName = i.FieldValues["UserName"];
        var userId = i.FieldValues["ID"];

        System.Diagnostics.Debug.Print(String.Format("{0} {1} {2}",name,userName,userId));
    }
}

The ID field contains the correct ID for the tagging the user.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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