PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to access the MOSS UserProfile through the ProfileManger

Posted by PANVEGA on November 7, 2008

To those doing software development for a Sharepoint environment, user profiles can be a mysterious and confusing area. This post will detail some of the basics, and outline some snafoos you may run into if you find yourself doing software development that interacts with the Sharepoint user profile information.

First, it should be explained that the user profile setup differs depending on whether you are operating in a full MOSS environment, or simply using the basic WSS structure. In the full MOSS environment, user profile information is stored in a central database, and profile information is shared across site collections that may be hosted on your server. This profile info can be read and/or edited via the UserProfileManager class within the Microsoft.Office.Server dll.

Read a little deeper about Profiles administration in more in one of my previous posts

https://panvega.wordpress.com/2008/11/05/mapping-user-properties-in-the-sp-central-administration

As mentioned above, this profile information is held in a global profile database, and persists across different site collections on your server. On the other hand, if you are operating in only a WSS environment, the user profile information is stored directly in a hidden list specific to an individual site collection, which is called the User Information List. If multiple site collections exist, the server will store separate user profiles for each one. These profiles can be access via the following:

using(Microsoft.Sharepoint.SPSite site = new Microsoft.Sharepoint.SPSite("http://url")){

    Microsoft.Sharepoint.SPWeb web = site.RootWeb;

    Microsoft.SharePoint.SPList userList = web.Lists["User Information List"];

    foreach (Microsoft.SharePoint.SPListItem user in userList.Items)

    {

        Console.WriteLine(user["ID"] + ": " + user["Name"]);

    }

}

Before you begin screaming that this second method does not make use of the SPUser class, you should note that the SPUser class provides access to fewer properties than those that are available by accessing this user list directly. However, for sake of completeness (and for those of you who shy away from tinkering under the hood), a list of all SPUser objects associated with the site collection can be obtained via the following:

using(Microsoft.Sharepoint.SPSite site = new Microsoft.SharePoint.SPSite("http://url")){

    Microsoft.SharePoint.SPWeb web = site.RootWeb;

    foreach(Microsoft.SharePoint.SPUser user in web.Users){

        System.Console.WriteLine(user.ID + ": " + user.Name);

    }

}

As I stated above, the profile properties available from the SPUser class are fewer than those available from the User Information List. The SPUser class allows you to access/change the following profile properties (e.g. those that show up in the My Settings page):

  • ID
  • Email
  • Name
  • LoginName
  • Notes
  • DisplayName
  • PrincipalType…

By comparison, the User Information List provides access to the following profile properties (plus many more that I’m not listing here):

  • Title
  • Name
  • EMail
  • Notes
  • SipAddress
  • Locale
  • IsSiteAdmin
  • Picture
  • Department
  • JobTitle
  • IsActive
  • FirstName
  • LastName
  • WorkPhone
  • UserName
  • WebSite
  • Office
  • ID
  • Modified
  • Created
  • Author
  • Editor

http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.propertyconstants_members.aspx

As you can see, these different methods of accessing user profile information can be a bit confusing. A good first step in tackling this problem is to look at the default layout of your website. If you have a link for My Site at the top, near your name, then you are in a full MOSS environment, and should access user profiles via the first method described. If this link does not exist, you may be only using WSS profiles, in which case either of the other methods will be your preferred mode of access.

CodeSnippet, Accessing the WSS Profile of the SP MySite for the PeopleEditor:

protected PeopleEditor editor;…..

ArrayList peEntities = editor.Entities;

// 2. cast object Entities in PickerEntity class
PickerEntity pickEn = (PickerEntity)peEntities[i];

if (pickEn.IsResolved)
{

Hashtable hstEntityData = pickEn.EntityData;
builder.AppendLine(pickEn.DisplayText);
builder.AppendLine(pickEn.Description);

builder.AppendLine(Convert.ToString(hstEntityData[“DisplayName”]));
builder.AppendLine(Convert.ToString(hstEntityData[“Email”]));
builder.AppendLine(Convert.ToString(hstEntityData[“PrincipalType”]))

In this Code above you can only access the basic properties.

How to retreive all Profile Properties with the SPUserProfileManager and store them in a drop down list:

;
SPSite site = SPContext.Current.Site;
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
PropertyCollection props = profileManager.Properties;
DropDownList ddl = new DropDownList();
foreach (Property prop in props)
{
ddl.Items.Add(prop.Name);
}

Important SP Objects:

  • SPUserInfo
  • SPUserProfileManager
  • PickerEntity
  • UserProfile
  • PeopleEditor

Here is an example how to access the MOSS UserProfile rather than the WSS profile.

for (int i = 0; i < peoplePicker.ResolvedEntities.Count; i++)
{
PickerEntity pickerEntity = (PickerEntity)peoplePicker.ResolvedEntities[i];
SPUserInfo userInfo = new SPUserInfo();
userInfo.LoginName = pickerEntity.Key;

if (isGroup(web, userInfo))
{
// Loop through all members of this group and add them individually
foreach (SPUser usr in web.Groups[userInfo.LoginName].Users)
{
// Add the User
if (profileManager.UserExists(usr.LoginName))
{
UserProfile user = profileManager.GetUserProfile(usr.LoginName);
givenName = user[PropertyConstants.FirstName];
lastName = user[PropertyConstants.LastName];
email = user[PropertyConstants.WorkEmail];
}
}
}
else
{
// pickerEntity relates to an individual user
// Add the User
if (profileManager.UserExists(userInfo.LoginName))
{
UserProfile user = profileManager.GetUserProfile(userInfo.LoginName);
givenName = user[PropertyConstants.FirstName];
lastName = user[PropertyConstants.LastName];
email = user[PropertyConstants.WorkEmail];
}
}
}


Note:

Actually you can retreive all Users and Groups in the entire Active Directory forest when using the PeoplePicker Addressbook. However when you use the profileManager.UserExists(usr.LoginName), the Profilemanager looks only into the MOSS UserProfile store. Otherwise you have to import all users from the entire forest or make a custom specific AD User import.

Or access the properties without the PropertyConstants:

UserProfile user = profileManager.GetUserProfile(userInfo.LoginName);

string givenName = user[FirstName].Value.toString();

More Information:

http://www.codeplex.com/MOSSProfileImport

http://blogs.technet.com/tothesharepoint/archive/2008/10/21/3139351.aspx

Good luck!

Advertisements

One Response to “How to access the MOSS UserProfile through the ProfileManger”

  1. Nice Article and it helped me a lot. Can you tell me how to access the custom fields which are added by users themselves. Eg, I have i custom field, NickName

    UserProfile user = profileManager.GetUserProfile(userInfo.LoginName);
    NickName = user[PropertyConstants.NickName];

    How to do this? how can access the custom properties?

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

 
%d bloggers like this: