Monday, July 29, 2013

SharePoint 2007 Lists Web Service

SharePoint 2007 offers a web service that can be used to query the lists/libraries in your SharePoint site. The service is available at http://server/adm/_vti_bin/lists.asmx and is enabled by default. Below is an image containing the various methods available for use.

Use of the service requires authentication; via Active Directory or in this example using forms based authentication. First create a web reference to the URL above (customize it for your server) and call the reference 'ListProxy'.

ListProxy.Lists newListProxy = new AdvertReport.Generate.ListProxy.Lists();
Here is the code to authenticate with FBA:
protected bool AuthenticateFBAClaims()
{
string userName = ConfigurationSettings.AppSettings["SharePointUser"].ToString();
string password = ConfigurationSettings.AppSettings["SharePointPassword"].ToString();
string server = ConfigurationSettings.AppSettings["SharePointServer"].ToString();

Authentication spAuthentication = new Authentication();
spAuthentication.Url = server + "_vti_bin/Authentication.asmx";
spAuthentication.CookieContainer = new CookieContainer();
LoginResult loginResult = spAuthentication.Login(userName, password);
authCookie = new Cookie();
if (loginResult.ErrorCode == LoginErrorCode.NoError)
{
CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
authCookie = cookies[loginResult.CookieName];
cookieContainer = new CookieContainer();
cookieContainer.Add(authCookie);
return true;
}
else
return false;
}
}
Now a list can be queried (lists, views and other SharePoint objects are referenced by their GUIDs which can be found using SharePoint Manager 2007).
newListProxy.CookieContainer = cookieContainer;

XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
ndQuery.InnerXml = CAML
XmlNode ndListItems = newListProxy.GetListItems(listGuid, viewGuid, ndQuery, null, null, null, null);
The XML returned will be in SharePoint format (which can be difficult to work with). Below is a function I found online which converts SP XML to regular XML:
// Convert SP XML to Regular XML
public string ConvertZRowToRegularXml(string zRowData)
{
string xsltFromZRowToXml =
"<xsl:stylesheet version=\"1.0\" " +
"xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " +
"xmlns:s=\"uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882\" " +
"xmlns:z=\"#RowsetSchema\">" +
"<s:Schema id=\"RowsetSchema\"/>" +
"<xsl:output method=\"xml\" omit-xml-declaration=\"yes\" />" +
"<xsl:template match=\"/\">" +
"<xsl:text disable-output-escaping=\"yes\">&lt;rows&gt;</xsl:text>" +
"<xsl:apply-templates select=\"//z:row\"/>" +
"<xsl:text disable-output-escaping=\"yes\">&lt;/rows&gt;</xsl:text>" +
"</xsl:template>" +
"<xsl:template match=\"z:row\">" +
"<xsl:text disable-output-escaping=\"yes\">&lt;row&gt;</xsl:text>" +
"<xsl:apply-templates select=\"@*\"/>" +
"<xsl:text disable-output-escaping=\"yes\">&lt;/row&gt;</xsl:text>" +
"</xsl:template>" +
"<xsl:template match=\"@*\">" +
"<xsl:text disable-output-escaping=\"yes\">&lt;</xsl:text>" +
"<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
"<xsl:text disable-output-escaping=\"yes\">&gt;</xsl:text>" +
"<xsl:value-of select=\".\"/>" +
"<xsl:text disable-output-escaping=\"yes\">&lt;/</xsl:text>" +
"<xsl:value-of select=\"substring-after(name(), 'ows_')\"/>" +
"<xsl:text disable-output-escaping=\"yes\">&gt;</xsl:text>" +
"</xsl:template>" +
"</xsl:stylesheet>";

XslCompiledTransform transform = new XslCompiledTransform();
XmlDocument tidyXsl = new XmlDocument();
try
{
//Transformer
tidyXsl.LoadXml(xsltFromZRowToXml);
transform.Load(tidyXsl);
//output (result) writers
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (XmlTextWriter tw = new XmlTextWriter(sw))
{
//Source (input) readers
using (System.IO.StringReader srZRow = new System.IO.StringReader(zRowData))
{
using (XmlTextReader xtrZRow = new XmlTextReader(srZRow))
{
//Transform
transform.Transform(xtrZRow, null, tw);
return sw.ToString();
}
}
}
}
}
catch(Exception ex)
{
ErrorLog(ex.ToString());
return null;
}
}
Now you can convert the regular XML into a DataSet object:
DataSet ds = new DataSet();
using (System.IO.StringReader sr = new System.IO.StringReader(cleaned)) // Convert XML to DataSet
{
     ds.ReadXml(sr,
    XmlReadMode.Auto);
}

No comments:

Post a Comment