How to add default associated groups when creating new site

Hi All,

I am trying to create a new subsite in sharepoint 2013 using CSOM (code is mentioned below). But no default groups (MEMBER, VISITOR, OWNER) are getting created in that site. When we try through UI we will got through a page "Set Up Groups for this Site" where we can specify these details.. Is it possible to do the same (creating default groups together with the site creation) through CSOM or powershell.

CSOM code:

WebCreationInformation creation = new WebCreationInformation();
            creation.Url = "NewSubSite6";
            creation.Title = "NewSubSite6";
            creation.UseSamePermissionsAsParentSite = false;
            Web newWeb = clientContext.Web.Webs.Add(creation);
            //clientContext.Load(newWeb);
            clientContext.ExecuteQuery();

Regards,

Shahabas

February 6th, 2015 12:50pm

Shahbas, here is the code:

private static void SetSecurityOnSubSite(ClientContext clientContext, ListItem item, bool confidential, Web newWeb)
        {
            try
            {
                if (confidential)
                {
                    newWeb.BreakRoleInheritance(false, false);
                    clientContext.ExecuteQuery();
                    Group ownerGroup = default(Group); Group memberGroup = default(Group); Group visitorGroup = default(Group);

                    // web has unique permissions, so create default assosiated groups (owners, members, visitors)
                    if (!newWeb.GroupExists(newWeb.Title + " Owners"))
                    {
                        ownerGroup = newWeb.AddGroup(newWeb.Title + " Owners", "", true);
                        clientContext.Load(ownerGroup);
                    }
                    if (!newWeb.GroupExists(newWeb.Title + " Members"))
                    {
                        memberGroup = newWeb.AddGroup(newWeb.Title + " Members", "", false);
                        clientContext.Load(memberGroup);
                    }
                    if (!newWeb.GroupExists(newWeb.Title + " Visitors"))
                    {
                        visitorGroup = newWeb.AddGroup(newWeb.Title + " Visitors", "", false);
                        clientContext.Load(visitorGroup);
                    }

                    // executequery in order to load the groups if not null
                    clientContext.ExecuteQuery();

                    newWeb.AssociateDefaultGroups(ownerGroup, memberGroup, visitorGroup);

                    newWeb.AddPermissionLevelToGroup(newWeb.Title + " Owners", RoleType.Administrator);
                    newWeb.AddPermissionLevelToGroup(newWeb.Title + " Members", RoleType.Contributor);
                    newWeb.AddPermissionLevelToGroup(newWeb.Title + " Visitors", RoleType.Reader);


                    FieldUserValue userValueCreatedBy = item[Constants.Projects.CreatedBy] as FieldUserValue;
                    User createdByUser = clientContext.Web.EnsureUser(userValueCreatedBy.LookupValue);
                    clientContext.Load(createdByUser);
                    clientContext.ExecuteQuery();

                    UserCreationInformation createdByUserCI = new UserCreationInformation();
                    createdByUserCI.LoginName = createdByUser.LoginName;
                    ownerGroup.Users.Add(createdByUserCI);
                    clientContext.ExecuteQuery();

                    foreach (FieldUserValue userValue in item[Constants.Projects.ProjectTeam] as FieldUserValue[])
                    {
                        User user = clientContext.Web.EnsureUser(userValue.LookupValue);
                        clientContext.Load(user);
                        clientContext.ExecuteQuery();
                        UserCreationInformation userCI = new UserCreationInformation();
                        userCI.LoginName = user.LoginName;
                        memberGroup.Users.Add(userCI);
                    }
                    clientContext.ExecuteQuery();
                }
            }
            catch (Exception)
            {                
                throw;
            }           
        }

Reference link: 

http://sharepoint.stackexchange.com/questions/116682/how-to-create-a-group-in-a-subweb-using-csom



Free Windows Admin Tool Kit Click here and download it now
February 6th, 2015 4:37pm

Thanks Pratik...
February 9th, 2015 1:52am

Hi pratik,

Is this code written using CSOM?..

I didnt find many APIs used in this code in CSOM class libraries ( https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.aspx )

Thanks and Regards,

Shahabas

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 3:24am

yes, it is in CSOM. Please refer the link i have provided.

Can you please let me know which API/Class you are not able to find it out. Are you getting any error? please provide more information if you're getting any error.

February 9th, 2015 5:07pm

newWeb.GroupExists()

newWeb.AddGroup()

newWeb.AddPermissionLevelToGroup()

newWeb.AssociateDefaultGroups(), .. are not part of microsoft.sharepoint.client

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 6:22am

Hi,

You are right. GroupExists, AddGroup aren't the CSOM methods. In order to check whether a group exists or not in a site, you get all its groups and then check if it is present or not. Here is an example which shows how to create a group in CSOM:

Web oWebsite = clientContext.Web;
GroupCollection collGroup = clientContext.Web.SiteGroups;
clientContext.Load(collGroup);
clientContext.ExecuteQuery();
CreateGroup(collGroup, "SampleAppGroup", oWebsite, clientContext);
private void CreateGroup(GroupCollection collGroup, string groupName, Web oWebsite, ClientContext clientContext)
{
    Group grp = collGroup.Where(g => g.Title == groupName).FirstOrDefault();
    if (grp == null)
    {
        GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
        groupCreationInfo.Title = groupName;
        groupCreationInfo.Description = "Description of " + groupName;
        Group oGroup = oWebsite.SiteGroups.Add(groupCreationInfo);
        RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
        RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.GetByType(RoleType.Contributor);
        collRoleDefinitionBinding.Add(oRoleDefinition);
        oWebsite.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
        clientContext.Load(oGroup, group => group.Title);
        clientContext.Load(oRoleDefinition, role => role.Name);
        clientContext.ExecuteQuery();
    }
}

I have written this post from remote event receivers, but this should help you in understanding the concept:

http://www.sharepointnadeem.com/2014/02/sharepoint-2013-app-event-receivers.html

Thanks,

Nadeem

February 12th, 2015 6:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics