Monday, July 20, 2015

SharePoint Online - Create a SharePoint Group dynamically based on the permission type to all SharePoint Sites

Scenario - Suppose you have a SharePoint site collection and you would like to add a new group to all sites and sub-sites (Unique Permissions). 

The following code creates a group to all sites based on the permission type and name it with "SITENAME_PERMISSION". You can change it to any parameter.


using (ClientContext clientContextSub = new ClientContext(strSiteUrl))
            {
                SecureString passWord = new SecureString();
                foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
     clientContextSub.Credentials = new SharePointOnlineCredentials(userName, passWord);
                clientContextSub.ExecuteQuery();
                try
                {

                 Web webSubWeb = clientContextSub.Web;
                 GroupCollection groupCollection = webSubWeb.SiteGroups;
                 clientContextSub.Load(webSubWeb.RoleAssignments);
                 clientContextSub.Load(
                 webSubWeb,
                   web2SubWebSubNode => web2SubWebSubNode.Title,
                   web2SubWebSubNode => web2SubWebSubNode.Url,
                   web2SubWebSubNode => web2SubWebSubNode.LastItemModifiedDate,
                   web2SubWebSubNode => web2SubWebSubNode.HasUniqueRoleAssignments);
                   clientContextSub.ExecuteQuery();
                  
                    if (webSubWeb.HasUniqueRoleAssignments)
                    {
                        bool _permissionGroupAvailable = false;
                        string _groupNameToAssign = string.Empty;
                        string groupOwener = string.Empty;
                        foreach (RoleAssignment assignment in webSubWeb.RoleAssignments)
                        {
                            try
                            {
                                string Role = string.Empty;
                                clientContextSub.Load(assignment.Member);
                                clientContextSub.Load(assignment.RoleDefinitionBindings);
                                clientContextSub.ExecuteQuery();

                    foreach (RoleDefinition roleDef in assignment.RoleDefinitionBindings)
                                {
//CMBAddGroup is selected permission type from a dropdown (Ex- Full Control, Read, Contribute)
       if (roleDef.Name.ToString().Trim() == cmbAddGroup.SelectedItem.ToString().Trim())
                                    {
                                        _permissionGroupAvailable = true;
                                        break;
                                    }

                                }
                                if (_permissionGroupAvailable)
                                { break; }

                            }

                            catch (Exception ex)
                            {
                               continue;
                            }
                        }
                        if (!(_permissionGroupAvailable))
                        {
                           
                           {
_groupNameToAssign = webSubWeb.Title + "_" + cmbAddGroup.SelectedItem.ToString();
GroupCreationInformation newGroupInfo = new GroupCreationInformation();
newGroupInfo.Title=_groupNameToAssign.ToString();
Group groupExist = webSubWeb.SiteGroups.GetByName(_groupNameToAssign);
                            if (groupExist == null)
                            {
Group oGroup = webSubWeb.SiteGroups.Add(newGroupInfo);
RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContextSub);
RoleDefinition oRoleDefinition = webSubWeb.RoleDefinitions.GetByName(cmbAddGroup.SelectedItem.ToString());
collRoleDefinitionBinding.Add(oRoleDefinition);
webSubWeb.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
clientContextSub.Load(oGroup, group => group.Title);
clientContextSub.Load(oRoleDefinition,role => role.Name);
clientContextSub.ExecuteQuery();
                            }
                            else
                            {
RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContextSub);
RoleDefinition oRoleDefinition = webSubWeb.RoleDefinitions.GetByName(cmbAddGroup.SelectedItem.ToString());
collRoleDefinitionBinding.Add(oRoleDefinition);
webSubWeb.RoleAssignments.Add(groupExist, collRoleDefinitionBinding);
clientContextSub.Load(groupExist, group => group.Title);
clientContextSub.Load(oRoleDefinition,role => role.Name);
clientContextSub.ExecuteQuery();

                            }
                            }
                            }

                       
                        }
                    }

            

                catch (Exception ex)
                {
                    helpClass.ErrorLog("", "Error MESSAGE: " + ex.Message);

                }
            }

        }