If Users aren’t Site Collection Administrators of Their MySite …

Many organizations don’t want their users to be site collection administrators of their MySite and the owner property will point to some administrative account. Here is how to find out which user’s MySite a given page belongs to in that case. To allow for multiple web front-ends only the path is checked.

public static SPUser GetMySiteOwner(SPWeb mySite) {
 SPUser result = null;
 string mySiteUrl = mySite.Url;
 if (mySiteUrl.EndsWith("/") == false) {
  mySiteUrl += "/";
 }
 Uri mySiteUri = new Uri(mySiteUrl);
 UserProfileManager upm = new UserProfileManager(
  SPServiceContext.GetContext(mySite.Site));
 for (int i = 0; i < mySite.SiteUsers.Count; i++) {
  string loginName = mySite.SiteUsers[i].LoginName;
  if (upm.UserExists(loginName)) {
   UserProfile up = upm.GetUserProfile(loginName);
   if (up != null) {
    Uri personalUrl = new Uri(up.PersonalUrl.AbsoluteUri);
    Uri publicUrl = new Uri(up.PublicUrl.AbsoluteUri);
    if (mySiteUri.LocalPath.StartsWith(personalUrl.LocalPath)
     || mySiteUri.PathAndQuery.StartsWith(publicUrl.PathAndQuery)) {
     result = mySite.SiteUsers[i];
    }
   }
  }
 }
}

External References

  • Modifying My Site Owner Security. This post has some background info on changing the MySite owner and also shows how to correct the site’s title as SharePoint has a timer job setting it to the site collection administrator’s name.
Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s