SharePoint Online

The cloud parts are functional components that extend your SharePoint Online environment in Microsoft 365.

Supports Modern sites for SharePoint Online/Microsoft 365

View All Products

Small Business Pricing and Discounts

Learn More

Subscription Pricing

Web Part Subscription
Alert Plus Enterprise Subscription

SharePoint

Top SharePoint Online Products

Experience greater power and savings by bundling our SharePoint apps and cloud parts.


Alert Plus


Calendar Plus


Knowledge Base


Employee Directory Plus


Org Chart Plus


Simple Search


Tabify


Tree View

 

View More

On-Premises Only

These web parts extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of SharePoint Web Parts.

SharePoint 2016, 2019, SPSE - Classic Pages Only

View All Products
SharePoint

Top On-Premises Only Products

Experience greater power and savings by bundling our SharePoint apps and web parts.


Calendar Plus


Data Viewer


Password Change


Password Expiration


Password Reset

 

View More

Our team of Microsoft 365 Technology Consultants helps you get the most out of your Microsoft technology, we have the best Microsoft 365 talent to streamline your organization.

M365 On-Call

On-Demand Microsoft 365 & Power Automate Expertise

Learn More

M365 Plus

Managed Services

Learn More
Microsoft 365

Accelerated Scheduling


Scheduling for Aviation

Scheduling for Home Health


Scheduling for Deliveries


Scheduling for Emergency Personnel

View More

Our Consultants Have What You Need

View All Services

Federal Contractors

GCC Migration Support
Microsoft 365

Our Consultants Have What You Need


Microsoft 365 Enablement


M365 Portals / Intranets


Microsoft 365 Security Configuration


Power Automate


Power Apps


Modern Forms

 

View More
Bamboo Solutions
  • Products
  • Services
    • Migration to Microsoft 365
    • GCC Government Community Cloud
    • Migration Center of Excellence
  • About
    • Blog
    • Webinars & Events
    • Resources
    • Careers
    • Bamboo Partner Network Program
  • Support
    • M365 Art of the Possible
    • Request Support
    • Enterprise Product Support Plan
    • M365 Plus Program
    • SharePoint Online Product Documentation
    • SharePoint On Premises Product Documentation
  • My Account
  • My Cart
  • Contact Us
Select Page

SharePoint 2010 Cookbook: Programmer’s Guide to User Profiles, Part 2 – How to Get the UserProfile Object

by Quang San | Nov 9, 2010 | SharePoint 2010

This is the second of a four-part series of blog entries in which I will describe how to read and modify SharePoint UserProfile using the SharePoint Object Model.

The series will consist of the following:

  • Part 1: How to Check Admin Rights and Get All UserProfile Properties
  • Part 2: How to Get the UserProfile Object
  • Part 3: How to Update UserProfile Properties
  • Part 4: How to Push Changes in Active Directory to the SharePoint User Profile

Challenge:

Both Microsoft Office SharePoint Server 2007 and Microsoft SharePoint Server 2010 offer a rich object model for programmatically manipulating user profiles, but what are the differences between the two versions, and how does one accomplish user profile manipulation in 2010?

Solution:

What's the relevant differences between SharePoint 2007 and SharePoint 2010?

In MOSS 2007, you use the Microsoft.Office.Server.UserProfiles namespace in Microsoft.Office.Server.dll to work with a user's profile. In SharePoint Server 2010, however, you should use the Microsoft.Office.Server.UserProfiles namespace in Microsoft.Office.Server.UserProfiles.dll (the same namespace but with a different assembly). The Microsoft.Office.Server.UserProfiles namespace contains two new types, and classes for creating and managing UserProfile. Note: The OrganizationProfile class is equivalent to UserProfile. This class enables you to group and distinguish users by membership in organizations that have properties equivalent to those that a UserProfile object would have. You manage this class by using the OrganizationProfileManager class. Another new class, ProfileSubtype, enables you to create role-specific properties for any type of profile. For example, this class can distinguish users who are customers from those who are employees, or between employees who perform different tasks in an organization. Use the ProfileSubtypeManager class to manage ProfileSubtypes, and use the ProfileSubtypePropertyManager class to manage properties for ProfileSubtypes. Although the Property class is still available for backward compatibility, you can now set profile properties by using additional classes, such as CoreProperty, ProfileTypeProperty, and ProfileSubtypeProperty.

How to retrieve UserProfiles

First, make sure that you have the admin right to retrieve UserProfiles, please refer to part one of this series, How to Check Admin Rights and Get All UserProfile Properties, for more details.

1. Getting the UserProfile Object in Office SharePoint Server 2007

You should add the following references to your Microsoft Visual Studio project before running the code example:

  • Microsoft.Office.Server
  • Microsoft.SharePoint
  • System.Web

public UserProfile GetUserByAccountName(string strAccountName)
{
    UserProfile upUser = null;
    try
    {
        ServerContext context = ServerContext.Current;
        UserProfileManager upm = new UserProfileManager(context);
        upUser = upm.GetUserProfile(strAccountName); ;
    }
    catch (Exception ex)
    {
        //throw something
    }
    return upUser;
}

2. Getting the UserProfile Object in Sharepoint Server 2010

Here is the new way to write code by using the User Profiles object model in SharePoint Server 2010. You should add the following references to your Microsoft Visual Studio project before running the code example:

  • Microsoft.Office.Server
  • Microsoft.Office.Server.UseProfiles
  • Microsoft.SharePoint
  • System.Web

 

public Microsoft.Office.Server.UserProfiles.UserProfile GetUserByAccountName(string strAccountName)
{
    Microsoft.Office.Server.UserProfiles.UserProfile upUser = null;
    try
    {
        System.Security.PermissionSet ps = 
            new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
        ps.Assert();
 
        Microsoft.SharePoint.SPSite siteColl = Microsoft.SharePoint.SPContext.Current.Site;
        Microsoft.SharePoint.SPServiceContext serviceContext = 
            Microsoft.SharePoint.SPServiceContext.GetContext(siteColl);
        Microsoft.Office.Server.UserProfiles.UserProfileManager upm = 
            new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
 
        upUser = upm.GetUserProfile(strAccountName);
        return upUser;
    }
    catch (Exception ex)
    {
        this.WriteErrorLog("GetUserByAccountName: " + ex.ToString());
    }
    finally
    {
        System.Security.CodeAccessPermission.RevertAssert();
    }
    return upUser;
}

3. How to retrieve a user profile property value in Office SharePoint Server 2007 and SharePoint Server 2010

The common way to retrieve a user profile property value is via the following approach:

string propertyValue = userInfo[strPropName].Value.ToString();


Console.WriteLine(propertyValue);


 

But in some cases, such as the multiple value property, we can't get the correct value using that approach.  In such cases, the way to retrieve the correct value is:

private System.Collections.ArrayList GetArrValueOfUserField(
    Microsoft.Office.Server.UserProfiles.UserProfile userInfo, 
    Microsoft.Office.Server.UserProfiles.Property proper)
{
    System.Collections.ArrayList arrValue = new System.Collections.ArrayList();
    try
    {
        System.Security.PermissionSet ps = 
            new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
        ps.Assert();
        
        bool isMultivalue = proper.IsMultivalued;
        string separatorName = proper.Separator.ToString();
 
        object a = userInfo[proper.Name].Value;
        if (isMultivalue && (a != null))
        {
            Microsoft.Office.Server.UserProfiles.UserProfileValueCollection valueOfField = userInfo[proper.Name];
            char separatorSymBol = GetSeparatorOfProperty(separatorName);
 
            for (int i = 0; i < valueOfField.Count; i++)
            {
                object ieValue = valueOfField[ i ];
                arrValue.Add(ieValue.ToString());
            }
        }
        else
        {
            arrValue.Add(a.ToString());
        }
    }
    catch (Exception ex)
    {
        this.WriteErrorLog("GetArrValueOfUserField: " + ex.ToString());
    }
    finally
    {
        System.Security.CodeAccessPermission.RevertAssert();
    }
    return arrValue;
}

We can also get the direct reports using the userInfo.GetDirectReports() method, and get manager by using the userInfo.getManager() method.

See Also

  • Microsoft Office SharePoint Server 2007 – User Profiles Object Model Overview [MSDN]
  • What's New in SharePoint Server 2010: User Profiles and Social Data object model [MSDN]

Blog Categories

  • AI Agents (3)
  • Alert Plus (8)
  • Cyber Security (7)
  • data migration services (11)
  • Emoji (2)
  • Employee Spotlight (33)
  • Events (158)
  • In the News (64)
  • Microsoft (61)
  • Microsoft AppSource Product Update (3)
  • Microsoft SharePoint Bloggers (27)
  • Midshare (1)
  • Office 365 (60)
  • Pentalogic (1)
  • PowerApps (12)
  • Press Release (73)
  • Product Updates (141)
  • Products (334)
  • SharePoint 2010 (173)
  • SharePoint 2013 (257)
  • SharePoint Blank (47)
  • SharePoint Experts (207)
  • SharePoint for Administrators (32)
  • SharePoint for Project Management (20)
  • SharePoint Solutions (255)
  • SharePoint Term Store (3)
  • SharePointXperts (195)
  • SPFx (3)
  • The Bamboo Team Blog (1,393)
  • The Bottom-Line PM: A Realists's Guide to SharePoint and Microsoft Project (25)
  • The Business of SharePoint (25)
  • Year-End Review (4)
Bamboo Solutions and its related brands is the copyright of SharePointXperts, LLC. ©2002–2026 | Privacy Policy | Terms of Use | Sitemap
Bamboo Solutions Knowledge Base product overview

Explore Knowledge Base ↗

Bamboo Solutions Alert Plus product overviewExplore Alert Plus ↗
Bamboo Solutions Simple Search product overviewExplore Simple Search ↗
Bamboo Solutions Tabify product overviewExplore Tabify ↗
Bamboo Solutions Tree View product overviewExplore Tree View ↗
Bamboo Solutions Data Viewer product overviewExplore Data Viewer ↗