Login

Create MySites using C# and the SharePoint 2007 object model


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.

  1. Click File->New->Project, then select Console Application from the list of templates.
  2. Next add a reference to the following assemblies
    • Microsoft.SharePoint
    • Microsoft.Office.Server

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";
    if (pmManager.UserExists(strUserName))
    {
        UserProfile spUser = pmManager.GetUserProfile(strUserName);
        if (spUser.PersonalSite == null)
        {
            Console.WriteLine("This may take a few minutes...");
            spUser.CreatePersonalSite();
        }
    }
}

Addition Resources

sharepoint-pro 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

 
 
 
 
 
Digg It!  StumbleUpon  Reddit  Del.icio.us  NewsVine  Furl  BlinkList  Ma.gnolia  Technorati

» Trackback URL to this Post

http://sharepointguys.com/brendon/trackback.ashx?id=35

Comments

#1 Sam on 7.22.2009 at 4:37 PM

Brendon,

Is there a way to create MySite according to a specific site template? Here is what I have in mind: Create a MySite manually, go in the new MySite site and make what ever changes to the left menues and to the web parts, then save it as a template. Then create the new MySites with code based on that template. Or, create the new MySites with code then change it according to the saved template. I have your book and i could not find something in there to help me do this task. Any help is greatly appeciated.

Thanks,

-Sam

» Trackbacks & Pingbacks

  1. First follow the steps to create a console application for working with SharePoint 2007 Social Computing features Now that you have your application ready to go you can display users that already have a MySite using the code below. Display Users that