Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to get this to work all day and its just not wanting to work. I am building a application to add the three default groups to sites in hopes of cleaning up permissions. The code I am about to show you will create the groups but they will not show up when i go to permissions. It just basicly creates the group and thats it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;


private void CreateMainGroups(string siteurl, string SiteName)
        {
          //method creates a defualt visitor, contributor and full control group for a site.
            // Add a new custom group to the site 
        
            ClientContext context = new ClientContext(siteurl);
        
            Web web = context.Web;
            //See if the groups exist
            context.Load(web.SiteGroups);
            context.ExecuteQuery();
            Group Admin = (from g in web.SiteGroups where g.Title == SiteName + " Owners" select g).SingleOrDefault();
            Group Visitor = (from g in web.SiteGroups where g.Title == SiteName + " Visitors" select g).SingleOrDefault();
            Group Members = (from g in web.SiteGroups where g.Title == SiteName + " Members" select g).SingleOrDefault();

                      
            
            
            if (Admin == null)
            {
                //Create the Admin Group if it dont exist
                GroupCreationInformation groupCreationInfoAdmin = new GroupCreationInformation();
                groupCreationInfoAdmin.Title = SiteName + " Owners";
                groupCreationInfoAdmin.Description = " Use this group to give people full control permissions to the SharePoint site: " + SiteName;
                Admin = web.SiteGroups.Add(groupCreationInfoAdmin);
               
            }
           
            if(Members == null)
            {
            //Create the Contributer Group  if it dont exist
            GroupCreationInformation groupCreationInfoContribut = new GroupCreationInformation();
            groupCreationInfoContribut.Title = SiteName + " Members";
            groupCreationInfoContribut.Description = "Use this group to give people contribute permissions to the SharePoint site: "+ SiteName;
            Members = web.SiteGroups.Add(groupCreationInfoContribut);
           
            }

            if(Visitor == null)
            {
            //Create the Visitor Group  if it dont exist
            GroupCreationInformation groupCreationInfoVisitors = new GroupCreationInformation();
            groupCreationInfoVisitors.Title = SiteName + " Visitors";
            groupCreationInfoVisitors.Description = "Use this group to grant people read permissions to the SharePoint site: " + SiteName;
            Visitor = web.SiteGroups.Add(groupCreationInfoVisitors);
            
            }
           
           
           
            //Assign site roles This is the problem area it's supposed to assign the group to the associated group on the web but it never works. 

            web.AssociatedOwnerGroup = Admin;
            web.AssociatedOwnerGroup.Update();
            Admin.Update();

            web.AssociatedVisitorGroup = Visitor;
            web.AssociatedVisitorGroup.Update();
            Visitor.Update();

            web.AssociatedMemberGroup = Members;
            web.AssociatedMemberGroup.Update();
            Members.Update();

            web.Update();
            
           
            
            context.ExecuteQuery();
          





        }


Am I missing something here? Any help would be appreciated.
Posted
Updated 18-Jun-14 3:46am
v3

1 solution

I was able to come up with a solutionf ro this issue doing the following code this basicly replaced my problem code section

RoleDefinition rda = web.RoleDefinitions.GetByName("Full Control");
              RoleDefinition rdm = web.RoleDefinitions.GetByName("Contribute");
              RoleDefinition rdv = web.RoleDefinitions.GetByName("Read");



              RoleDefinitionBindingCollection bindA = new RoleDefinitionBindingCollection(context);
              RoleDefinitionBindingCollection bindM = new RoleDefinitionBindingCollection(context);
              RoleDefinitionBindingCollection bindV = new RoleDefinitionBindingCollection(context);
              bindA.Add(rda);
              bindM.Add(rdm);
              bindV.Add(rdv);

              web.RoleAssignments.Add(Admin, bindA);
              web.RoleAssignments.Add(Members, bindM);
              web.RoleAssignments.Add(Visitor, bindV);


If anyone has a better solution let me know this is the only way I could get it to work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900