Working with User Profiles Programmatically

In this lab I am providing a code to update user profile in SharePoint 2010. In the domain of SharePoint most of the time we need to update the user profile and make changes in the portal as per user requirement, allow certain option to allow and some to hide from user, In the contenxt of user profile,
In brief, we can easily retrieve a user’s profile, update the user profile. we can also create a user personal site by just call a simple method in sharpeoint 2010.
we use SpServiceContext class
All of the code must be run with administrative privileges for the user profile Service application to execute the code
Before dong any code, check the Userprofile Service Application service is started,
Go to the Central Administration – > Applicaiton Management – > User Profile Service Application
In this lab I will add code with comment each line, we are going to update the user email and optionto create a personal site if not available
(1) Create an empty SharePoint project, and set the level of trust to Farm solutions…..

(2) Add below mentioned references.. to interact with Userprofile domain……

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.Office.Server; // for user profile

references  to the userprofile.....
references to the userprofile.....

using Microsoft.Office.Server.UserProfiles; //for user profile

using System.Web.UI.WebControls; //for button on thepage or web controls on the page

using System.Web.UI;

Now add start code to make it programatically…..
// Create the Profile manager to null
UserProfileManager _userProMgr = null;// hold the collection of user profile object, use to access userprofile data
SPServiceContext svrCtx =SPServiceContext.Current; //get the contenxt of a page that is currently running
_userProMgr=new UserProfileManager(svrCtx); //initialize the new instance of

foreach (UserProfile profile in _userProMgr)
ProfileSubtypePropertyManager porpman=_userProMgr.DefaultProfileSubtypeProperties; //getsubtype properties of profile
string _accountName=profile[“AccountName”].Value.ToString();
string Emavil=string.Empty,personalsite=string.Empty;
if (profile[“WorkEmail”].Value!=null)
if (profile[“PersonalSpace”].Value!=null)
if (Emavil != string.Empty)
HyperLink emailhyper = new HyperLink();
emailhyper.Text = Emavil;
emailhyper.NavigateUrl = “mail to” + Emavil;

Button eMAILupdater = new Button();
eMAILupdater.CommandName = profile.RecordId.ToString();
eMAILupdater.Text = “Set to Default”;
eMAILupdater.Click += new EventHandler(eMAILupdater_Click);


if ((personalsite==”http://”) || (personalsite==null))

Button mysitecreate=new Button();
mysitecreate.Text=”Create My personalSite”;
mysitecreate.Click+=new EventHandler(mysitecreate_Click);


…….. if personal site is not found, then we add button to create one…..

void mysitecreate_Click(object sender, EventArgs e)
Button src = (Button)sender;
UserProfile userProfile = _userProMgr.GetUserProfile(long.Parse(src.CommandName));
catch (Exception ex)
{ this.Page.Response.Write(ex.InnerException.ToString()); }

……….update email if your want to change by the following method…….

void eMAILupdater_Click(object sender, EventArgs e)
Button src = (Button)sender;
// UserProfile UProfile = (UserProfile)_userProMgr.GetUserProfile(“salman”);
UserProfile UProfile = (UserProfile)_userProMgr.GetUserProfile(long.Parse(src.CommandName));
string _AccountName = UProfile[“AccountName”].Value.ToString();
int ipos = _AccountName.IndexOf(“\\”);
_AccountName = _AccountName.Substring(ipos + 1);
string _NewEmail = “”;
UProfile[“WorkEmail”].Value = _NewEmail;
catch (Exception exx)


…. for this lab I add a new application page on the portal……..
….next blog is on Taxonomiess…… hope this may help lil to the readers….





5 thoughts on “Working with User Profiles Programmatically

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s