Monday, January 28, 2008

An Overview on MOSS User Pofiles

Target Readers: MOSS 2007 Developers & Designers

Keywords: MOSS User Profiles, MOSS 2007, .Net,C#, Profile Properties, UserProfile, UserProfileManager, User Profile.

Purpose of the Document:

Objective of this document is to throw some light on User profiles in MOSS.
This article will cover following topics:-

1. Introduction of Moss User Profile and Properties
2. Creating\Updating User Profiles using SharePoint Central Administration
3 Creating\Updating User Profiles and Properties using c# code in visual studio

Introduction :

Within any organization, a user has a unique id, name, a phone number, an address, a title and maybe even a photograph and many more properties.
In MOSS 2007, this information can be kept in sync with the other systems in the organization using BDC, an Active Directory domain controller, or an LDAP server.

By default, SharePoint 2007 automatically detects the domain controller and imports the user information from the Active directory service. User profiles can also be imported as per requirement and configured through UI.

User Profiles & Properties:-


User profile properties are name/value pairs attached to users and used for entering personal information about users (and groups).
For example, you could create a property called "Gender" that contains information regarding gender of the User. User profile properties appear as input fields in the MOSS Portal when you edit a user's profile values.
Profile & its Properties can be accessed/updated/created in following ways:-

1. Go to Shared Services Administration :-> Shared Service :-> User Profile and Properties

As shown in snapshot, User Profiles and properties is broadly divided into two sections:

1) Profile and Import Settings
2) User Profile Properties

1) Profile and Import Settings:-


Profile and Import Settings contain General information about profiles like Number of profiles, Import source etc and also provide links like Add user profile, View user profile etc .

2)User Profile Properties:-

This section consists two links named Add profile property & View profile Properties.
Some of these properties are public and appear on the public profile page. However, many of these properties are only visible to administrators. Only the SSP administrators can view and edit all user profile properties at the SSP level.
While creating new property, In Default Privacy Settings you can select the visibility level of property like it should be available to only that user, manager or everyone as shown below
While creating new property, by selecting Index checkbox these properties are indexed by MOSS Search engine and available for search .

In this way, User profile and properties are getting created through SharePoint Central Administration. Users profiles are maintained in the form of Sites called “MY Site” belong to particular SSP, which gets created when the user accesses it for the first time.

This site can be viewed through Shared Services Administration :-> Shared Service :-> User Profile and Properties :-> View user profiles .List of available profiles will be shown.

Now click on particular user and select Manage personal site option from the menu .

When Manage Personal Site is clicked, my site of that user will be opened , which has features like any SharePoint site.
The end user can maintain all relevant information in his or her "My Site" using the "My Profile" link.

This was the process of creating, managing and updating User profiles and their properties through SharePoint Central Administration.

This approach requires a lot of manual intervention at each and every step. Moreover each user login is required for my site creation. Besides all this, it can be tedious task if we need to create profiles of 10,000 users. Even though user profile import option is available, it cannot create and update user profiles dynamically or whenever required.

In all these conditions, rather than using previous approach user profile and properties creation\updation task can be done through code without manual intervention.

Visual studio .Net provides classes like UserProfileManager, UserProfile to access and update user profiles which are available in dynamic link library named
Microsoft.Office.Server.UserProfiles.

Add References :

The following references should be added to the project for accessing User profiles, which is available at location C:\Program Files\Common Files\Microsoft Shared\web
server extensions\12\ISAPI

· Microsoft.Office.Server

Add namespace directives :

The following namespace directives need to be added to the respective class

· using Microsoft.Office.Server
· using Microsoft.Office.Server.UserProfiles

Following code should be added to create\update user profiles in respective class file.

Code Snippet (in c#) for User Profile Creation:-

public static void CreateUserProfile(string UserAccountName)
{
using (SPSite site = new SPSite("http://moss"))
{
//ServerContext object of current site
ServerContext context = ServerContext.GetContext(site);
//UserProfileManager object to access MOSS user profiles
UserProfileManager profileManager = new UserProfileManager(context);
//Check whether user of given account name exists or not
if(!profileManager.UserExists(UserAccountName))
{
profileManager.CreateUserProfile (UserAccountName)
}
}
}


Code Snippet (in c#) to fetch User Profile & Properties:-

public static void GetUsername(string UserAccountName)
{
using (SPSite site = new SPSite("http://moss"))
{
//ServerContext object current site
ServerContext context = ServerContext.GetContext(site);

//UserProfileManager object to access MOSS user profiles
UserProfileManager profileManager = new UserProfileManager(context);

//Find the user profile of given account name
UserProfile user = profileManager.GetUserProfile(UserAccountName);

//Fetching the Name of the Current user
if (user [“UserName”].Value != null)
{
String UserName=user [“UserName”].Value.ToString();
}
}
}


Code Snippet (in c#) to update User profile property:-

public static void GetUsername(string UserAccountName)
{
using (SPSite site = new SPSite("http://moss"))
{

//ServerContext object of current site
ServerContext context = ServerContext.GetContext(site);

//UserProfileManager object to access MOSS user profiles
UserProfileManager profileManager = new UserProfileManager(context);

//Find the user profile of given account name
UserProfile user = profileManager.GetUserProfile(UserAccountName);

//Assigning value to Department property of the given user
if (user [“Department”].Value == null)
{
String strDept;
strDept=”Mfgx”:
user[“Department”].Value.ToString()=strDept;
}
}
}

References:-
http://msdn2.microsoft.com/en-us/library/bb847941.aspx http://msdn.microsoft.com/msdnmag/issues/05/10/CuttingEdge/