2

Given the following situation:

Within our company's farm the creation of MySites / PersonalSites is limited to members of a special AD Group ("MysiteCreator").

This group has been given the following permissions within the User Profile Service:

Screenshot from Central Administration

All the other, "casual" users are not permitted to create a MySite / Personal Site.

Now I'm wondering how to check by code if the current user is permitted to create a personal site and then create it on demand.

I've read some posts about checking the UserProfile.PersonalSiteCapabilities == "14" (see here and here). So I wrote this piece of code:

internal bool EnsurePersonalSite()
{
  UserProfileManager upm = new UserProfileManager(SPServiceContext.Current);
  UserProfile up = upm.GetUserProfile(true);

  // User not permitted (?) to create personal site
  if ((up.PersonalSiteCapabilities & PersonalSiteCapabilities.Social)
    != PersonalSiteCapabilities.Social)
  {
    return false;
  }

  // User permitted ==> create personal site
  up.CreatePersonalSiteEnque(true);
  return false;

  // Creation ongoing
  if (up.PersonalSiteInstantiationState != PersonalSiteInstantiationState.Created
    || up.PersonalSite == null)
  {
    return false;
  }

  return true;
}

The casual user has a PersonalSiteCapabilities == 0 entry in his user profile (shown in Central Administration). Adding the user to the "MysiteCreator" AD group does not change this value to 14 and my code will always return false.

If I would set PersonalSiteCapabilities = 14 for all users my code will always return true no matter if the user is member of the AD Group "MysiteCreator" or not.

So what's the best, safest way to ensure a MySite / Personal Site for users only belonging to a special AD group?

  • SharePoint can sometimes take around a day to recognize that a person is part of an AD group. Have you tried waiting next day? (It's caused due to cache, so you can try an iisreset and see if it starts to work...) – Choggo Jan 30 '15 at 16:03
  • @Choggo: I'll give it a try to wait 24h. But after that period of time the PersonalSiteCapabilities should have been set to 14 for all users (profiles) belonging to the special AD group, right? – jcp Jan 30 '15 at 16:08
  • Yes. If you take a user that hasn't logged in to SP in the last 10 hours (i think it's the default) it might already work for him. Another test you can run, to see if it's a cache issue, is to give that AD group permissions to see an item and see if the user that is reporting 0 can see the file. If he can see the file, it's not a cache issue – Choggo Jan 30 '15 at 16:18
1

I'm not sure the value will be 14 unless the person has actually created a mysite.

Here is how SharePoint is doing it but it involves some internal methods:

SPServiceContext context = SPServiceContext.GetContext(SiteInfo.CurrentWeb().Site);
UserProfileApplicationProxy proxy = UserProfileApplicationProxy.GetProxy(context);
if (proxy.CheckUserAccess(context, UserProfileApplicationUserRights.CreatePersonalSite))
{

}

You can use reflection to call the GetProxy and CheckUserAccess methods.

  • 1
    That seems to be very promising. Where (namespace / class) have you found that piece of code? – jcp Feb 2 '15 at 12:39
  • 1
    I found a few instances of similar code but this code was most concise here: Microsoft.SharePoint.Portal.WebControls.AddQuickLinkToolBarButton.OnPreRender. But in that code they were checking for UsePersonalFeatures so I just changed it to CreatePersonalSite. – Steve Lineberry Feb 2 '15 at 12:52
1

With Steve Lineberry's answer pointing to reflection I've found an internal property CanCreatePresonalSite of the Microsoft.Office.Server.UserProfiles.UserProfile class which I'm now using this way:

internal static bool CheckPersonalSiteCreation(UserProfile up)
{
  Type type = up.GetType();

  PropertyInfo pi = type.GetProperty(
    "CanCreatePersonalSite",
    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

  object value = pi.GetValue(up);

  return (bool)value;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.