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("https://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();

}
}
}
}
}

Comments

  • Anonymous
    September 28, 2008
    PingBack from http://www.easycoded.com/how-to-create-mysite-programmatically/

  • Anonymous
    December 10, 2008
    Hello,      I have create MySites for all the users in my Portal programatically like this, I am having a problem now administring the sites. When I go to central Admin and Click on "Site Collections List" and browse all the site collection list when it is getting to personal site, the page is show me an error message with "Error Occured, go back to the page..". Even when I try to ENUM sites using stsadm tool I am getting a message like <Site Url="http://xyz/personal/abcdf" Error="User cannot be found.">. I am thinking that because I have not assigned an owner to the site this is happening. Do we have to assign an ower to the Perosnal site like      PersonalSite.Owner = SPUser.... when we create the personal site Programatically? Any you please tell me if there is any work around to fix all the sites?          Thanks,            Ram

  • Anonymous
    May 17, 2009
    nao consigo acessar o meu email, chego a pensar que ainda nao estou conectado. o que e que faco crio outro.