Tuesday, February 26, 2013

Sharepoint 2013 get user property values

Hi guys in this post im going to show how we can get the user profile property values programmatically. So here for example I have shown how to get the 'Ask Me About' tag values.

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
          {
SPUserCollection userCollection = web.AllUsers;
SPSite mySite = new SPSite("http://MySite-Url");

//Define Server Context
SPServiceContext serverContext = SPServiceContext.GetContext(mySite);

//Create an instance of the User Profile Manager
UserProfileManager profileManager = new UserProfileManager(serverContext);

foreach (SPUser spUser in userCollection)
{
String loginName = spUser.LoginName.ToString();
//Get user profile
UserProfile user = profileManager.GetUserProfile(loginName);
String userProperty = "SPS-Responsibility"; //Ask me about user property

UserProfileValueCollection coll = user[userProperty];

foreach (String val in coll)
{
String myTag = val;
}
}
          }
}

So using this code you can loop through users list and check eachone's Ask Me About property value. When initializing userProperty variable give the interal name of it. You can check the internal name by going to Central Admin > Manage service applications > User Profile Service Application > Manage User Properties and select a user property (Ask Me About) and click edit. In the Name field you can check the interal name. Check below..



Comment and give your feedbacks
Cheers.

Luckshan Roy.

2 comments:

  1. cool..ur blog was one of the top results for something I've searched..nice work Roy..keep it up.

    ReplyDelete
    Replies
    1. Thanks Buddhi!! I hope this blog helped you :)

      Delete