Managing SharePoint User Group Memberships with JQuery and SPServices

 Computer, Sharepoint, Software  Comments Off on Managing SharePoint User Group Memberships with JQuery and SPServices
Dec 162015
 
Share

Create a simple interface to add/remove a single user to/from multiple groups at once. When you identify/select a user, it will show you two select lists. The right side will list all of the groups the current user is a member of while the left box shows all the groups the user is NOT a member of. Between the two select lists are buttons to ‘move’ the groups from one side to the other for the identified user (while modifying their memberships appropriately in the process). The select boxes are configured to allow multiple select so you can usually perform the maintenance in just a couple clicks.

<script src="jquery1.11.js" type="text/javascript" ></script>
<script type="text/javascript" src="jquery.SPServices.js"></script>

<table width="100%" class="ms-rteTable-default ms-rteThemeBackColor-5-5 ms-rteThemeForeColor-1-0 ms-rteFontSize-2" cellspacing="0" _moz_resizing="true" style="font-size: 1em">
<tbody>
<tr class="ms-rteTableHeaderRow-default ms-rteThemeBackColor-5-5 ms-rteThemeForeColor-1-0 ms-rteFontSize-2"><th class="ms-rteTableHeaderFirstCol-default ms-rteThemeBackColor-5-5 ms-rteThemeForeColor-1-0 ms-rteFontSize-2" rowspan="1" colspan="1" style="text-align: left"><span>User Group Membership</span></th></tr></tbody></table>

<script type="text/javascript">

$(document).ready(function() {
//Populate the users pick list
  var strHTMLSiteUsers = "";
  $().SPServices({
      operation: "GetUserCollectionFromSite",
      async: false,
      completefunc: function(xData, Status) {
        $(xData.responseXML).find("User").each(function() {
        strHTMLSiteUsers += "<option value='" + $(this).attr("LoginName") + "'>" + $(this).attr("Name") + "</option>";
        });
        $("#my_SiteUsers").append(strHTMLSiteUsers);
  		//	$("#xmlOutput").append("<textarea rows='15' cols='100'>" + xData.responseXML.xml + "</textarea>");
		//	$("#groupOutput").append("<textarea rows='15' cols='100'>" + strHTMLSiteUsers  + "</textarea>");
      }
});
  RefreshGroupLists();
});

function RefreshGroupLists(){
  var strHTMLAvailable = "";
  var strHTMLAssigned = "";
  var arrOptionsAssigned = new Array();
  var intOpts = 0;
  var booMatch;
  var booErr = "false";
  var SelectedUser = $('#my_SiteUsers').val();
 // alert(SelectUser);


  $("#my_SPGroupsAssigned").html("");
  $("#my_SPGroupsAvailable").html("");

  if($("#my_SiteUsers").val() == 0){
    alert("You must select a user");
    return;
  }

  //Populate the Groups Assigned

  $().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $('#my_SiteUsers').val(),
      async: false,
      completefunc: function(xData, Status) {
        $(xData.responseXML).find("errorstring").each(function() {
          alert("User not found");
          booErr = "true";
          return;
        });
      
        $(xData.responseXML).find("Group").each(function() {


          strHTMLAvailable += "<option value='" + $(this).attr("Name") + "'>" + $(this).attr("Name") + "</option>";
          arrOptionsAssigned[intOpts] = $(this).attr("Name");
          intOpts = intOpts + 1;
        });
        $("#my_SPGroupsAssigned").append(strHTMLAvailable);
      }
  });

  //Populate available site groups
  if(booErr == "false"){
    $().SPServices({
        operation: "GetGroupCollectionFromSite",
        async: false,
        completefunc: function(xData, Status) {
          $(xData.responseXML).find("Group").each(function() {
            booMatch = "false";
            for (var i=0;i<=arrOptionsAssigned.length;i++){
              if($(this).attr("Name") == arrOptionsAssigned[i]){
                booMatch = "true";
                break;
              }
            }
            if(booMatch == "false"){
              strHTMLAssigned += "<option value='" + $(this).attr("Name") + "'>" + $(this).attr("Name") + "</option>";
            }
          });
          $("#my_SPGroupsAvailable").append(strHTMLAssigned);
        }
    });
  }
}

function AddGroupsToUser(){
  var i;

  if($("#my_SiteUsers").val() == 0){
    alert("You must select a user");
    return;
  }

  if($("#my_SPGroupsAvailable").val() == null){
    alert("You haven't selected any groups to add");
    return;
  }
  else{
    var arrGroups = $("#my_SPGroupsAvailable").val();
      var SelectedUser = $('#my_SiteUsers').val();
     alert(SelectedUser);
      alert(arrGroups);
    for (i=0;i<arrGroups.length;i++){
      $().SPServices({
          operation: "AddUserToGroup",
          groupName: arrGroups[i],
          userLoginName: $('#my_SiteUsers').val(),
          async: false,
          completefunc: null
      });
    }
    RefreshGroupLists();
  }
}

function RemoveGroupsFromUser(){
  var i

  if($("#my_SiteUsers").val() == 0){
    alert("You must select a user");
    return;
  }

  if($("#my_SPGroupsAssigned").val() == null){
    alert("You haven't selected any groups to remove");
    return;
  }
  else{
    var arrGroups = $("#my_SPGroupsAssigned").val();
    var SelectedUser = $('#my_SiteUsers').val();
    
    for (i=0;i<arrGroups.length;i++){
      $().SPServices({
          operation: "RemoveUserFromGroup",
          groupName: arrGroups[i],
          userLoginName: $('#my_SiteUsers').val(),
          async: false,
          completefunc: null
      });
    }
    RefreshGroupLists();
  }
}</script>

<table align="center">
<tbody>
<tr>
<td colspan="3" style="text-align: center">
<font style="font-weight: bold">Select a User:&#160;&#160;&#160;</font> <select id="my_SiteUsers" onchange="RefreshGroupLists()" style="width: 350px"></select>
</td></tr>
<tr><th class="ms-vh2">Available Groups</th>
<th></th>
<th class="ms-vh2">Assigned Groups</th></tr>
<tr><td class="ms-vb2">
<select id="my_SPGroupsAvailable" multiple="multiple" style="height: 450px; width: 250px"></select> 
</td>
<td><button id="my_AddGroupsToUser" onclick="AddGroupsToUser()" type="submit" style="width: 80px">&gt;&gt;</button><br/><br/><button id="my_RemoveGroupsFromUser" onclick="RemoveGroupsFromUser()" type="submit" style="width: 80px">&lt;&lt;</button>
</td>
<td class="ms-vb2"><select id="my_SPGroupsAssigned" multiple="multiple" style="height: 450px; width: 250px"></select> 
</td>
</tr>
</tbody>
</table>
<!--- For Debugging Purposes --->
<div id="xmlOutput"></div>
<div id="groupOutput"></div>

image_thumb

Query for SharePoint Groups and Members with JQuery and SPServices

 Computer, Sharepoint, Software  Comments Off on Query for SharePoint Groups and Members with JQuery and SPServices
Dec 152015
 
Share

The following code returns all SharePoint permissions groups and the respective members.
This uses JQuery 1.xx and SPServices.


// Add JQuery and SPServices references. //
<script type="text/javascript"  src="jquery1.11.js" ></script>
<script type="text/javascript" src="jquery.SPServices.js"></script>

<script>
// alert("Debug Check 1");
$(document).ready(function() {
// Function returns all Permissions Groups
$().SPServices({
                        operation: "GetGroupCollectionFromSite",
                        async: false,
                        completefunc: function (xData, Status) {
                                        $(xData.responseXML).find("Group").each(function() {
                                        // alert($(this).attr("Name"));
                                        getAllUsersFromGroup($(this).attr("Name"));
                                                                                           });
                                                               }
                });
 });
                             
                                                                       
// Function returns all users in respective Groups                                           
function getAllUsersFromGroup(groupName) {
// alert(groupName);
$().SPServices({
                       operation: "GetUserCollectionFromGroup",
                       groupName: groupName,
                       async: false,
                       completefunc: function (xDataUser, Status) {
                                       $(xDataUser.responseXML).find("User").each(function() {
                                       $('#GroupUsers').append("<tr><td>" + groupName + "</td><td>" + $(this).attr("Name") + "</td></tr>");
                                                                                              });
                                                                   }
                });
}
</script>

<!-- Table for Output -->
<table align="LEFT" id="GroupUsers">
<tbody>
<tr>
<tr><td>Group</td><td>Member</td></tr>
</tbody>
</table>