How do you get the UserName from UserInformationListItem in SharePoint Service Reference

2k Views Asked by At

I am accessing SharePoint List data via WCF and listdata.svc

One of my lists called Tasks has a field named 'AssignedTo'. When I loop through the list items, the AssignedTo field returns a UserInformationListItem rather than a string value.

How do I get the username of the person to which the task is assigned? It should come from the UserInformationList, but I can't figure out how to get it.

Here is my code:

        SpIMDLists.InformationManagementDivisionDataContext dc = new SpIMDLists.InformationManagementDivisionDataContext(new Uri("https://myurl/SiteDirectory/IMD/_vti_bin/ListData.svc/"));

        dc.Credentials = System.Net.CredentialCache.DefaultCredentials;

        var source = dc.Tasks;


        foreach (var task in source)
        {
            string taskTitle = task.Title;
            string taskDesc = task.TaskDescription;
            string taskDueDate = task.DueDate.ToString();
            string taskStartDate = task.StartDate.ToString();
            string taskStatusValue = task.StatusValue;
            string taskOutcome = task.TaskOutcome;
            string taskAssignedTo ="";
            System.Collections.ObjectModel.Collection<SpIMDLists.UserInformationListItem> assignedTo = task.AssignedTo;


         }
2

There are 2 best solutions below

1
Justin Russell On

If the AssignedTo field is a Person or Group field, it holds the SharePoint ID of the user or group. For example:

<d:AssignedToId m:type="Edm.Int32">8</d:AssignedToId>

In this case, the SharePoint ID of the user is 8. To get the users name, you'll have to look at the UserInformationList located at /_vti_bin/ListData.svc/UserInformationList. You can either get all the users in the UserInformationList and store it in an array, or you can lookup a specific user by creating the URL as follows (using the User ID of 8 from our example): /_vti_bin/ListData.svc/UserInformationList(8)

If you want to see this user in your browser, you can do so at the following URL: /_layouts/userdisp.aspx?ID=8.

Alternatively, you can use the following endpoint to get the same information: _vti_bin/ListData.svc/Tasks(1)/AssignedTo

Where 1 in this example, is the ID of the task. Your approach depends on your needs.

More information:

0
Hannah Castellaw On

UPDATE: Appreciate the input. It led me to the following solution. This is how I was able to get the user's name. AssignedTo is a complex field, so I had to use Expand in order to populate it. Then I just looped through the AssignedTo collection to get the user (in this case the tasks are assigned to 1 user only).

Here is my new working code:

        SpIMDLists.InformationManagementDivisionDataContext dc = new SpIMDLists.InformationManagementDivisionDataContext(new Uri("https://myurl/SiteDirectory/IMD/_vti_bin/ListData.svc/"));

        dc.Credentials = System.Net.CredentialCache.DefaultCredentials;

        var source = dc.Tasks;


        foreach (var task in source.Expand("AssignedTo")
        {
            string taskTitle = task.Title;
            string taskDesc = task.TaskDescription;
            string taskDueDate = task.DueDate.ToString();
            string taskStartDate = task.StartDate.ToString();
            string taskStatusValue = task.StatusValue;
            string taskOutcome = task.TaskOutcome;

            var assignedTo = task.AssignedTo;
            foreach (var usr in assignedTo)
            {
                string taskAssignedTo = usr.Name;
            }


         }