Wednesday, February 25, 2009

How to access SharePoint User Profile from C# code

Hi All,

We all know sharepoint user profile stores users information. Sometimes we use out of box features to update these profile values like mysite stc but many times we come across the situation where we need to access uer profile from code, update the properties and save the profile.

In this post, I will tell you the step by step way to access user profile and to access/update the property field.

Step 1: Refer the following dlls in the code:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;



Step 2: Create the instance of SPSite object:

using (SPSite ospSite=new SPSite("http://servername:portname")
{}

where http://servername:portname is your sharepoint application url.
Here we have used using to create the instance of SPSite since 'using' automatically disposes the object when the block gets finished.

Withing 'using' block , add the following codes:


Step 3: Create the instance of servercontext object:

ServerContext ospServerContext = ServerContext.GetContext(ospSite);


Step 4: Create the instance of userporfile object propertycollection object:

UserProfileManager ospUserProfileManager = new UserProfileManager(ospServerContext);
UserProfile ospUserProfile = ospUserProfileManager.GetUserProfile(UniqueId);
Microsoft.Office.Server.UserProfiles.PropertyCollection propColl = ospUserProfile.ProfileManager.PropertiesWithSection;

here uniqueid is the id by which each profile is recognized individually. Eacn user profile has a uniqueId many ny 5-2-1 id if it is an AD account or GUID.


Step 5: Check whether the USerprofile object is null or not

if (ospUserProfile != null && propColl != null)
{}

Step 6: Iterate thorugh all the proerties in property collection:

foreach (Property prop in propColl)
{}

In the for loop you can get the property name and value like this:

To get property name: prop.Name
To get property value: ospUserProfile[prop.Name].Value

To set a value to a proerty : ospUserProfile[prop.Name].Value ="SOME VALUE";

To save the user profile after updation: ospUserProfile.Commit();

This will get the userporfile of a user and update the profile.


The whole code is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using System.IO;
using System.Data;
using Microsoft.Office.Server.UserProfiles;

namespace Program1
{

public static void Main(string args[])
{
using (SPSite ospSite = new SPSite(webApp))
{
try
{
ServerContext ospServerContext = ServerContext.GetContext(ospSite);
UserProfileManager ospUserProfileManager = new UserProfileManager(ospServerContext);
UserProfile ospUserProfile = ospUserProfileManager.GetUserProfile(uniqueId);
Microsoft.Office.Server.UserProfiles.PropertyCollection propColl = ospUserProfile.ProfileManager.PropertiesWithSection;

if (ospUserProfile != null && propColl != null)
{
foreach (Property prop in propColl)
{
Console.WriteLine("property Name : " + prop.Name);
Console.WriteLine("proerpty Value : " + ospUserProfile[prop.Name].Value);

//If updation is required
ospUserProfile[prop.Name].Value="SOME VALUE";

}
ospUserProfile.Commit();
}
}
catch(Exception ex){
Console.WriteLine(ex.Message);
}
}
}
}

3 comments:

Anonymous said...

Good work buddy. Send some more

Anonymous said...

Nice one..

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Development Company