1 minute read

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){}
            }
        }