Monday, October 26, 2009

Client-Side Programming in Sharepoint # 2

Get All User Collection From Web using javascript


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 = '&lt;?xml version="1.0" ?&gt;' +
    '&lt;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/"&gt;' +
    ' &lt;xsl:template match="/"&gt;' +
    ' &lt;table class="resulttable"&gt;' +
    ' &lt;th class="heading"&gt;' +
    ' &lt;td class="heading"&gt;Name&lt;/td&gt;' +
    ' &lt;td class="heading"&gt;Login&lt;/td&gt;' +
    ' &lt;td class="heading"&gt;Email&lt;/td&gt;' +
    ' &lt;/th&gt;' +
    ' &lt;xsl:apply-templates select="//response:Users" /&gt;' +
    ' &lt;/table&gt; &lt;div class="ms-vb"&gt;Site administrators are &lt;span class="admintd"&gt;highlighted&lt;/span&gt;.&lt;/div&gt;' +
    ' &lt;/xsl:template&gt;' +
    ' &lt;xsl:template match="//response:Users"&gt;' +
    ' &lt;xsl:for-each select="response:User"&gt;' +
    ' &lt;tr&gt;' +
    ' &lt;td&gt;&lt;/td&gt;' +
    ' &lt;td&gt;' +
    ' &lt;xsl:choose&gt;' +
    ' &lt;xsl:when test="@IsSiteAdmin=\'True\'"&gt;' +
    ' &lt;xsl:attribute name="class"&gt;admintd&lt;/xsl:attribute&gt;' +
    ' &lt;/xsl:when&gt;' +
    ' &lt;xsl:otherwise&gt;' +
    ' &lt;xsl:attribute name="class"&gt;&lt;/xsl:attribute&gt;' +
    ' &lt;/xsl:otherwise&gt;' +
    ' &lt;/xsl:choose&gt;' +
    ' &lt;xsl:value-of select="@Name"/&gt;' +
    ' &lt;/td&gt;' +
    ' &lt;td class="userlistvalue"&gt;' +
    ' &lt;xsl:value-of select="@LoginName"/&gt;' +
    ' &lt;/td&gt;' +
    ' &lt;td class="userlistvalue"&gt;' +
    ' &lt;xsl:if test="@Email!=\'\'"&gt;' +
    ' &lt;a&gt;' +
    ' &lt;xsl:attribute name="href"&gt;' +
    ' mailto:&lt;xsl:value-of select="@Email"/&gt;' +
    ' &lt;/xsl:attribute&gt;' +
    ' &lt;xsl:value-of select="@Email"/&gt;' +
    ' &lt;/a&gt;' +
    ' &lt;/xsl:if&gt;' +
    ' &lt;/td&gt;' +
    ' &lt;/tr&gt;' +
    ' &lt;/xsl:for-each&gt;' +
    ' &lt;/xsl:template&gt;' +
    '&lt;/xsl:stylesheet&gt;';


    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(/&lt;/g, '<').replace(/&gt;/g, '>'));

    el.innerHTML = xml.transformNode(xsl);
    }
    else
    {
    el.innerText = xml;
    }
    }
    }
    }
    </script>




More - Client-Side Programming in Sharepoint

2 comments:

Nancy said...

Is there any way we can display the actual username and groupnames which user belong to in Form Based Application?

Nancy Burns

Ganesh Jat said...

you can try usergroup web services
http://msdn.microsoft.com/en-us/library/ms772647.aspx