Provisioning My Sites SharePoint 2010

In certain scenarios you would like to provision the My Sites of the users before you go live with the environment. Within SharePoint 2010 you have got several options to complete this task.

One of the options is to create a PowerShell script. With PowerShell you have the ability to read through the user profile store that is used for a certain web application and create a My Site for each profile in the profile store. If you want to use PowerShell to accomplish this task you can use the following script:

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") 
 
$siteurl = "http://yoursiteurl"     
 
$site = New-Object Microsoft.SharePoint.SPSite($siteurl) 
$context = [Microsoft.Office.Server.ServerContext]::GetContext($site) 
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context) 

$upm.Count;
$profiles = $upm.GetEnumerator()
foreach ($userobject in $Profiles ) {

    # start loop here 
    $user = $userobject.item("AccountName")
    $user
    if ($upm.UserExists($user)) { 
        $profile = $upm.GetUserProfile([string]$user) 
     
        # there are other exceptions you can catch, check out the UserProfiles class 
        trap [Microsoft.Office.Server.UserProfiles.PersonalSiteExistsException] { 
            Write-Host "personal site already exists for $user" 
            continue 
        } 
        $profile.CreatePersonalSite();
        trap {
        Write-Host "Error creating site for $user" -ForeGroundColor Red
        continue
        }
        Write-Host "Personal site for $user created..." -ForeGroundColor Green
    }  else { 
      Write-Host: "user $user did not exist" 
    } 
}
# end loop here 
$site.Dispose() 

The second option you have is creating a C# application. This c# application will to exactly the same as the powershell script. The method to create the MySites is displayed below.

private static void CreateMysite(string siteUrl)
        {
            using (SPSite site = new SPSite(p_2))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site.WebApplication.ServiceApplicationProxyGroup, SPSiteSubscriptionIdentifier.Default);
                UserProfileManager profileConfigManager = new UserProfileManager(serviceContext);
                try
                {
                    long count = profileConfigManager.Count;

                    for (int i = 1; i <= count; i++)
                    {
                            UserProfile profile = profileConfigManager.GetUserProfile(i);
                            Console.WriteLine(profile.DisplayName);
                            profile.CreatePersonalSite(1043);
                    }
                }
                catch (UserNotFoundException) {}
                catch(PersonalSiteExistsException){}
            }
        }

In the application above I catch the UserNotFoundException this is done because it can occur that a user profile is present but not the user.

3 Replies to “Provisioning My Sites SharePoint 2010”

  1. Thanks for this post. I posted a question in the SharePoint Admin forum how to do this and someone pointed me to this post of yours.

  2. Hi,

    I’m no developer so forgive the lack of detail. I’ve run this powershell script but receive the following error for every user.

    appex\SHunteering
    Error creating site for appex\SHunteering
    Personal site for appex\SHunteering created…

    While the output states the personal site was created, it wasn’t.

    Any suggestions?

    Many thanks,

    Joel

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.