Select Page

Programmatically create MySite for an user

To use social features in SharePoint, it is necessary that the user has a mysite created. Two core SharePoint lists – Microfeed and Social – are created inside user’s MySite. For serious enterprise social scenarios, it is important to ensure that all the users have created my site. For that purpose, you can use this piece of code in some background process (like timer job).

using (SPSite site = new SPSite(siteName))
{
    SPServiceContext context = SPServiceContext.GetContext(site);

    UserProfileManager profileManager = new UserProfileManager(context);

    if (profileManager.UserExists(username))
    {
        UserProfile userProfile = profileManager.GetUserProfile(username);

        userProfile.CreatePersonalSite();
    }
    else
    {
        trow new Exception(string.Format("User Profile for user {0} does not exist", username));
    }

}

Previous

Next