SharePoint 2013 hat viele Stärken, aber auch einige Schwächen. Eine dieser Schwächen ist der Umstand, dass man Benutzergruppen nur sehr unkomfortabel behandeln kann. Z.B. das Durchsuchen einer Benutzergruppe nach einem bestimmten User ist schlichtweg nicht möglich. Ebenso kann man nicht nach Gruppen selbst suchen. Es wird jeweils nur eine Liste mit Resultaten angezeigt, in der man dann mittels eines Vor- und Zurück-Pfeils blättern kann, bis man an die gewünschte Stelle kommt.
Das folgende Script, bestehend aus HTML und jQuery eröffnet die Möglichkeit, auf einer Page in einer Selektion aller User Groups der aktuellen Site die gewünschte schnell zu finden und zu laden und dann in der Liste der User nach bestimmten Usern zu suchen. Weiterhin hat man die Möglichkeit, einzelne oder mehrere User zu markieren und diese aus der Benutzergruppe zu entfernen.
Den folgenden Code am Besten als .html-Datei speichern, in der Assets-Library einer Site hochladen und über ein Content Editor WebPart in einer SharePoint Page einbinden.
<script> window.jQuery || document.write('<script src="//code.jquery.com/jquery-1.11.2.min.js">\x3C/script>'); </script> <p>Site groups considered only</p> <button id="loadGroups" type="button">load site groups</button> <div id="groupSelectArea" style="display:none"> <select id="groupsSelect"></select> <button id="takeOverGroupName" type="button">Load selected group</button> </div> <div id="groupInputArea" style="display:none"> <input type="text" id="groupName" placeholder="enter group name" style="width:30em"></input> <button id="checkGroup" type="button">Load group by entry</button> </div> <div id="groupSearchMemberArea" style="display:none"> <input type="text" id="searchTerm" placeholder="user" style="width:30em" value=""></input> <button id="doSearch" type="button">Search in selected group</button> <div id="groupSearchMemberResult"></div> </div> <div id="removeUserFromGroupArea" style="display:none;"> <button id="removeUsersFromGroup" type="button">remove selected users from group</button> <div id="removeUserFromGroupResult" style="display:none; border:1px solid red;"></div> </div> <div id="groupMembersResultArea" style="display:none; padding:10px 0"> <div id="jQueryResult" style="border:1px solid grey; padding:5px;">results</div> </div> <script> (function($j) { $j(document).ready(function (e) { //ExecuteOrDelayUntilScriptLoaded(showSiteGroups(), "sp.js"); }); $j( "#loadGroups" ).click(function() { $j("#groupSelectArea").css("display","block"); $j("#groupInputArea").css("display","block"); $j("#groupSearchMemberArea").css("display","block"); ExecuteOrDelayUntilScriptLoaded(showSiteGroups(), "sp.js"); }); $j( "#takeOverGroupName" ).click(function() { grpName = $j("#groupsSelect").val(); $j("#groupName").val(grpName); $j("#groupMembersResultArea").css("display","block"); $j("#removeUserFromGroupArea").css("display","block"); ExecuteOrDelayUntilScriptLoaded(showMembersOfGroup(grpName), "sp.js"); }); $j( "#checkGroup" ).click(function() { grpName = $j("#groupName").val(); $j("#groupMembersResultArea").css("display","block"); ExecuteOrDelayUntilScriptLoaded(showMembersOfGroup(grpName), "sp.js"); }); $j( "#doSearch" ).click(function() { searchTerm = $j("#searchTerm").val(); ExecuteOrDelayUntilScriptLoaded(searchInGroupMembers(searchTerm), "sp.js"); }); $j( "#removeUsersFromGroup" ).click(function() { userIDs = $j( "#jQueryResult input.userID" ).serializeArray(); //userIDs = $j( "#jQueryResult input.userID" ); grpName = $j("#groupName").val(); $j("#removeUserFromGroupResult").css("display","block"); ExecuteOrDelayUntilScriptLoaded(removeUsersFromGroup(userIDs, grpName), "sp.js"); }); function showSiteGroups() { var currentContext = new SP.ClientContext.get_current(); var currentWeb = currentContext.get_web(); var allGroups = currentWeb.get_siteGroups(); currentContext.load(allGroups); currentContext.executeQueryAsync(OnSuccess, OnFailure); function OnSuccess(sender, args) { var groupEnumerator = allGroups.getEnumerator(); while (groupEnumerator.moveNext()) { var group = groupEnumerator.get_current(); $j("#groupsSelect").append('<option value="' + group.get_title() + '">' + group.get_title() + '</option>' ); } } function OnFailure(sender, args) { $j("#jQueryResult").html("No group / no members found"); } } function showMembersOfGroup(grpName) { $j("#jQueryResult").html('<table id="resultTable" border="1" ></table><p> </p>'); $j("#jQueryResult").prepend("<h4>Group: "+grpName+"</h4>"); var userInGroup; var currentContext = new SP.ClientContext.get_current(); var currentWeb = currentContext.get_web(); var allGroups = currentWeb.get_siteGroups(); currentContext.load(allGroups); var group = allGroups.getByName(grpName); //$j("#jQueryResult h4").append("(id: " + group.get_id() + ")"); currentContext.load(group); var groupUsers = group.get_users(); currentContext.load(groupUsers); currentContext.executeQueryAsync(OnSuccess, OnFailure); function OnSuccess(sender, args) { var groupEnumerator = allGroups.getEnumerator(); while (groupEnumerator.moveNext()) { var group = groupEnumerator.get_current(); $j("#groupsSelect").append('<option value="' + group.get_title() + '">' + group.get_title() + '</option>' ); } $j("#jQueryResult table").append('<tr><td>check</td><td>id</td><td>title</td><td>email</td></tr>'); var groupUserEnumerator = groupUsers.getEnumerator(); while (groupUserEnumerator.moveNext()) { var groupUser = groupUserEnumerator.get_current(); $j("#jQueryResult table").append('<tr><td><input class="userID" name="userID" type="checkbox" value="' + groupUser.get_id() + '" /></td><td>' + groupUser.get_id() + '</td><td>' + groupUser.get_title().toLowerCase() + '</td><td>' + groupUser.get_email().toLowerCase() + '</td></tr>' ); } } function OnFailure(sender, args) { $j("#jQueryResult").html("No group / no members found"); } } function searchInGroupMembers(searchTerm) { searchTerm = searchTerm.toLowerCase(); $j("#jQueryResult td").css( "background-color", "transparent" ); $j("#jQueryResult").css("border-color", "red"); $j("#groupSearchMemberResult").html(searchTerm); if( searchTerm !="" && ( $j("#jQueryResult td:contains("+searchTerm+")").size() > 0 ) ){ $j("#groupSearchMemberResult").prepend("FOUND: "); $j("#groupSearchMemberResult").css("background-color", "lightgreen"); $j("#jQueryResult td:contains("+searchTerm+")").css( "background-color", "yellow" ); }else{ $j("#groupSearchMemberResult").prepend("NOT found: "); $j("#groupSearchMemberResult").css("background-color", "red"); } if( searchTerm == "" ){ $j("#groupSearchMemberResult").html("Please enter a search term"); } } function removeUsersFromGroup(userIDs, grpName) { var currentContext = new SP.ClientContext.get_current(); var currentWeb = currentContext.get_web(); var allGroups = currentWeb.get_siteGroups(); var group = allGroups.getByName(grpName); currentContext.load(group); $j("#removeUserFromGroupResult").html("Remove user from group <strong>" + grpName + "</strong>: <br/>" ); userIDs.forEach(remove); function remove(field) { var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(" + group.get_id() + ")/users/removebyid(" + field.value + ")"; $j("#removeUserFromGroupResult").prepend(requestUri + "<br/>"); $j.ajax({ url: requestUri, type: "POST", success: onSuccess, error: onError, headers: { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "IF-MATCH": "*", "X-HTTP-Method": null } }); function onSuccess(data) { $j("#removeUserFromGroupResult").append(field.value + " removed successfully<br/>"); } function onError(error) { $j("#removeUserFromGroupResult").append(field.value + " not removed. Error: " + JSON.stringify(error) + "<br/>"); } } } })(jQuery); </script>