The following code has been tested in dev, test environments and it works perfect.

However when I run it on qa environment I get the following exception:


/// <summary>
        /// Creates the sharepoint group Members MA, for Manager Assistants for job and opportunitysites.
        /// </summary>
        /// <param name="weburl">Web site url</param>
        /// <param name="groupPrefix">Group prefix</param>
        public static void CreateMembersMAGroupForSubsitesIfNotExists(string weburl, string groupPrefix)
            LoggingService.LogInfo(LoggingCategory.Security, String.Format("Starting  CreateMembersMAGroupForSubsitesIfNotExists: {0}", weburl));

                        using (var site = new SPSite(weburl))
                            using (var web = site.OpenWeb())
                                LoggingService.LogInfo(LoggingCategory.Security,String.Format("Creating member MA group for web: {0}", web.Name));
                                //Create custom members ma group if it doesnt exist
                                if (!SecurityHelper.GroupExists(web.SiteGroups, String.Concat(groupPrefix, " ",Constants.ContentTypes.System.SecurityMapping.MASPGroup)))
                                    web.AllowUnsafeUpdates = true;
                                    web.SiteGroups.Add(String.Concat(groupPrefix, " ",Constants.ContentTypes.System.SecurityMapping.MASPGroup),web.SiteAdministrators[0], null, String.Empty);
                                    var maGroupCreated =web.SiteGroups[String.Concat(groupPrefix, " ",Constants.ContentTypes.System.SecurityMapping.MASPGroup)];
                                    var roleAssignment = new SPRoleAssignment(maGroupCreated);
                                    var roleDefBindings = roleAssignment.RoleDefinitionBindings;
                                    var roleDefinition = web.RoleDefinitions["Contribute Share"];
                                    web.AllowUnsafeUpdates = false;
                                    LoggingService.LogInfo(LoggingCategory.Security,String.Format("Created member MA group for web: {0}", web.Name));
                                    LoggingService.LogInfo(LoggingCategory.Security, String.Format("Group default member ma already existed: {0},{1}", web.Name, groupPrefix));
            catch (Exception ex)
                LoggingService.LogError(LoggingCategory.Security, ex, String.Format("Creating members MA group for web failed: {0}", weburl));

            LoggingService.LogInfo(LoggingCategory.Security, String.Format("Finished  CreateMembersMAGroupForSubsitesIfNotExists: {0}", weburl));


 /// <summary>
        /// Group exists by name
        /// </summary>
        /// <param name="groups"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool GroupExists(SPGroupCollection groups, string name)
            if (String.IsNullOrEmpty(name) ||
                (name.Length > 255) ||
                (groups == null) ||
                (groups.Count == 0))
                return false;
            return (groups.GetCollection(new String[] { name }).Count > 0);

(Specified argument was out of the range of valid values.) System.ArgumentOutOfRangeException at Microsoft.SharePoint.SPUserCollection.get_Item(Int32 index) at xx.SP.DMS.Common.Helpers.SecurityManager.<>c_DisplayClass23.b_21() at Microsoft.SharePoint.SPSecurity.<>c_DisplayClass5.b_3() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at xx.xx.DMS.Common.Helpers.SecurityManager.CreateCustomVisitorsGroupForSubsitesIfNotExists(String weburl, String groupPrefix)

The only thing I can think of is web.SiteAdministrator[0],? or any other reason?


For some reason the QA environment, the site collection administrator was empty, :(, dont know why, but then it works. Lesson learned

  • Hi! Can you please add more details to this post? How did you check it was empty, and so on. Makes it more helpful to future readers – Robert Lindgren Mar 25 '14 at 9:13
  • Site ssettings -> Site collection administrators, I dont know why,but it was empty, and that was the cause the code was not working. after I added a user there, then it works again – Luis Valencia Apr 10 '14 at 12:03

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.