Wednesday, 23 July 2014

Grabbing Wiki Content Using SharePoint Web Services

I presented a session at UNHS today. One of the samples I showed was how information can be consumed from a SharePoint web service and repurposed. The specific example was taking the Wiki content we host internally on our FAQ site (it's a great example of using Wikis to build a Frequently Asked Questions knowledgebase) and creating an html file from it.

The code sample is straightforward. I developed it using Visual C# Express Edition on my Windows server 2012 machine. The web reference I added was http://tfserver:505/_vti_bin/lists.asmx.

In this particular sample, I retrieve content from a specific view. In that view, I've actually added the Wiki Content field which is why the content is returned. Instead of getting content from a specific view, I could have specified a query and the fields I wanted returned as parameters to the web service.

  Here's the sample:


SharePoint_FAQ_Lists.Lists ls = new SharePoint_FAQ_Lists.Lists();
ls.Credentials = System.Net.CredentialCache.DefaultCredentials;
string url = "http://faq/sites/sharepoint";
ls.Url = url + @"/_vti_bin/lists.asmx";

//get all wiki pages from the "Wiki Pages" library from a particular view. The second parameter is the GUID of the view
XmlNode wListItems = ls.GetListItems("Wiki Pages", "{3F1DB907-7D89-4139-99EC-443D1F9BECC7}", null, null, "", null, "");

//assemble the html content that will eventually
string htmlBody = "<html><body>";

//iterate through the returned XML and parse out the relevant information
foreach (XmlNode outerNode in wListItems.ChildNodes)
{
if (outerNode.NodeType.Equals(System.Xml.XmlNodeType.Element))
{
foreach (XmlNode node in outerNode.ChildNodes)
{
if (node.NodeType.Equals(System.Xml.XmlNodeType.Element))
{
XmlNode nameNode = node.Attributes.GetNamedItem("ows_LinkFilename");
XmlNode wikiNode = node.Attributes.GetNamedItem("ows_WikiField");

String wikiName = nameNode.InnerText;
String wikiBody = wikiNode.InnerText;

htmlBody += "<a href=\"" + "http://faq/sites/sharepoint/wiki pages/"+ wikiName + "\">" + wikiName + "</a><br>";
htmlBody += wikiBody;
}
}
}
}

htmlBody += "</body></html>";

string fileName = @"c:\temp.html"
GenerateFile(htmlBody, fileName); 

---------------------------------------- END OF CODE--------------------------------------------------

1 comment: