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;
}
If the AssignedTo field is a Person or Group field, it holds the SharePoint ID of the user or group. For example:
In this case, the SharePoint ID of the user is 8. To get the users name, you'll have to look at the
UserInformationListlocated 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)/AssignedToWhere 1 in this example, is the ID of the task. Your approach depends on your needs.
More information:
http://yetanothersharepointblog.wordpress.com/2012/12/11/working-with-sharepoint-lookup-columns-in-knockout-js/
http://social.technet.microsoft.com/Forums/office/en-US/8e6badbf-a270-4b8e-9a62-c9f7be44ada2/rest-api-how-to-get-author-name?forum=sharepointdevelopmentprevious