Wednesday 30 November 2011

Using jQuery to return all users of a group in SharePoint

Using jQuery to return all members / users of a group in SharePoint

Determine if a user is within a particular group in SharePoint and act upon the output.
Luckily the standard SharePoint web services allow to query for this information utilising the User Group service at location: http://<server>/_vti_bin/usergroup.asmx.
In particular the method GetUserCollectionFromGroup will provide the complete list including the Windows SAMAccountName (login name) as part of the return.
However, in this instance I was caught a little by a permissions problem.  Although I could query the web service, it kept returning a status of “parsererror” and an output of “Access Denied”.
This was resolved by changing the group settings to allow “Everyone” to read the membership of the group.
GroupSettingsBlog
Code Example
The following code utilises jQuery to access the web service:
1 <script type='text/javascript'> 2 3 _spBodyOnLoadFunctionNames.push("getUserCollectionFromGroup"); 4 5 function getUserCollectionFromGroup() { 6 var soapEnv = 7 "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \ 8 <soap:Body> \ 9 <GetUserCollectionFromGroup xmlns='http://schemas.microsoft.com/sharepoint/soap/directory/'> \ 10 <groupName>Group Name Here</groupName> \ 11 </GetUserCollectionFromGroup> \ 12 </soap:Body> \ 13 </soap:Envelope>"; 14 $.ajax({ 15 url: "/_vti_bin/usergroup.asmx", 16 type: "POST", 17 dataType: "xml", 18 data: soapEnv, 19 complete: getUserCollectionFromGroupReturn, 20 contentType: "text/xml; charset=\"utf-8\"" 21 }); 22 } 23 function getUserCollectionFromGroupReturn(xData, status) { 24 alert("Status: " + status); 25 alert("Output: " + xData.responseText); 26 } 27 </script> 28

No comments:

Post a Comment