In this article we will see how we can call sharepoint webservices using javascript.Sharepoint provide a webservice usergroup.asmx for user related operations.with the help of xslt , we can show the data retruned by webservice.Following are the steps-
- Add a content editor webpart on your page
- Open SOurce editor
- Add a div element which will show the result
<div id="results"></div>
Add following style element
<style>
.resulttable { margin-left:5px; margin-top:3px; padding-top:0px; }
.heading { font-weight:bold; }
.admintd { font-weight: bold; color: #009900; }
</style>
Add following javascript code in source editor
<script type="text/javascript">
//declare a variable which will hold xslt code
var _xslText = '<?xml version="1.0" ?>' +
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:response="http://schemas.microsoft.com/sharepoint/soap/directory/">' +
' <xsl:template match="/">' +
' <table class="resulttable">' +
' <th class="heading">' +
' <td class="heading">Name</td>' +
' <td class="heading">Login</td>' +
' <td class="heading">Email</td>' +
' </th>' +
' <xsl:apply-templates select="//response:Users" />' +
' </table> <div class="ms-vb">Site administrators are <span class="admintd">highlighted</span>.</div>' +
' </xsl:template>' +
' <xsl:template match="//response:Users">' +
' <xsl:for-each select="response:User">' +
' <tr>' +
' <td></td>' +
' <td>' +
' <xsl:choose>' +
' <xsl:when test="@IsSiteAdmin=\'True\'">' +
' <xsl:attribute name="class">admintd</xsl:attribute>' +
' </xsl:when>' +
' <xsl:otherwise>' +
' <xsl:attribute name="class"></xsl:attribute>' +
' </xsl:otherwise>' +
' </xsl:choose>' +
' <xsl:value-of select="@Name"/>' +
' </td>' +
' <td class="userlistvalue">' +
' <xsl:value-of select="@LoginName"/>' +
' </td>' +
' <td class="userlistvalue">' +
' <xsl:if test="@Email!=\'\'">' +
' <a>' +
' <xsl:attribute name="href">' +
' mailto:<xsl:value-of select="@Email"/>' +
' </xsl:attribute>' +
' <xsl:value-of select="@Email"/>' +
' </a>' +
' </xsl:if>' +
' </td>' +
' </tr>' +
' </xsl:for-each>' +
' </xsl:template>' +
'</xsl:stylesheet>';
var _feedUrl = 'http://server-test/_vti_bin/usergroup.asmx?op=GetAllUserCollectionFromWeb';
var httpObject = false;
if (window.XMLHttpRequest)
{
try
{
httpObject = new XMLHttpRequest();
}
catch(e)
{
httpObject = false;
}
}
else if (window.ActiveXObject)
{
try
{
httpObject = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
httpObject = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
httpObject = false;
}
}
}
if (httpObject)
{
//Prepare the SOAP request string
var SOAPBody = '<?xml version="1.0" encoding="utf-8"?><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/"><soap:Body><GetAllUserCollectionFromWeb xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/" /></soap:Body></soap:Envelope>';
httpObject.onreadystatechange = function() { DisplayFeed(); };
httpObject.open("POST", _feedUrl, true);
httpObject.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
httpObject.setRequestHeader("Content-Length", SOAPBody.length);
httpObject.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/directory/GetAllUserCollectionFromWeb");
httpObject.send(SOAPBody);
}
function DisplayFeed()
{
if (httpObject.readyState == 4 && httpObject.status == 200 && httpObject.responseXML)
{
var xmlText = httpObject.responseText
var el = document.getElementById('results');
if (!el)
{
alert('display div missing');
}
else
{
if (window.ActiveXObject)
{
var xml = new ActiveXObject("Msxml2.DOMDocument");
xml.loadXML(xmlText);
var xsl = new ActiveXObject("Msxml2.DOMDocument");
xsl.loadXML(_xslText.replace(/</g, '<').replace(/>/g, '>'));
el.innerHTML = xml.transformNode(xsl);
}
else
{
el.innerText = xml;
}
}
}
}
</script>
More - Client-Side Programming in Sharepoint
2 comments:
Is there any way we can display the actual username and groupnames which user belong to in Form Based Application?
Nancy Burns
you can try usergroup web services
http://msdn.microsoft.com/en-us/library/ms772647.aspx
Post a Comment