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 1 – How to Check Admin Rights & Get All UserProfile Properties

by Quang San | Nov 1, 2010 | SharePoint 2010

This is the first 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

Before we begin, I want to say that for these articles, the knowledge about the User Profile in 2010 has been collected from various blogs, forums, and Microsoft’s online documentation.  I have pulled them together, provided my own procedures, and have built a working program based on my sample Web Part.

Challenge:

In many situations, you want to check the permissions of a logged-in user. How can you check permissions on SharePoint Server 2007 and SharePoint Server 2010? Specifically, how do you get all properties of the UserProfile object?

Solution:

Let’s get started. First, you will need to assign an Administrator for the User Profile Service Application:

  1. Go to your Central Administration site.
  2. Choose “Manage service applications” in the “Application Management” section.
  3. In the Manage service applications page, make sure the status of User Profile Service Application is “Started”. Click User Profile Service Application to enable the “Administrator” Ribbon:

Add your Administrator and set “Manager Profiles” permissions for that account.

You should add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server
  • Microsoft.Office.Server.UserProfiles
  • Microsoft.SharePoint
  • Microsoft.SharePoint.Portal
  • Microsoft.SharePoint.Security

In SharePoint 2007, you can use a feature called SiteAccessChecker to check the portal rights:

public bool CheckUserPortalRight()
{
    //Can't pass Portal enum in since calling function will throw exception if it is run on WSS
    PortalRight portalRight = PortalRight.ManagePeople;
    try
    {
        SiteAccessChecker sac = new SiteAccessChecker(this.portalContext, portalRight);
        sac.Demand();
        return true;
    }
    catch (Microsoft.SharePoint.Portal.Security.AccessDeniedException)
    {
        return false;
    }
}

But in SharePoint 2010, that feature is not available. So, how can we check the portal rights of the logged-in user? I’m using the following method:

public bool CheckUserPortalRight()
{
    bool flag = true;
    try
    {
        UserProfile userInfo = null;
        userInfo = GetUserByAccountName("the account name of login user");
        object obj = userInfo["FirstName"].Value;
        userInfo["FirstName"].Value = obj;
    }
    catch (Exception ex)
    {
        string targetSite = ex.TargetSite.ToString().ToLower();
        if (targetSite.Contains("checkupdatepermissions"))
        {
            flag = false;
        }
    }
    return flag;
}

Next, how do you get all properties of the User Profile?

SharePoint 2010 gets UserProfileManager by using both SPServiceContext and ServerContext, but I haven’t yet determined which way is better. In this article, I use the SPServerContext to get UserProfileManager for SharePoint 2010:

public Microsoft.Office.Server.UserProfiles.PropertyCollection GetUserProperties()
        {
            Microsoft.Office.Server.UserProfiles.PropertyCollection oPropertyCollection = null;
            try
            {
                System.Security.PermissionSet ps =
                    new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
                ps.Assert();

                Microsoft.SharePoint.SPServiceContext serviceContext =
                    Microsoft.SharePoint.SPServiceContext.Current;
                Microsoft.Office.Server.UserProfiles.UserProfileManager upm =
                    new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);

                oPropertyCollection = upm.PropertiesWithSection;
            }
            catch (Exception ex)
            {
                this.WriteInfoLog("GetUserProperties: " + ex.Message);
            }
            finally
            {
                System.Security.CodeAccessPermission.RevertAssert();
            }
            return oPropertyCollection;
        }

And for SharePoint 2007:

public Microsoft.Office.Server.UserProfiles.PropertyCollection GetUserProperties()
{
    Microsoft.Office.Server.UserProfiles.PropertyCollection oPropertyCollection = null;
    try
    {
        ServerContext context = ServerContext.Current;
        UserProfileManager upm = new UserProfileManager(context);
        oPropertyCollection = upm.PropertiesWithSection;
    }
    catch (Exception ex)
    {
        this.WriteErrorLog("GetUserProperties: " + ex.Message);
    }
    return oPropertyCollection;
}

Some default properties:

Name DisplayName
UserProfile_GUID Id
SID SID
ADGuid Active Directory Id
AccountName Account name
FirstName First name
LastName Last name
PreferredName Name
WorkPhone Work phone
Office Office
Department Department
Title Title
Manager Manager
AboutMe About me
PersonalSpace Personal site
PictureURL Picture
UserName User name
QuickLinks Quick links
WebSite Web site
PublicSiteRedirect Public site redirect
SPS-Dotted-line Dotted-line Manager
SPS-Peers Peers
SPS-Responsibility Responsibilities
SPS-Skills Skills
SPS-PastProjects Past projects
SPS-Interests Interests
SPS-School Schools
SPS-SipAddress SIP Address
SPS-Birthday Birthday
SPS-MySiteUpgrade My Site Upgrade
SPS-DontSuggestList Don’t Suggest List
SPS-ProxyAddresses Proxy addresses
SPS-HireDate Hire date
SPS-LastColleagueAdded Last Colleague Added
SPS-OWAUrl Outlook Web Access URL
SPS-ResourceSID Resource Forest SID
SPS-ResourceAccountName Resource Forest Account Name
SPS-MasterAccountName Master Account Name
Assistant Assistant
WorkEmail Work e-mail
CellPhone Mobile phone
Fax Fax
HomePhone Home phone

 

Notes:

  • User Profile is only available on MOSS 2007 and SharePoint 2010 Server. You cannot use this on Windows SharePoint Services or SharePoint Foundation 2010.
  • This code may require permissions you don’t currently possess. To make these calls, you need to change the trust level from WSS_Custom to Full in web.config (bad solution), or write the following code and put it in your GAC:

SPSUserProfileHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourNamespace
{
    [Serializable]
    [Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.InheritanceDemand,
        ObjectModel = true),
     Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.Demand,
        ObjectModel = true)]

    public class SPSUserProfileHelper
    {
        public Microsoft.Office.Server.UserProfiles.PropertyCollection YourMethods()
        {
            try
            {
                System.Security.PermissionSet ps =
                    new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
                ps.Assert();

                //Put your code here
            }
            catch (Exception ex)
            {
                //throw something
            }
            finally
            {
                System.Security.CodeAccessPermission.RevertAssert();
            }
        }
    }
}

 

See Also:

  • Configuring the User Profile Service in SharePoint 2010 [sharepointgeorge]
  • Getting User Profile Properties from SharePoint SSP using PowerShell [SharePoint Dev Wiki]
  • ProfileValueCollectionBase.CheckUpdatePermissions Method [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 ↗