Blogger

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items

Login

Tuesday, 12 February 2013 21:00

Retrieve User profile properties via SharePoint Client Object Model

Written by
Rate this item
(0 votes)

In this example, I wanto to explain how to use the Client Object Model to retrieve User Profile Properties.

In my example, I have a list with some fields and one of this is a SPFieldUserValue (UserField is the internal name of my field).

Show/Hidden csharp code

View source
 
 
 
ClientContext clientContext = new ClientContext("htttp://intranet.domain022.dev");
clientContext.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
 
 
Site siteCol = clientContext.Site;
Microsoft.SharePoint.Client.Web web = clientContext.Web;
clientContext.Load(web, w => w.Title, w => w.Language, w => w.ServerRelativeUrl);
clientContext.ExecuteQuery();
 
 
clientContext.Load(web, c => c.Lists);
clientContext.ExecuteQuery();
 
List list = web.Lists.GetByTitle("CustomList");
clientContext.Load(list);
clientContext.ExecuteQuery();
 
 
var userInfoList = clientContext.Web.SiteUserInfoList;
clientContext.Load(userInfoList);
 
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View/>";
Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(camlQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();
 
foreach (Microsoft.SharePoint.Client.ListItem item in items)
{
    clientContext.Load(item);
    clientContext.ExecuteQuery();
    
    var temp = item["UserField"];
    
    var query = new CamlQuery { ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='ID' /><Value Type='int'>" + ((FieldUserValue)temp).LookupId + "</Value></Eq></Where></Query></View>" };
    var user = userInfoList.GetItems(query);
    clientContext.Load(user);
    clientContext.ExecuteQuery();
    if (user.Count > 0)
    {
 
 
        //here I retrieve only Name property. You can iterate on all properties to view all User properties
        string name = user[0].FieldValues["Name"] as string;
        Console.WriteLine(string.Format("User found {0} with id {1}", name.Split('|').LastOrDefault(), user[0].Id));
    }
    else
    {
        Console.WriteLine(string.Format("User not found: {0} with id {1}", ((FieldUserValue)temp).LookupValue;, ((FieldUserValue)temp).LookupId.ToString()));
    }
}
 
 
 

Read 12019 times Last modified on Tuesday, 12 January 2016 10:41

1 comment

  • Comment Link Annie Saturday, 24 September 2016 03:10 posted by Annie

    Thanks for sharing your info. I really appreciate your efforts and I am waiting for
    your further post thanks once again.

    Report

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.