Print Shortlink

Creating new SharePoint 2010 user profiles programatically

If you`re reading this post you`re familiar with the notion of a user profile in SharePoint 2010 (although it`s also a concept from MOSS). User profile a set of properties that is mapped to a username. You can access those properties from within a context of logged in user. You can modify and manage those properties which has variety of applications (many of which I implemented in my new SharePoint 2010 book).

Before you can do all of those things you need to create a SharePoint 2010 profile for a give user. One of the ways to create a profile automatically is to run a profile synchronization wizard that will pick up all of the relevant information from Active Directory and port it over to sharepoint. This approach is not going to be looked at here, instead we`ll focus on how to create a user profile programatically in your code – so that you can execute something like this in you r custom code.

First of all – you need to make sure you have your UserProfile Service Application installed and running; so ensure in your http://[central admin URL]/_admin/ServiceApplications.aspx you have profile service available:

profile service app

If you do – click on the profile service link and you will be taken to the Profile Service application management screen. From here, click Manage User Profiles

You will notice New Profile option, if you click on it – this is basically the interface that we will try to access programatically and achieve the same result as by filling out the form.

As you notice, there are few required fields. Those are not going to be required if you create profile object programatically, but it`s good to supply those since you never know why SharePoint may expect those.

Here is the piece of code that will create a user profile for you:

///

using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using System.Web;
using Microsoft.SharePoint.WebControls;
using System.Web.Profile;
using Microsoft.SharePoint.Portal.WebControls;

using Microsoft.Office.Server.UserProfiles;


using Microsoft.SharePoint;

using Microsoft.SharePoint.Taxonomy;

using System.Web;

using Microsoft.SharePoint.WebControls;

using System.Web.Profile;

using Microsoft.SharePoint.Portal.WebControls;

  }


/// 

public string AccountName { get; set; }

public string WorkPhone { get; set; }


public string Department { get; set; }

public string Title { get; set; }

public string DistinguishedName { get; set; }

public string WorkEmail { get; set; }

public string Office { get; set; }

public void Create()

{

string socialDataStatsSite = SPContext.Current.Site.Url;

using (SPSite siteColl = new SPSite(socialDataStatsSite))

{

SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);

UserProfileManager userProfileManager = new UserProfileManager(serviceContext);

UserProfile newProfile = userProfileManager.CreateUserProfile(AccountName, AccountName);

newProfile[PropertyConstants.WorkPhone].Add(WorkPhone);

newProfile[PropertyConstants.Department].Add(Department);

newProfile[PropertyConstants.Title].Add(Title);

newProfile[PropertyConstants.DistinguishedName].Add(AccountName);

newProfile[PropertyConstants.Office].Add(Office);

newProfile.Commit();

}

 

In the first part we got a hold of the UserProfileManager , then CreateUserProfile is all that we need to create a new user profile. The remaining commands make sure that required information is populated after which we execute commit additional field updates to the SharePoint Profile Service Database.

Check out other available method of UserProfileManager and you`ll discover few other useful methods to search for a profile etc. Here is MSDN more info on the object.

Good luck!