Create User Profiles using SharePoint Object Model
In this post i will show you how you can use the SharePoint object model to create User Profiles programmatically. For the code to work make sure you have a User Profile Service application created and associated to your web application.
You have to add references to two assemblies which will be available in the 14 hive/ISAPI
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles
You have to get the ServiceContext first and then get the UserProfileManager from the service context.
The UserProfileManager provides ways to add and edit profile to your SharePoint.
string socialDataStatsSite = "http://servename:port/";
You have to add references to two assemblies which will be available in the 14 hive/ISAPI
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles
You have to get the ServiceContext first and then get the UserProfileManager from the service context.
The UserProfileManager provides ways to add and edit profile to your SharePoint.
string socialDataStatsSite = "http://servename:port/";
using(SPSite siteColl = new SPSite(socialDataStatsSite))
{
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
UserProfileManager userProfileManager = new UserProfileManager(serviceContext);
UserProfile profile = null;
bool existingUser = userProfileManager.UserExists(AccountName);
if (existingUser)
{
profile = userProfileManager.GetUserProfile(AccountName);
}
else
{
profile = userProfileManager.CreateUserProfile(AccountName, AccountName.Split('|')[2]);
}
profile["WorkPhone"].Value = WorkPhone;
profile["Department"].Value = Department;
profile["Title"].Value = WorkTitle;
profile["Office"].Value = Office;
profile["WorkEmail"].Value = WorkEmail;
profile["VendorNumber"].Value = "123456";
profile.Commit();
}
The above code will check if a user profile already exists. If it does not it creates a new user profile. If it exists then it updates the values for the user profile
can non AD user account created ? i wanted to understand that is it possible to add non AD accounts to SharePoint user profile ?
ReplyDelete