Simplicity is prerequisite for reliability

First, solve the problem. Then, write the code.

  • Subscribe

  • January 2008
    M T W T F S S
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  

Archive for January 14th, 2008

Sharepoint User Profiles

Posted by ricetristan on January 14, 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. For example, you can display all current profiles via the following:

using(Microsoft.Sharepoint.SPSite site = new Microsoft.Sharepoint.SPSite("http://url")){     Microsoft.Office.Server.UserProfiles.UserProfileManager mgr = new UserProfileManager(ServerContext.GetContext(site));

    foreach (UserProfile profile in mgr)

    {

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

    }

}

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

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

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.

Either way, there are MANY examples available on the web that provide samples of code used to access profile properties. In a future post, I’ll discuss an additional method of user profile access using the built-in Sharepoint web services. Hope this helped!

-Tristan

Posted in .NET Development, Sharepoint | 7 Comments »