Sharing a Calendar in Sharepoint 2007 – Part II

Earlier I wrote about how you can use XML+XSL to get around the limitation in Sharepoint 2007 of sharing calendars across multiple sites. This technique stores your events as XML and allows you to publish entries wherever and however you wish.

This article expands that earlier introduction with some tips and tricks we collating when implementing this for one client:

  • working outside Sharepoint
  • presenting a URL
  • sorting items
  • grouping items
  • showing a message if a filter set is empty

Note: You can download sample XML and XSL here. These files show all the techniques described in this article.

the original XML filtered view grouped view

Working outside SharePoint

It can be painful to develop your XML/XSL solution in Sharepoint because of the need to upload new versions (or edit webparts), then refresh to see the results, or lack thereof. Easier to work outside and just upload when done.

To link your XML and XSL outside SharePoint edit your XML file to include a link to your XSL file:

<!--?xml version="1.0" encoding="utf-8"?-->
<!--?xml-stylesheet type="text/xsl" href="calendar.xsl"?-->
....

Now when you open your XML file in a browser it will use your XSL to present.

Save your XLS as a file with a name to match the XML link and you’re nearly there. The only other change you may need to make is to extend your XSL so it generates a full webpage, not just a fragment.

For example our standard calendar presentation XSL generates a paragraph:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="calendar">
<p>
<xsl:for-each select="event[program='Leadership Foundations']">
...
</xsl:for-each>
</p>
</xsl:template>
</xsl:stylesheet>

To work outside SharePoint it needs to generate a proper page; make it do so by simply adding HTML and BODY tags:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="calendar">
<html>
<body>
<p>
<xsl:for-each select="event[program='Leadership Foundations']">
...
</xsl:for-each>
</p>
<body>
<html>
</xsl:template>
</xsl:stylesheet>

Just remember to remove these bonus lines when re-inserting into your SharePoint site.

Presenting a URL

If one element you’re recorded in your XML is a URL then chances are you’ll want to use XSL to convert that into a link. And the obvious approach you’d think would be the simplest:

<a href='<xsl:value-of select="link">'>link text</a>

Other than the issue over nested quotes, all looks pretty straightforward. Except it doesn’t work.

The solution is to use a different format for the URL selection:

<a href="{link}">link text</a>

Now it works fine.

Sorting items

One feature we wanted to implement was the automatic sorting of results. This meant anyone maintaining the master XML list did not need to worry about where to add individual items to preserve their order. They could just add anywhere in the file and be confident they’d appear in the correct sequence when displayed.

To sort results, add the <xsl:sort> command as the first element within your loop. For example to sort by a date element:

<xsl:for-each select="event">
    <xsl:sort select="date" /> 
    ...
</xsl:for-each>

Check W3Schools if wanting to do something more complicated than an ascending text order.

As you can see from this example, what we were wanting to search on were dates. And early on we’d decided that users would enter dates in “human” format (ie ’23 May 2011″, not 20110523). One issue we had on sorting dates was the chestnut of how to sort “2 May 2011″ to appear before “15 May 2011″. Our solution was to require all dates in the XML to be two characters (“02 May 2011″ not “2 May 2011″) and then use XSL to remove the ugly leading zero when displayed. The code to remove that zero is straightforward:

<xsl:choose>
    <xsl:when test="substring(date,1,1)=0">
         <xsl:value-of select="substring(date,2)" />
    <xsl:otherwise>
</xsl:choose>

You have to use the <xsl:choose> construct because while XSL does have an IF statement (not surprisingly <xsl:if>), it does not have an equivalent ELSE.

Note: The alternative would be to define the date in a nice sortable format (I vote for YYYYMMDD). And then use similar XSL transformations to present in a more readable format. Either way you’re putting some overhead on the user. We stuck with this simple approach because it was (a) less user effort and (b) allowed a wider range of dates to be acceptable. For example we had some programs that only specified a month. Sorting this way meant we could accept “May 2011″ as a valid date.

Grouping items

Now this I thought would be easy. Since we’re working on a calendar, let’s group entries according to the month. ie:

April 2011
- This course (12 April, London)
- That course (19 April, Helsinki)
May 2011
- That course (2 May, Romford)
- The other course (30 May, Malaka)

After several false starts, all roads do eventually lead to a fabled Meunchian method. Trouble is, even after building it, I’ve no idea how it works. Of all the articles uncovered to try and explain it, this one came closest. Cut&paste their approach (or our example using it) and you should be able to get it to work.

Showing a message if the filter set is empty

In the first article we covered how simple it is to present a subset of your entries. However once implemented we discovered we needed to address the situation where a subset was empty. Rather than just showing a blank list we wanted to show a messsage directing the visitor to other sources.

Simplest way to implement this is to repeat the same filter query used to generate the subset. Just place within a COUNT() and use as a test:

<xsl:choose>
     <xsl:when test="count(event[program='First Program']) > 0">

          <xsl:for-each select="event[program='First Program']">
          ...
          </xsl:for-each>

     </xsl:when>
     <xsl:otherwise>
          <xsl:text>No events recorded for this program.</xsl:text>
     </xsl:otherwise>
</xsl:choose>

Only potential issue may be around your formatting. Be mindful of where your layout tags appear depending on which data is presented.

Conclusion

With just these techniques you can present and manipulate your content in quite ingenious ways. Just good luck trying to understand the Muench.

2 thoughts on “Sharing a Calendar in Sharepoint 2007 – Part II

  1. carri.hutcheson@reallyfast.info

    Amazing! Its genuinely awesome article, I have got much clear idea about from this piece of writing.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>