When using MySites it is good to know that MySites are created on an as needed basis, this is to save disk space and resources. There are times when you might want to create a number of MySites before users go to them the first time. We will look at how to do that using C# code and the SharePoint object model.
NOTE: This console application assumes you have the correct permissions such as an administrator and is running on a machine that has the SharePoint object model.
Create Console Application to host MySite Code
To automatically create users MySites ahead of time using the SharePoint object model you can simply create a console application.
- Click File->New->Project, then select Console Application from the list of templates.
- Next add a reference to the following assemblies
Add MySite C# Code
Now that you have your application to run the code, you will use two major objects
- Microsoft.Office.Server.UserProfiles.UserProfile – namely you will use the method CreatePersonalSite().
- Microsoft.Office.Server.UserProfiles.UserProfileManager – this object is used to get each user.
The code to create the sites is as follows, just add it to your console application and make changes as needed.
//Get the site associated with the users
using (SPSite spSite = new SPSite(@http://localhost))
//Create the server context because you are in a console application
ServerContext siteContext = ServerContext.GetContext(spSite);
UserProfileManager pmManager = new UserProfileManager(siteContext);
//Loop through all of the users to create sites for
string strUserName = "devcow\\brendon";
UserProfile spUser = pmManager.GetUserProfile(strUserName);
if (spUser.PersonalSite == null)
Console.WriteLine("This may take a few minutes...");
For an in-depth look at the API and programmatically using the Social Computing features of SharePoint 2007 check out chapter 8 of Professional SharePoint 2007 Development