Create a SharePoint group and add users using JSOm

Below code will create a sharegroup and adds login user and a static user to the group.

   <div>  
     <h1>Create SharePoint Group </h1>  
     <br />  
     <input id="btnCreateCustomGroup" type="button" value="Create SPGroup" />  
   </div>  
   <script src="/Style%20Library/Scripts/jquery-1.12.1.js"></script>   
   <script src="/_layouts/15/sp.js" type="text/javascript"></script>  
   <script src="/_layouts/15/SP.RequestExecutor.js" type="text/javascript"></script>  
   <script src="/_layouts/15/SP.search.js" type="text/javascript"></script>  
   <script type="text/javascript">  
   $(function() {  
     $('#btnCreateCustomGroup').click(btnCreateCustomGroup_Click);  
   });  
   function btnCreateCustomGroup_Click() {  
     var grpName = "DSAuditors1"  
     var grpDesc = "Auditors custom group with edit access";  
     var grpRole = "PEO Requestor";  
         var userVal = ("i:0#.w|corp\\p40000358;").split(".w|")[1];  
         var userName = userVal.slice(0,-1);  
     var appweburl = _spPageContextInfo.siteAbsoluteUrl;  
     var clientContext = new SP.ClientContext(appweburl);  
     CreateCustomGroup(clientContext, grpName, grpDesc, grpRole, userName, true, false, true,  
       function success() {  
         alert("Successfully created group: " + grpName);  
       },  
       function fail(src, info) {  
         alert("Failed to create group: " + grpName + ". " + info.get_message());  
       });  
   }  
   function CreateCustomGroup(context, name, desc, roleName, userName, canMembersEdit, visibleToMembersOnly, addCurrentUser, success, fail) {  
     var web = context.get_web();          
     //Get all groups in site  
     var groupCollection = web.get_siteGroups();  
     var spoc = web.ensureUser(userName);  
     var currentUser = web.get_currentUser();  
     context.load(currentUser);  
     context.load(groupCollection, 'Include(Id, Title)');      
     context.load(web, 'Title', 'HasUniqueRoleAssignments');  
     context.executeQueryAsync(function() {  
       var grpEnu = groupCollection.getEnumerator();  
       var i = 0;  
      var isGroupfound = false;  
       while(grpEnu.moveNext)  
       {  
         if ((groupCollection.itemAt(i) != null || groupCollection.itemAt(i) != undefined) ) {  
          if(groupCollection.itemAt(i).get_title() == name ){  
               alert(groupCollection.itemAt(i).get_title());  
               isGroupfound = true;   
           break;  
          }  
         }   
       else{  
           break;  
         }           
          i++;  
       }  
       if(!isGroupfound)  
       {        
            // Create Group information for Group  
                       var newGRP = new SP.GroupCreationInformation();  
                 newGRP.set_title(name);  
                         newGRP.set_description(desc);  
                       //add group to site gorup collection  
             var newCreateGroup = groupCollection.add(newGRP);  
             //Role Definition  
             var rolDef = web.get_roleDefinitions().getByName(roleName);  
             var rolDefColl = SP.RoleDefinitionBindingCollection.newObject(context);  
             rolDefColl.add(rolDef);  
             // Get the RoleAssignmentCollection for the target web.  
             var roleAssignments = web.get_roleAssignments();  
             // assign the group to the new RoleDefinitionBindingCollection.  
             roleAssignments.add(newCreateGroup, rolDefColl);  
             //Set group properties  
             newCreateGroup.set_allowMembersEditMembership(canMembersEdit);  
             newCreateGroup.set_onlyAllowMembersViewMembership(visibleToMembersOnly);  
             //add currect user to group  
             if (addCurrentUser) {  
               newCreateGroup.get_users().addUser(currentUser);  
             }  
                       newCreateGroup.get_users().addUser(spoc);  
             newCreateGroup.update();  
             context.load(newCreateGroup);        
             //Execute Query  
             context.executeQueryAsync(function() {  
                alert(newCreateGroup.get_id());  
          }, fail);  
       }else{  
         var visitorsGroup = groupCollection.getByName(name);  
             context.load(visitorsGroup);  
         context.executeQueryAsync(function() {  
          //add currect user to group  
          if (addCurrentUser) {  
           visitorsGroup.get_users().addUser(currentUser);  
          }                       
          visitorsGroup.get_users().addUser(spoc);  
          visitorsGroup.update();  
          context.load(visitorsGroup);  
          //Execute Query  
          context.executeQueryAsync(function() {  
           alert(visitorsGroup.get_id());  
          }, fail);              
        }, fail);  
       }  
     }, fail);  
   }  
   </script>  

Comments

Popular posts from this blog

Email Validation for SharePoint list column

SP, UDF and VIEW