Wednesday 9 November 2011

How to create MySite Programmatically


 

SharePoint provides APIs to create MySite programmtically. UserProfile object provides a method to create personal site. Following console application creates MySite for a given account:

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


namespace UserProfileCreate
{
class Program
{
static void Main(string[] args)
{

using (SPSite site = new SPSite("http://servername"))
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);

string accountName = "domainname\\username";
UserProfile userProfile;
if (profileManager.UserExists(accountName))
{
userProfile = profileManager.GetUserProfile(accountName);

userProfile.CreatePersonalSite();

}
}
}
}
}

No comments:

Post a Comment