Monday, July 27, 2015

Create default associated groups for Sharepoint site collection via client object model (CSOM)

When you create site collection via CSOM default Sharepoint groups (Visitors, Members and Owners) are not created by default. So when you will go to Site settings > People and groups most probably you will only see Everyone and Excel Services Viewers:

image

In order to create these groups you need to write additional code by yourself. Here it is:

   1: using (var ctx = new ClientContext("http://example.com"))
   2: {
   3:     ctx.Load(ctx.Web);
   4:     ctx.ExecuteQuery();
   5:     CreateDefaultAssociatedGroups(ctx);
   6: }
   7:  
   8: private static void CreateDefaultAssociatedGroups(ClientContext ctx)
   9: {
  10:     EnsureDefaultAssociatedGroup(ctx, GetGroupName(ctx, "Visitors"),
  11:         String.Format("Use this group to grant people read permissions to the SharePoint site: {0}",
  12:             ctx.Web.Title));
  13:     EnsureDefaultAssociatedGroup(ctx, GetGroupName(ctx, "Members"),
  14:         String.Format("Use this group to grant people contribute permissions to the SharePoint site: {0}",
  15:             ctx.Web.Title));
  16:     EnsureDefaultAssociatedGroup(ctx, GetGroupName(ctx, "Owners"),
  17:         String.Format("Use this group to grant people full control permissions to the SharePoint site: {0}",
  18:             ctx.Web.Title));
  19: }
  20:  
  21: private static void EnsureDefaultAssociatedGroup(ClientContext ctx, string groupName,
  22:     string description)
  23: {
  24:     if (string.IsNullOrEmpty(groupName))
  25:     {
  26:         return;
  27:     }
  28:  
  29:     if (GroupExist(ctx, groupName))
  30:     {
  31:         return;
  32:     }
  33:     var ci = new GroupCreationInformation
  34:     {
  35:         Title = groupName,
  36:         Description = description
  37:     };
  38:     var group = ctx.Web.SiteGroups.Add(ci);
  39:     var roleType = RoleType.None;
  40:     if (groupName.EndsWith("Visitors"))
  41:     {
  42:         ctx.Web.AssociatedVisitorGroup = group;
  43:         ctx.Web.AssociatedVisitorGroup.Update();
  44:         roleType = RoleType.Reader;
  45:     }
  46:     else if (groupName.EndsWith("Members"))
  47:     {
  48:         ctx.Web.AssociatedMemberGroup = group;
  49:         ctx.Web.AssociatedMemberGroup.Update();
  50:         roleType = RoleType.Contributor;
  51:     }
  52:     else if (groupName.EndsWith("Owners"))
  53:     {
  54:         ctx.Web.AssociatedOwnerGroup = group;
  55:         ctx.Web.AssociatedOwnerGroup.Update();
  56:         roleType = RoleType.Administrator;
  57:     }
  58:  
  59:     ctx.Web.Update();
  60:     ctx.ExecuteQuery();
  61:  
  62:     if (roleType != RoleType.None)
  63:     {
  64:         var roleDefinition = ctx.Web.RoleDefinitions.GetByType(roleType);
  65:         ctx.Load(roleDefinition);
  66:         ctx.ExecuteQuery();
  67:  
  68:         var roleDefinitions = new RoleDefinitionBindingCollection(ctx);
  69:         roleDefinitions.Add(roleDefinition);
  70:         ctx.Web.RoleAssignments.Add(group, roleDefinitions);
  71:         ctx.ExecuteQuery();
  72:     }
  73: }
  74:  
  75: private static string GetGroupName(ClientContext ctx, string suffix)
  76: {
  77:     return String.Format("{0} {1}", ctx.Web.Title, suffix);
  78: }
  79:  
  80: private static bool GroupExist(ClientContext ctx, string groupName)
  81: {
  82:     var groups = ctx.Web.SiteGroups;
  83:     ctx.Load(groups);
  84:     ctx.ExecuteQuery();
  85:     foreach (var g in groups)
  86:     {
  87:         if (g.Title == groupName)
  88:         {
  89:             return true;
  90:         }
  91:     }
  92:     return false;
  93: }

After executing this code you will see 3 additional groups in Site settings > People and groups:

image

If you will go to Site settings > Site permissions you will find that Visitors group has Read permissions on the site, Members – Contribute and Owners – Full control:

image

Hope that this information will help you.

1 comment: