Rocky Mountain SharePoint User Group

"Together We Can Learn More."
Home     Meetings     Resources     Contact Us     Latest Jobs     Events      
News     Training     Books     Sites     Webcasts      
SharePoint Resources & Articles

The Rocky Mountain SharePoint User Group is here to provide you with an abundance of SharePoint-related resources.

Check out the link-tabs above to browse extra content that will sure help you in your learning and experience.

 

Calendar Rollup from Today Forward with Custom Styles

by Mike Dockery

 

Let's say you have a calendar on a subsite with many events.  You'd like to display a summary of the events from today forward in an easy to read table format showing the event title, the start date and the end date.

To accomplish this we need use a Content Query Web Part (CQWP), use a Content Editor Web Part, export/import a web part, and modify the ItemStyle XSL  We don't need to use SharePoint Designer for this.  Of course, with SPD you can convert an Events list into a Data View and modify it any number of ways.  Let's keep it simple.

Open the calendar.

In a new browser, open a parent site in the same site collection.  It could be the same site.
From the Site Actions menu, click Edit Page, Add a Web Part, and drag a Content Query Web Part onto the page.

Modify the web part and expand Query.

Click the Show items from the following list button and click the Browse button.  Expand the subsite (if applicable) and find and select the Calendar.  Click OK to populate the field.

For List Type, select Calendar.

For Content Type, leave default of <All Content Types>.

 


Optionally to show events from today forward, in the Additional Filters in Show items when select Start Time then choose is greater than or equal to and click [Today].
 

 

Expand the Presentation section.

Leave Group items by alone and for Sort items by, select Created then check the box to Show items in descending orderOptionally check the box to Limit the number of items to display and enter a desired number in Item limit.
 


In the Styles section, leave the Group style as Default and for Item style leave the default (for now) such as 'Image on left'.  We will handle the styling next.

Click OK.  You should see the query return values from the calendar without dates.  Too plain to be useful at this point!

Still in page edit mode, on the new Content Query Web Part, click edit and click Export.
 

 

Save the web part to your computer.  It will save as named, e.g., Content_Query_Web_Part.webpart.  (If you inserted a title in the Appearance the web part will be saved as that name).

Open the .webpart file in a text editor.  Scroll down and locate the CommonViewFields property.

As mentioned previously, we want to return the title, the event start date and the end date.  How do find out how to get these? 

 

You need the internal column names.  See Heather Solomon's blog and this MSDN Forum for further details.

Edit it to read (with no spaces):

<property name="CommonViewFields" type="string">Title,RichHTML;EventDate,DateTime;EndDate,DateTime</property>

Save the .webpart file and return to the page in edit mode.

Click Add a Web part and in the new window click the Advanced Web Part gallery and options link.  At the top of the edit pane it will default to Browse.  Click the arrow down to Import.
 


Browse to locate the edited .webpart saved locally.  Click Open to get the file. Click the Upload button.

The uploaded web part will be shown, so drag it onto the page.

Delete the previous Content Query Web Part [1] as it's not needed  any more.

Exit edit mode.

Now we turn to the ItemStyle.xsl file located at the site collection level.

From the top site, browse All Site Content or Content and Structure.  Open the Style Library.  Get into the XSL Styles Sheets folder.


Click the context menu for ItemStyle to Send To and choose Download a copy and save it to your desktop.  Please make a backup of the file right away! Versioning is on by default in the Style Library but it's sometimes easier to quickly upload the original file if things go awry in the XSL editing (don't worry, you'll error out at some point!). 
A great reference is found in "Customizing the Content Query Web Part XSL" on MSDN.

Open ItemStyle.xsl in any text editor or SharePoint Designer (SPD). 

At the top alongside the other attributes, add the ddwrt namespace which will handle using dates. 


    xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

 

Scroll down in the file and copy any existing <xsl:template from its start tag to the end tag.

 

Edit the copied template as follows:

  • Change the name and match attributes to be unique.  The name will be selectable in the Item Style section when editing the CQWP.
  • Add a StartDate variable to pull in the calendar entry start date which uses the internal column name of EventDate. 
    • 1033 refers to English. 
      • 1031 German
      • 1036 French
      • 1040 Italian
      • 1034 Spanish
      • 1049 Russian
      • 1043 Dutch
    • MMM dd, yyyy will display the date in a particular format you need.  Some of the options are:
      • MMM dd, yyyy -- Oct 11, 2008
      • MMMM dd, yyyy -- October 11, 2008
      • MM/dd/yyyy -- 10/11/2008.
  • Add an EndDate variable to pull in calendar entry ending date.  The same options as before.
  • The actual output will be contained within the <div class="link-item"> near the bottom of your copied template.  Add HTML tags with CSS attributes.  Be careful in this section and test after small changes to be sure you don't crash the page.
  • Full sample (see an image of this at the end of the article):

    <xsl:template name="MyCustomCalendar" match="Row[@Style='MyCustomCalendar']" mode="itemstyle">
        <xsl:variable name="StartDate">
            <xsl:value-of select="ddwrt:FormatDateTime(string(@EventDate) ,1033 ,'MMM dd, yyyy')" />
        </xsl:variable>
        <xsl:variable name="EndDate">
            <xsl:value-of select="ddwrt:FormatDateTime(string(@EndDate) ,1033 ,'MMM dd, yyyy')" />
        </xsl:variable>
        <xsl:variable name="SafeImageUrl">
            <xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
                <xsl:with-param name="UrlColumnName" select="'ImageUrl'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="SafeLinkUrl">
            <xsl:call-template name="OuterTemplate.GetSafeLink">
                <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="DisplayTitle">
            <xsl:call-template name="OuterTemplate.GetTitle">
                <xsl:with-param name="Title" select="@Title"/>
                <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="LinkTarget">
            <xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
        </xsl:variable>
        <div id="linkitem" class="item">
            <xsl:if test="string-length($SafeImageUrl) != 0">
                <div class="image-area-left">
                    <a href="{$SafeLinkUrl}" target="_blank">
                        <img class="image-fixed-width" src="{$SafeImageUrl}" alt="
{@ImageUrlAltText}"/>
                    </a>
                </div>
            </xsl:if>
            <div class="link-item" width="100%">
            <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
                <div style="clear:all;float:left;width:35%;"><a href="{$SafeLinkUrl}" target="_blank" title="
{@LinkToolTip}" style="font:bold 11px arial,helvetica,tahoma,verdana,sans-serif;color:#3190D0;">
                    <xsl:value-of select="$DisplayTitle"/>
                </a></div>
                <div style="float:right;width:30%;color:#555555;font:normal 11px arial,tahoma,verdana;">
                    <xsl:value-of select="$EndDate"/>
                </div>
                <div style="width:35%;color:#555555;font:normal 11px arial,tahoma,verdana;">
                    <xsl:value-of select="$StartDate"/>
                </div>
            </div>
        </div>
      </xsl:template>

 

Save the file and upload it back to the Style Library, add in comments and check in the file.

Open up the site where you placed the imported Content Query Web Part.

From the Site Actions menu, click Edit Page.

Modify the CQWP and expand Presentation.

In the Styles section, leave the Group style as Default and for Item style select your new template name from the XSL file.  It is MyCustomCalendar in my sample.

 


Expand the Appearance section.  Scroll down to Chrome Type and change it to None.

Click OK.  The query will return the events from the calendar from today forward in the styles based on the CSS attributes you created.  If you used my sample XSL template, your output will be:
 

 

The final step (and it's optional) is to jazz up the site and add headings to the columns and a title.

Edit the page and add a Content Editor Web Part just above your Content Query Web Part.

Open the Rich Text or Source editor.  It may be a good to add a link to add a new entry and a link to view all events.  Code the HREF for each to those pages (testing first, obviously).  For the column headers, play with the widths until it matches the output in the CQWP events.  Finally, modify the Appearance and enter a title of the calendar that describes the actual calendar, business unit, calendar purpose or whatever.  Leave the Chrome Type to default, Title only, or Title and Border.

Have fun with the XSL and don't be afraid to customize to your needs.  The attributes described here are only a starting point. 

 

Good luck!

Here's the finished example: