IntraNoggin

Blither, Blather, and SharePoint

Main menu

Skip to primary content
Skip to secondary content
  • Home
  • About

Tag Archives: User Profile

Retrieve a User’s Profile Property Value

Posted on March 11, 2009 by Ryan Miller
Reply

I’ve been using this code for a while now, copying it from project to project as I needed it. I used it again today and thought I’d post it for anyone else out there that may be doing this for the first time.

 

Hand this method in a user’s account and the name of the profile property that you want to retrieve. Make sure you give it the actual property name, and not just the display name.

It will give you back the value of the property. If the property allows multiple values, this will hand them all back in a comma delimited string.

using Microsoft.Office.Server.UserProfiles;

private string GetProfileValueBrowser(string userID, string profileProperty)

{

string profileValue = string.Empty;

if (!string.IsNullOrEmpty(userID))

{

UserProfileManager profileManager = new UserProfileManager();

UserProfile userProfile = profileManager.GetUserProfile(userID);

UserProfileValueCollection profileValues = userProfile[profileProperty];

if (profileValues.Value != null)

{

string[] valueStrings = new string[profileValues.Count];

for (int i = 0; i < profileValues.Count; i++)

{

valueStrings[i] = Convert.ToString(profileValues[i]);

}

profileValue = string.Join(", ", valueStrings);

}

}

return profileValue;

}

Now, today I needed to do the same thing, but within a workflow’s code. The catch here is that you have no context and no user privileges. The updated method looks like this:

using Microsoft.Office.Server.UserProfiles;

using Microsoft.Office.Server;

private string GetProfileValueWorkflow(string userID, string profileProperty)

{

string profileValue = string.Empty;

if (!string.IsNullOrEmpty(userID))

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

UserProfileManager profileManager = new UserProfileManager(ServerContext.Default);

UserProfile userProfile = profileManager.GetUserProfile(userID);

UserProfileValueCollection profileValues = userProfile[profileProperty];

if (profileValues.Value != null)

{

string[] valueStrings = new string[profileValues.Count];

for (int i = 0; i < profileValues.Count; i++)

{

valueStrings[i] = Convert.ToString(profileValues[i]);

}

profileValue = string.Join(", ", valueStrings);

}

});

}

return profileValue;

}

Hope it helps.

-Ryan

Posted in SharePoint | Tagged Code Sample, SharePoint, User Profile | Leave a reply

Archive

Categories

  • Blather (26)
  • Environment (5)
  • Open Source (22)
  • PowerShell (1)
  • SharePoint (100)
  • SharePoint Designer (8)
  • Windows 8 (1)
  • Workflow (11)

@Intranoggin Tweets

  • It's File Cleanup Day!!! 2012/05/11
  • RT @Modernizr: Wolfenstein 3D, recreated in HTML5: http://t.co/FAei7yhN Built with Modernizr: http://t.co/urr16a0x 2012/05/10

Tags

Audio Books Breadcrumbs Bridge Bug Code Sample Content Query CQWP Database Data View DVWP Engineering Environment Food Master Page Nintex Office 365 Open Source OSU PowerShell Products Pub Crawl Questions Quota Recycling Research Sandbox Sandboxed Solutions Security Provider SharePoint SharePoint Designer SharePoint Saturday Speaking SPMail stsadm Tip ToDo Usage Reports User Profile WCF Service web.config White Paper Workflow WSS Activities XBox

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
IntraNoggin