MS SharePoint as CMS
Friday, June 20th, 2008In the same vein as using a Google Spreadsheet as a CMS, you can use Microsoft SharePoint similarly.
At work, we us Microsoft SharePoint as our intranet website. Although I think that it has complicated processes, in some ways, I am open to making the best of it. When we started maintaining calendars and lists in SharePoint that we then duplicated on our website, I saw an opening to not only reduce this redundancy, but also to automate website content. Additionally, because I, or someone else, must make any changes to the website, moving the management of some content to within SharePoint will allow others to update this information.
Posting the staff directory in SharePoint, complete with job titles and phone extensions, provided the perfect opportunity to demonstrate to the powers-that-be how we could take advantage of SharePoint beyond it being a company forum and bulletin board.
Basically, I used the RSS feed from SharePoint. There are three parts: the PHP to grab the RSS feed, one XSL file to convert the RSS feed to predictable XML, and another XSL file to render the rss2xml data however it is desired.
Here’s the PHP. We must access our SharePoint through HTTP authentication, therefore CURL was used to access the RSS feed. After one has the RSS feed, the RSS is converted to more predictable XML, and then converted again for HTML output.
// SharePoint RSS feed ID
$feedid = “{01835674-500B-4B3D-BB6C-3F1B1E301E71}”;
$xml_file = “http://username:password@sharepoint/_layouts/listfeed.aspx?List=$feedid”;
// generally the first column of a list is the <title> element
// this variable customizes the XML element name when is is converted
$title_field = “firstname”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT,“Mozilla/4.0″);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$results = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close ($ch);
//print_r($results);
//exit;
// MS SharePoint uses a CDATA section for most of the list content.
// Let’s get rid of Cdata sections, so that the entire file is treated as XML
$results = str_replace(“<![CDATA[" , "" , $results);
$results = str_replace("]]>” , “” , $results);
$results = str_replace(“<b>” , “” , $results);
$results = str_replace(“</b>” , “” , $results);
// Load the XML source
$xml = new DOMDocument;
$xml->loadXML($results);
// This file converts the RSS feed to usable XML, see below
$xsl_file = “sharepointrss2xml.xsl”;
$xsl = new DOMDocument;
$xsl->load($xsl_file);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
// Set XSLT parameters
$proc->setParameter(”, ‘TITLEELEMENT’, $title_field);
$usablexml = $proc->transformToXML($xml);
$xml = new DOMDocument;
$xml->loadXML($usablexml);
// This file converts the usable XML, to HTML output for display
$xsl_file = “staff2html.xsl”;
$xsl = new DOMDocument;
$xsl->load($xsl_file);
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
?>
Convert MS SharePoint RSS to usable XML. In theory, this file can be used on most, if not all, MS SharePoint RSS feeds. For the most part, the column heading of each column in the SharePoint list is captured and used as the name of the XML element.
<xsl:stylesheet
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“1.0″>
<xsl:output method=“xml” indent=“yes”></xsl:output>
<xsl:strip-space elements=“*”></xsl:strip-space>
<xsl:template match=“/”>
<sharepointfeed>
<xsl:apply-templates select=“*/channel/item” />
</sharepointfeed>
</xsl:template>
<xsl:template match=“*/channel/item”>
<item>
<xsl:text disable-output-escaping=“yes”><</xsl:text><xsl:value-of select=“$TITLEELEMENT” /><xsl:text disable-output-escaping=“yes”>></xsl:text>
<xsl:value-of select=“title” />
<xsl:text disable-output-escaping=“yes”></</xsl:text><xsl:value-of select=“$TITLEELEMENT” /><xsl:text disable-output-escaping=“yes”>></xsl:text>
<xsl:for-each select=“description/div”>
<xsl:text disable-output-escaping=“yes”><</xsl:text>
<xsl:value-of select=“substring-before( translate( . , ‘ ‘ , ‘_’) , ‘:’)” />
<xsl:text disable-output-escaping=“yes”>></xsl:text>
<xsl:value-of select=“normalize-space( substring-after( . , ‘:’) )” />
<xsl:text disable-output-escaping=“yes”></</xsl:text>
<xsl:value-of select=“substring-before( translate( . , ‘ ‘ , ‘_’) , ‘:’)” />
<xsl:text disable-output-escaping=“yes”>></xsl:text>
</xsl:for-each>
</item>
</xsl:template>
</xsl:stylesheet>
Finally, convert the XML above to HTML output for display.
<xsl:stylesheet
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
version=“1.0″>
<xsl:output method=“html” indent=“yes”></xsl:output>
<xsl:strip-space elements=“*”></xsl:strip-space>
<xsl:template match=“/”>
<xsl:call-template name=“dodepartments” />
<xsl:call-template name=“donames” />
</xsl:template>
<xsl:template name=“dodepartments”>
<table width=“95%”>
<xsl:for-each select=“sharepointfeed/item”>
<xsl:sort select=“Last_Name”/>
<xsl:if test=“normalize-space(Last_Name)=””>
<tr>
<td valign=“top”><b><xsl:value-of select=“firstname” /></b></td>
<td valign=“top”>312.555.<xsl:value-of select=“Extension” /></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
<br /><br />
</xsl:template>
<xsl:template name=“donames”>
<table width=“95%”>
<xsl:for-each select=“sharepointfeed/item”>
<xsl:sort select=“Last_Name”/>
<xsl:if test=“Last_Name!=””>
<tr>
<td valign=“top”>
<p><b><xsl:value-of select=“Last_Name” /><xsl:text>, </xsl:text><xsl:value-of select=“firstname” /></b><br />
<xsl:value-of select=“Title” /><br />
<a href=“#”>Profile Page</a></p>
</td>
<td valign=“top”>
<p align=“right”>
312.555.<xsl:value-of select=“Extension” /><br />
<a href=“mailto:{Email}@domain.com”><xsl:value-of select=“Email” />@domain.com</a>
</p>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Here are the files altogether:
MS SharePoint as CMS