{"id":435,"date":"2015-12-16T13:53:16","date_gmt":"2015-12-16T18:53:16","guid":{"rendered":"http:\/\/markwheeler.com\/wordpress\/?p=435"},"modified":"2015-12-16T13:53:16","modified_gmt":"2015-12-16T18:53:16","slug":"managing-sharepoint-user-group-memberships-with-jquery-and-spservices","status":"publish","type":"post","link":"https:\/\/markwheeler.com\/?p=435","title":{"rendered":"Managing SharePoint User Group Memberships with JQuery and SPServices"},"content":{"rendered":"<p>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 \u2018move\u2019 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.  <\/p>\n<pre>\n<script src=\"\/jquery1.11.js\" type=\"text\/javascript\" ><\/script>\n<script type=\"text\/javascript\" src=\"\/jquery.SPServices.js\"><\/script>\n\n<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\">\n<tbody>\n<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>\n\n<script type=\"text\/javascript\">\n\n$(document).ready(function() {\n\/\/Populate the users pick list\n  var strHTMLSiteUsers = \"\";\n  $().SPServices({\n      operation: \"GetUserCollectionFromSite\",\n      async: false,\n      completefunc: function(xData, Status) {\n        $(xData.responseXML).find(\"User\").each(function() {\n        strHTMLSiteUsers += \"<option value='\" + $(this).attr(\"LoginName\") + \"'>\" + $(this).attr(\"Name\") + \"<\/option>\";\n        });\n        $(\"#my_SiteUsers\").append(strHTMLSiteUsers);\n  \t\t\/\/\t$(\"#xmlOutput\").append(\"<textarea rows='15' cols='100'>\" + xData.responseXML.xml + \"<\/textarea>\");\n\t\t\/\/\t$(\"#groupOutput\").append(\"<textarea rows='15' cols='100'>\" + strHTMLSiteUsers  + \"<\/textarea>\");\n      }\n});\n  RefreshGroupLists();\n});\n\nfunction RefreshGroupLists(){\n  var strHTMLAvailable = \"\";\n  var strHTMLAssigned = \"\";\n  var arrOptionsAssigned = new Array();\n  var intOpts = 0;\n  var booMatch;\n  var booErr = \"false\";\n  var SelectedUser = $('#my_SiteUsers').val();\n \/\/ alert(SelectUser);\n\n\n  $(\"#my_SPGroupsAssigned\").html(\"\");\n  $(\"#my_SPGroupsAvailable\").html(\"\");\n\n  if($(\"#my_SiteUsers\").val() == 0){\n    alert(\"You must select a user\");\n    return;\n  }\n\n  \/\/Populate the Groups Assigned\n\n  $().SPServices({\n      operation: \"GetGroupCollectionFromUser\",\n      userLoginName: $('#my_SiteUsers').val(),\n      async: false,\n      completefunc: function(xData, Status) {\n        $(xData.responseXML).find(\"errorstring\").each(function() {\n          alert(\"User not found\");\n          booErr = \"true\";\n          return;\n        });\n      \n        $(xData.responseXML).find(\"Group\").each(function() {\n\n\n          strHTMLAvailable += \"<option value='\" + $(this).attr(\"Name\") + \"'>\" + $(this).attr(\"Name\") + \"<\/option>\";\n          arrOptionsAssigned[intOpts] = $(this).attr(\"Name\");\n          intOpts = intOpts + 1;\n        });\n        $(\"#my_SPGroupsAssigned\").append(strHTMLAvailable);\n      }\n  });\n\n  \/\/Populate available site groups\n  if(booErr == \"false\"){\n    $().SPServices({\n        operation: \"GetGroupCollectionFromSite\",\n        async: false,\n        completefunc: function(xData, Status) {\n          $(xData.responseXML).find(\"Group\").each(function() {\n            booMatch = \"false\";\n            for (var i=0;i<=arrOptionsAssigned.length;i++){\n              if($(this).attr(\"Name\") == arrOptionsAssigned[i]){\n                booMatch = \"true\";\n                break;\n              }\n            }\n            if(booMatch == \"false\"){\n              strHTMLAssigned += \"<option value='\" + $(this).attr(\"Name\") + \"'>\" + $(this).attr(\"Name\") + \"<\/option>\";\n            }\n          });\n          $(\"#my_SPGroupsAvailable\").append(strHTMLAssigned);\n        }\n    });\n  }\n}\n\nfunction AddGroupsToUser(){\n  var i;\n\n  if($(\"#my_SiteUsers\").val() == 0){\n    alert(\"You must select a user\");\n    return;\n  }\n\n  if($(\"#my_SPGroupsAvailable\").val() == null){\n    alert(\"You haven't selected any groups to add\");\n    return;\n  }\n  else{\n    var arrGroups = $(\"#my_SPGroupsAvailable\").val();\n      var SelectedUser = $('#my_SiteUsers').val();\n     alert(SelectedUser);\n      alert(arrGroups);\n    for (i=0;i<arrGroups.length;i++){\n      $().SPServices({\n          operation: \"AddUserToGroup\",\n          groupName: arrGroups[i],\n          userLoginName: $('#my_SiteUsers').val(),\n          async: false,\n          completefunc: null\n      });\n    }\n    RefreshGroupLists();\n  }\n}\n\nfunction RemoveGroupsFromUser(){\n  var i\n\n  if($(\"#my_SiteUsers\").val() == 0){\n    alert(\"You must select a user\");\n    return;\n  }\n\n  if($(\"#my_SPGroupsAssigned\").val() == null){\n    alert(\"You haven't selected any groups to remove\");\n    return;\n  }\n  else{\n    var arrGroups = $(\"#my_SPGroupsAssigned\").val();\n    var SelectedUser = $('#my_SiteUsers').val();\n    \n    for (i=0;i<arrGroups.length;i++){\n      $().SPServices({\n          operation: \"RemoveUserFromGroup\",\n          groupName: arrGroups[i],\n          userLoginName: $('#my_SiteUsers').val(),\n          async: false,\n          completefunc: null\n      });\n    }\n    RefreshGroupLists();\n  }\n}<\/script>\n\n<table align=\"center\">\n<tbody>\n<tr>\n<td colspan=\"3\" style=\"text-align: center\">\n<font style=\"font-weight: bold\">Select a User:&#160;&#160;&#160;<\/font> <select id=\"my_SiteUsers\" onchange=\"RefreshGroupLists()\" style=\"width: 350px\"><\/select>\n<\/td><\/tr>\n<tr><th class=\"ms-vh2\">Available Groups<\/th>\n<th><\/th>\n<th class=\"ms-vh2\">Assigned Groups<\/th><\/tr>\n<tr><td class=\"ms-vb2\">\n<select id=\"my_SPGroupsAvailable\" multiple=\"multiple\" style=\"height: 450px; width: 250px\"><\/select> \n<\/td>\n<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>\n<\/td>\n<td class=\"ms-vb2\"><select id=\"my_SPGroupsAssigned\" multiple=\"multiple\" style=\"height: 450px; width: 250px\"><\/select> \n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!--- For Debugging Purposes --->\n<div id=\"xmlOutput\"><\/div>\n<div id=\"groupOutput\"><\/div>\n<\/pre>\n<p><a href=\"http:\/\/54.159.73.21\/wp-content\/uploads\/2015\/12\/image_thumb.png\" rel=\"attachment wp-att-436\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"404\" src=\"http:\/\/54.159.73.21\/wp-content\/uploads\/2015\/12\/image_thumb.png\" alt=\"image_thumb\" class=\"alignnone size-medium wp-image-436\" srcset=\"https:\/\/markwheeler.com\/wp-content\/uploads\/2015\/12\/image_thumb.png 620w, https:\/\/markwheeler.com\/wp-content\/uploads\/2015\/12\/image_thumb-300x195.png 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2,10,11],"tags":[62,99,104],"class_list":["post-435","post","type-post","status-publish","format-standard","hentry","category-computer","category-sharepoint","category-software","tag-jquery","tag-sharepoint-2010","tag-spservices"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":0,"href":"https:\/\/markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"wp:attachment":[{"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}