In a previous post I showed how to make a user click through go to their personal site. In the article I couldn’t find a way to get the URL of the correct site collection without iterating through the collections.
Well in MOSS 2007 there is a way to do it.
We will need the following namespaces:
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles
So the first job is to get a context and the UserProfileManager object. You can get the context using amongst others the SPSite or SPWebApplication object.
In this example we use a pre-created SPSite object.
ServerContext sContext = ServerContext.GetContext(site);
UserProfileManager pm = new UserProfileManager(sContext);
From here you can use the pm.MySiteHostUrl Property. This will get you the protocol, the hostname and any ports. From here you build any specific pages or URL parameters.
Alternatively you can use the pm.GetUserProfile(~LoginName~) to get the UserProfile object and from there use the PublicUrl.AbsoluteUri to return the full URL to the person.aspx page with the applicable accountname parameter set.
Full Code Listing
using (SPSite site = new SPSite(“http://…”)){
ServerContext sContext = ServerContext.GetContext(site);
UserProfileManager pm = new UserProfileManager(sContext);
string url=pm.MySiteHostUrl;
UserProfile up=pm.GetUserProfile(“AD\\MyGSN”);
string personnelURL=up.PublicUrl.AbsoluteUri;
}