Tuesday, March 17, 2009

Reading User Profiles from SharePoint(MOSS) programatically

User profile for SharePoint can be imported from Active directory(using master connection from current domain) and SQL server using Business data catalog(BDC) using an application definition file. Audiences can be created from the user profile information based on some rules.

To read the user profile information, we need the following namespaces.
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;
using Microsoft.SharePoint;

// GET THE current web
SPWeb web = SPContext.Current.Web;
// Get the logged in user
SPUser user = web.CurrentUser;

// Get the user context
ServerContext ctx = ServerContext.GetContext(HttpContext.Current);
// Get the user profile associated with the context
UserProfileManager userprofilemgr = new UserProfileManager(ctx);

// Get the user profile based on logged in User
UserProfile userprofile = userprofilemgr.GetUserProfile(user.LoginName);
// Reading User profile Information such as name, designation etc
string name = userprofile["First Name"];

userprofilemgr.GetUserProfile method in above code fails if the user does not have access to manage Audience. The permission to access the User profile is given by the following steps.
1. Open the SharePoint 3.0 Central Administration
2. Click on the Shared Services Provider (Farm SSP in this build) i.e. SSP linked to the current site.
3. Click on ‘Personalization services permissions’ under ‘User Profiles and My Sites’
4. Select the user ‘NT AUTHORITY\Authenticated Users’ and click on ‘Modify Permissions of Selected Users’
5. Select ‘Manage audiences’ and click on ‘Save’
6. ‘Manage Audiences’ right will be added to ‘NT AUTHORITY\Authenticated Users’

No comments: