3

I am new to doing things in C# coming from PowerShell so I may be missing the obvious, I want to check if a SharePoint Permission Group exists in the Web and if not I want to create it and assign it to a parameter I will use later in my script.

However, I cant find an easy check method that returns a bool so I just use the GetByName() method and check if its null, then once its created I assign it to the same parameter I use later adminGroup but VS tells me A local or parameter named cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

Is there a better way to check for an existing group and how should I handle assigning the adminGroup parameter? Any help appreciated. Thanks.

using (var context = new ClientContext(webUrl))
        {
            context.Credentials = new SharePointOnlineCredentials(userName, password);
            var web = context.Web;
            // Load required data
            context.Load(web, w => w.SiteGroups.Include(sg => sg.Title), w => w.RoleDefinitions.Include(rd => rd.Name), w => w.Lists);
            context.ExecuteQuery();

            // Create Base Libraries
            CreateBaseLibraries();

            // Create Admin Group
            // CHECK IF EXISTS AND ASSIGN PARAMETER OR CREATE?
            var adminGroup = web.SiteGroups.GetByName("Employee File Administrators2");
            context.Load(adminGroup);
            context.ExecuteQuery();
            if (adminGroup == null)
            {
                CreateAdminPermissionGroup(context, web);
                var adminGroup = web.SiteGroups.GetByName("Employee File Administrators2");
            }

            // Create Permission Groups
            var permissionItems = GetListItems(context, web, "Admin_Teams");
            foreach (ListItem item in permissionItems)
            {
                var groupName = item.FieldValues["Title"].ToString();
                CreatePermissionGroup(groupName, adminGroup, "This group is used to administer permissions in the Employee File System");
            }

            // Create Folders

            // Create Content Organiser Rules

        }
    }

****EDIT**** Using Muskan's answer I have re-written the section of code below

                // Create Admin Group
            Group adminGroup = null;
            try
            {
                adminGroup = web.SiteGroups.GetByName("Employee File Administrators2");
                context.Load(adminGroup);
                context.ExecuteQuery();
            }
            catch
            {
                Console.WriteLine("Admin group not found, creating...");
                adminGroup = null;
            }

            if (adminGroup == null)
            {
                CreateAdminPermissionGroup(context, web);
                adminGroup = web.SiteGroups.GetByName("Employee File Administrators2");
                context.Load(adminGroup);
                context.ExecuteQuery();
            }
  • regarding the error message you got from VS: 3 lines above the null check for 'adminGroup' you define the variable but 3 lines below the same if condition you try to declare it again. removing the 'var' before the second declaration should fix that. – garglblarg Aug 16 '16 at 15:33
  • Thanks garglblarg, I changed that in the fix I added too – Col.Cook Aug 17 '16 at 2:34
1
SPWeb root = site.RootWeb;
SPGroup group = null;

// Check if the group exists
try
{                                     
    group = root.SiteGroups["The Group Name"];
}
catch { }

// If it doesn't, add it
if (group == null)
{
    root.SiteGroups.Add("The Group Name", member, root.Author, "Your Group Description");
    group = root.SiteGroups["The Group Name"];

    // Add the group's permissions
    SPRoleDefinition roleDefinition = root.RoleDefinitions.GetByType(SPRoleType.Contributor);
    SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
    root.RoleAssignments.Add(roleAssignment);
    root.Update();
}
  • Thanks @Muskan using your answer I wrote the code below my question,, as this is for SharePoint online its slightly different, maybe there is a better way but it works. – Col.Cook Aug 17 '16 at 2:20
  • Glad that helped you ! – Muskan Aug 17 '16 at 4:29

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.