Tuesday, December 19, 2006

Automatic My Site creation with SharePoint 2007

Last week I got the question if it would be possible to automatically create the My Site for a specific user in SharePoint Server  2007. Normally the My Site gets created when the user accesses it for the first time. I got a some advice from a fellow MVP - Pierre  but when I looked at it a little more closely I noticed that some of the classes used in SharePoint Portal Server 2003 are now obsolete.

public static void CreatePersonalSite(string sAccount)
{

      //get portal site context from topology
      string strUrl = "http://moss";

      //TopologyManager Obsolete
TopologyManager tm = TopologyManager();
      PortalSite ps = tm.PortalSites[new Uri(strUrl)];
PortalContext pc = PortalApplication.GetContext(ps);

//initialize user profile config manager object

UserProfileManager upm = UserProfileManager(pc);
UserProfile u = upm.GetUserProfile(sAccount);

u.CreatePersonalSite();

}

So how does the code look like using the new SharePoint Server 2007 object model.


public static void CreatePersonalSite(string sAccount)
{
    using (SPSite site = new SPSite("http://moss"))
    {
       ServerContext context = ServerContext.GetContext(site);
       UserProfileManager profileManager = new UserProfileManager(context);
       UserProfile u = profileManager.GetUserProfile(sAccount);
       u.CreatePersonalSite();

    }
}


tags: , , , ,

 
 

4 comments:

Anonymous said...

thanks for this post :-)

Pierre

Oskar Austegard said...

Just for anyone else trying to implement this - this requires a reference to Microsoft.Office.Server (in the 12 hive/ISAPI folder)

Oskar
http://SharePointSearch.info

Unknown said...

where would you put this code?

jopx said...

You could create a console app and run this on the server itself