Change the SharePoint User Group Name Using JSOM

Steps for Implementation:

  • Get Current Context.
  • Get App web URL and Host Web URL from Query string parameter.
  • Calling check method in document ready.
  • Get web from app context site.
  • Get all site group from host web site.
  • Get Group by group name
  • Set the New Title
  • Finally title will be updated with new name on success function

In your JavaScript file write below code,

  1. // Js code Starts here   
  2. 'use strict';  
  3. //Get Current Context   
  4. var context = SP.ClientContext.get_current();  
  5. //Declaring the variables   
  6. var hostWebURL, appWebURL;  
  7. // this code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model   
  8. $(document).ready(function()  
  9. {  
  10.     // Get App web URL and Host Web URL from Query string parameter   
  11.     hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  12.     appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
  13.     //Calling check method in document ready   
  14.     ChangeGroupName();  
  15. });  
  16. //Change the group name   
  17. function ChangeGroupName()  
  18. {  
  19.     var cWeb = appCtx.get_web();  
  20.     var collGroup = cWeb.get_siteGroups();  
  21.     var oGroup = collGroup.getByName("HFS Managers");  
  22.     oGroup.set_title("Test");  
  23.     oGroup.update();  
  24.     context.load(oGroup);  
  25.     context.executeQueryAsync(function()  
  26.     {  
  27.         alert("success");  
  28.     }, function(sender, args)  
  29.     {  
  30.         alert("Request failed to change the group name " + args.get_message());  
  31.     });  
  32. }  
  33. //method for read query string value
    function manageQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == paramToRetrieve) {
    return singleParam[1];
    }
    }
    }

Summary

In this blog we have explored how to rename the SharePoint user group using JavaScript Object Model.

 

Ebook Download
View all
Learn
View all