Pull data from a user’s My Site in SharePoint

In a SharePoint that I support, we have custom properties in the My Sites that represent key knowledge areas that have been identified as significant to the Command. The knowledge areas were identified using a taxonomy creating a hierarchy. For example, one main KA is simply Analysis. Analysis is further broken down into subcategories like Capability Analysis, Cost Analysis and so on. Users can then go into their My Site, edit details and assign values like Novice, Intermediate, Expert, etc to these individual areas. I needed a way to be able to pull that information from their profiles for reporting, etc. The following code gives an example of pulling the value from the custom property “Analysis”.

   1: // the following code pulls data from a user's profile (i.e. mySite)

   2: string analysis="";

   3: using (web.Site)

   4: {

   5:     using (web.Site.OpenWeb())

   6:     {

   7:         SPSecurity.RunWithElevatedPrivileges(delegate()

   8:         {

   9:             // Retrieve the ServerContext of the site collection.

  10:             ServerContext serverContext = ServerContext.GetContext(web.Site);

  11:  

  12:             // Connect to the UserProfileManager of the SharePoint Web application.

  13:             UserProfileManager upm = new UserProfileManager(serverContext);

  14:             UserProfile uPro = upm.GetUserProfile(userValue.User.LoginName);

  15:             analysis = uPro["Analysis"].ToString();

  16:         });

  17:     }

  18: }

Nice and simple. Ninja

Advertisements

About lelandholmquest

After serving in the Navy as a Reactor Operator on fast attack submarines, I earned both a Bachelor's and Masters in Information Technology from American InterContinental University and am currently working on my doctorate. I have a beautiful wife and two of the sweetest daughters a man could ask for. And I work for the greatest company: Microsoft. At Microsoft I work on the knowledge management system for Microsoft Services: Campus on Office 365.
This entry was posted in C# Programming, Knowledge Management, SharePoint Coding, SharePoint Content Management Tips, Web Parts, Workflows and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s