Excel REST meets XSL

Firstly, I would like to thank all those that came along to my sessions at the Australian and New Zealand SharePoint Conferences in Sydney and Auckland. I am a big fan of empowering end-users and wanted to show a realistic example of what was possible so the level of interest and number of relevant questions made it all worthwhile.

So, my scenario is that I have a country manager who wants to see a dashboard of his team performance against controlled targets. Given that I am in Australia I will stick with a local example so the team are state managers. The country manager is a competent Excel user and wants to keep as much control over the chart types etc as possible. The state managers should be able to input their results but not change their targets.

To start with we will create our Excel spread sheet – this will work fine with either 2010 or 2013, Excel or SharePoint in any combination.

Create a worksheet called Targets with headings for State, Q1, Q2, Q3 and Q4. Enter your data for each state. Eg QLD, 24, 28, 32, 26. I often use the randbetween() function to create dummy data quickly. Format as a table using your preferred style and name the table Targets.

Create another worksheet called Chart, in A1 type State and in B1 type one of your states eg QLD. Under the Formulas ribbon define a name for B1 called State.
From B2:E2 type Q1 to Q4. In A3 type Target and A4 type Actual. Now we want to get our target values from the Targets table depending on which state is entered into B1. Use a vlookup function in cell B3 as follows =vlookup(State, Targets, 2, FALSE). This will use the defined name State as the lookup value, search for it in the Targets table and return the second column (which is the target for Q1). FALSE means it needs an exact match. Copy this formula for Q2 to Q4 changing the column returned to be 3 to 5 accordingly.
Now enter some dummy data for your Actual values in row 4.

Last step is to create our chart. Select the data in cells A2:E4 and insert your preferred chart. Name the chart as Chart.

All done – you should now have something that looks like this:

Chart

If you change the state typed into B1 then the appropriate targets should automatically populate in row 3 and the chart redraw accordingly. If this is not the case, recap the previous steps.

Save your Excel file as States.xlsx and upload to your Docs library in SharePoint. Note that you will need Excel services to be running which also means you will need an Enterprise licence of SharePoint 2010 or 2013. If you are using Office 365 then all should be fine.
Now you have the file in SharePoint we can try out some Excel REST fun. Excel REST allows me to get data or objects from the Excel file using a URL. The URL is constructed as follows:
 http:// – the server name
 /_vti_bin/ExcelRest.aspx – the Excel REST page
 /Docs/States.xlsx – the path to your spread sheet from the site you make the call
 /model/Charts(‘Chart’) – the chart (or other object/model) we want to retrieve
The models available are:
 Charts
 Ranges
 Tables
 PivotTables
Try this out in your browser window. Type the URL using the elements as defined above:
Eg //server/site/_vti_bin/ExcelRest.aspx/Docs/States.xlsx/model/Charts('Chart')

You should now see the chart from your Excel file in your browser window. If not, then make sure you check you have the correct library name and file name and also the name of your chart in the Excel file. I have used everything from my example spread sheet above so if you have done anything differently then you will need to change the path or names to match.

The chart is returned as an image so this can be used in a variety of ways such as adding an image to a page and entering the URL as above.
So far so good, but we are only using the data within the spread sheet at the moment. To tell the spreadsheet to use different data we add parameters to our URL.
Eg //server/site/_vti_bin/ExcelRest.aspx/Docs/States.xlsx/model/Charts('Chart')?Ranges('Chart!B4')=40

Following on from the example above, this will set the value of the cell B4 in the spread sheet to 40 (the actual for Q1) and redraw the chart before returning the image.

Now if all you wanted was a chart from your list data then you could edit your page and insert a picture from address and use this as the URL. Easy. However, I did promise to mix this with some XSL to give you a dashboard so here goes.

I have created some custom XSL below which as you can see is pretty simple. It creates a table and then for each row put the Title in the first column and then an image in the second. The URL for the image is our REST call but with the values coming from our list columns.


<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
<table style="width:100%">
<xsl:for-each select="$Rows">
<xsl:call-template name="RowView" />
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="RowView">
<tr>
<td>
<xsl:value-of select="@Title" />
</td>
<td>
<img src="/sites/SPC2013/IV/_vti_bin/ExcelRest.aspx/Documents/States.xlsx/Model/Charts('Chart')?Ranges('Chart!B1')={@Title}&amp;Ranges('Chart!B4')={@Quarter1}&amp;Ranges('Chart!C4')={@Quarter2}&amp;Ranges('Chart!D4')={@Quarter3}&amp;Ranges('Chart!E4')={@Quarter4}" />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>

Notice that the ampersands separating the URL parameters have to be escaped – ie &amp;
In my example, I have saved this stylesheet to /Style Library/custom/Charts.xsl.

So far so good – now we need a SharePoint list to apply this to. Create a list called States. Rename the Title column to State and create number columns for Quarter1, Quarter2, Quarter3 and Quarter4.

Interesting thing to note here is that if you try to abbreviate your column names to Q1 or Qtr1 you will have trouble with your XSL. For some reason SharePoint does not like column names which are less than 5 characters and so will encode the first character. This makes it pretty horrible in XSL so I suggest you stick with the column names as above.

Add some data to your list and make sure you use the same names for the States.

Now for the fun bit. Create a new view on your States list and call it Dashboard. No need to apply filters but make sure all the input columns are showing. Edit the page in the view and now edit the properties of the web part. In the Miscellaneous section add the URL to your XSL file in the XSL Link box.

ChartXSLlink

Finally the magic bit. Click OK and stop editing the page. You will now see the name of each state followed by a chart redrawn for the data from each item in your list.

RESTdashboard

Excel REST and XSL – what a great combination!
Download the blog post as PDF – Excel REST Meets XSLT
Download the spreadsheet example – States
Download the XSL stylesheet – Chart.xsl

Good luck and have fun.

Posted in SharePoint | Tagged , , , , , , , , , , | 3 Comments

SharePoint Conferences

Sorry if I have been a bit slack lately but hope to get back to some more posts soon.

In the meantime I have just been confirmed as speaking at the SharePoint Conferences in:

Sydney on 10 & 11 April 2013
Auckland on 16 & 17 April 2013

I will be presenting a couple of sessions on Information Visualisation so hope to see you there.

Dave

See below links to the presentations. Blog on Excel REST to follow soon.

SPC 2013 – Power View – Dave Paylor

SPC 2013 – Information Visualisation – Dave Paylor

Dave

Posted in SharePoint | Tagged , , , , , , | 6 Comments

CQWP and XSLT – Part 1

Previous posts have covered how XSLT can be used with List and Data View web parts.  The CQWP is a great tool to roll up data from multiple sites across a site collection though can also be used against a single list if you prefer.  Another huge advantage is the ease with which it presents data from lists on other sites and as far as I know it is the only standard web part which will respect item level target audiences.

So, some great reasons to make use of the CQWP, but how do we use XSLT to change how the information is presented?

To do these steps I am going to work in SharePoint Designer 2010.

Prepare the files

The standard CQWP uses three XSLT files which are located in /Style Library/XSL Style Sheets:

ContentQueryMain.xsl – acts as the opening template in normal XSLT and calls all other templates as needed

Header.xsl – manages formatting for group headers

ItemStyle.xsl – contains the output styling templates for the returned items

In most cases you can probably get away with just customising the ItemStyle.xsl though in this example I am also going to use the ContentQueryMain.xsl ready for later blog posts.

All XSLT files used with a CQWP need to be in the Style Library.  They can be placed in sub-folders but must be within the Style Library or all you will get is an error message.

Create a sub-folder under the Style Library called Custom (this can be anything you like).

Copy the ContentQueryMain.xsl and ItemStyle.xsl and paste them into your new sub-folder.

Edit the CQWP

Next thing is to get the CQWP to use our copied files.

Add a standard CQWP to a page (note that I usually just create a blank ASPX page in Site Pages).  If you wish your new CQWP to default to a particular list or have a specific filter etc then you can set this up now if you wish through the web part properties.  When ready, select the CQWP and you should get a new ribbon tab called Web Part Tools, click on this and under the Save Web Part section select To File.

Web Part Tools

Web Part Tools

Choose a suitable name for your file and save to disk.

You can now delete the CQWP from your page.

In SPD, select File -> Pages -> Open and navigate to the file you just saved.  This will open up as an XML file – fancy that, the inside of a CQWP is XML.

Most of the properties can be changed here as needed but for now we are going to keep it simple.

I usually change the Title property so it appears nicely in the web part gallery.

<property name=”Title” type=”string”>My Custom Content Query</property>

The other two we need to change will tell the web part to use our new XSLT files.

ItemXslLink is around line 46.

<property name=”ItemXslLink” type=”string”>/Style Library/custom/ItemStyle.xsl</property>

MainXslLink is around line 71.

<property name=”MainXslLink” type=”string”>/Style Library/custom/ContentQueryMain.xsl</property>

Note that you do not need any quotes and it will happily accept spaces in the link.

Save your file.

Load up the web part

Navigate to the /Style Library/Custom folder that you created earlier and click Import.  Select your web part file and upload.  From here you can drag your custom web part onto your ASPX page.

Alternatively you can upload the web part to the web part gallery which is accessed through the browser from Site Settings -> Galleries -> Web Parts.  Once in the web part gallery then your web part can be simply added to a page the same as any other.  However, I usually make sure it is working and I have no further changes using the previous method before I add it to the gallery.

Drag or add the CQWP to your ASPX page and preview it.  This should work the same as any standard CQWP.  If not then check back to confirm you have carried out all the steps correctly.

If you open up the web part properties then you should see the title is now whatever you set it to.

CQWP Title

CQWP Title

In Part 2 we will start to look at customising our XSLT files.

Posted in SharePoint | Tagged , , , , , | 3 Comments

Customising SharePoint People Search with XSLT

One of the clients I have been working with recently wanted to extend the normal people search in SharePoint with the ability to track staff who are First Aiders.  Exactly the same concept can be applied to other roles such as Fire Wardens, JPs and Health & Safety Representatives.

People Search First Aid

User Profile Service

The first thing we need to do is add a field to our User Profile properties.  Go to Central Administration -> Manage Service Applications -> User Profile Service -> Manage User Properties and add a new property.  I called mine FirstAid and made it a Boolean field, then allowed it to be edited by the user and shared with everyone.

User Profile Service First Aid

User Profile Service First Aid

Good start.  Now users will be able to set the checkbox themselves to indicate that they are first aid qualified.

Managed Property

Before we go any further, run a full crawl to make sure the search service knows about our new field.

Then head to Central Administration -> Manage Service Applications -> Search Service -> Metadata Properties and create a New Managed Property.  To keep it consistent I called this FirstAid again, set it to Yes/No (to match the Boolean) and then Add Mapping to the People:FirstAid field we have just created.

First Aid Managed Property

First Aid Managed Property

Make sure you tick the last checkbox to add the managed property to custom results.

Another full crawl should now make sure the property is ready to be used in our search results.

Custom XSLT

Now we need to add our field to the people search results page.  I decided to display this as an icon so I added a green cross image to a library to represent my first aiders.

Navigate to your people search page, by default this will be peopleresults.aspx in your search centre.  Edit the page through the browser and edit the People Search Core Results web part.

There are a couple of things we need to change here.  Firstly, we need to include the field in the results which are returned.  Under the Display Properties section we need to edit the Fetched Properties – you may need to un-tick Use Location Visualisation – then add in <Column Name=”FirstAid” /> – the order is not important.

Finally we need to display our icon for those users who have indicated that they are First Aiders.  You have a few options how to achieve this but I am going to suggest what I consider to be the most flexible.

Click on XSL Editor. . . and you will be presented with an extended text box containing the XSLT which is used to present the default people search.  Press CTRL-A and then CTRL-C to select it all and copy it.  In SharePoint Designer 2010 (or your preferred XSLT editor) create a new XML file and rename to PeopleSearch.xsl.  Edit this file and CTRL-V to paste in the copied XSLT.

I am not going to explain this file in detail – perhaps that is another blog post – but for now search for <div id=”ContactInfo”>

Insert the following just before it:

<xsl:if test=”firstaid = ‘True’”>
<img
src=”/imagelibrary/firstaid.png”
title=”First Aider”
alt=”First Aider”
style=”cursor:pointer”
onclick=”location.search=’?k=FirstAid:1’”
/>
</xsl:if>

This does two things:

  1. Displays the chosen image if the first aid checkbox is ticked in the user profile
  2. Refreshes the page to show all first aiders if a user clicks on the icon

Save the XSLT file and grab the path to it.  Return to your people results page and under the Miscellaneous section paste the path into the XSL Link.  This will override anything in the XSL Editor and means that in future you can make further modifications without editing the page itself.  It can also be reused on multiple pages if needed.

As a nice addition, you may also like to add the image as a fixed icon on the search page so users can find first aiders simply by clicking on it.

Note that an incremental crawl will need to run before any changes users make to their profiles appear in search results.  I would definitely recommend communicating this to the user population as it will avoid confusion and repeated queries.

Posted in SharePoint | Tagged , , , , | 4 Comments

Time Management

I need to confess that I have been slacking lately which you may have noticed from the lack of recent posts.  Having just completed a great time management training session by Peter Cook, http://petercook.com/ it has reminded me that I need to make time for my blogging – so you have Pete to thank for this one.

One of the clients I have been working with recently wanted to extend the normal people search in SharePoint with the ability to track members of staff who are First Aiders.  Exactly the same concept can be applied to other roles such as Fire Wardens, JPs and Health & Safety Representatives.  I have drafted a post and just need to tidy it up with some images etc so expect this in the next day or so.

Posted in Uncategorized | Tagged , | Leave a comment

XSLT Dashboard

Creating a dashboard in XSLT is reasonably easy and yet is one of the most popular things.

I am going to create a dashboard based on a list. For this example I am going to use a list of projects and add KPIs for Budget, Progress and Overall. To keep it really simple these are going to be subjective KPIs – so set manually – but the dashboard concept would work just the same if you were using some dynamic way to set the KPIs.

So create a custom list first with the following columns:

Title
Rename to Project
Single line of text

Budget
Choice (Red, Yellow, Green)

Progress
Choice (Red, Yellow, Green)

Overall
Choice (Red, Yellow, Green)

Now add a few rows of data and set the choice fields to be a collection of each colour.

With any luck you now have something that looks like this:

Dashboard List

Now for the fun bit. One thing I have been asked for a couple of times is a template to get started with XSLT so see the attached StarterTemplate.xsl which is completely stripped back to basics and should be OK for pretty much any SharePoint list XSLT.

StarterTemplate.xsl

To download, right click on the link above and select Save Target As – you may want to change the extension to .XSL instead of .PDF – seems that WordPress does not allow XSL files.

For those who like the result without the effort I have also added a complete Dashboard.xsl which you should be able to save to a library in your site and use as is – assuming you have not changed the column names of course. This includes a bit of CSS to improve the look and feel which I have left out of the code on the page here for simplicity.

StarterTemplate.xsl

To download, right click on the link above and select Save Target As – you may want to change the extension to .XSL instead of .PDF – seems that WordPress does not allow XSL files.

So for this example we are going to use 3 templates within our XSLT.

Main Template – to set up our HTML table with a header row and then call another template to display each row.

<xsl:template match=”/”>
<table>
<tr>
<th>Project</th>
<th>Budget</th>
<th>Progress</th>
<th>Overall</th>
</tr>

Don’t close the table just yet as we now need to display each of our rows:

<xsl:for-each select=”$Rows”>
<xsl:call-template name=”RowView” />
</xsl:for-each>

And now close the table:

</table>
</xsl:template>

That is our Main template all done.

RowView Template – to create a row for each list item, display the project name and then call another template to display an icon for each KPI.

<xsl:template name=”RowView”>
<tr>
<td>
<xsl:value-of select="@Title" />
</td>
<td>
<xsl:call-template name="DisplayKPI">
<xsl:with-param name="Colour" select="@Budget" />
</xsl:call-template>
</td>
<td>
<xsl:call-template name="DisplayKPI">
<xsl:with-param name="Colour" select="@Progress" />
</xsl:call-template>
</td>
<td>
<xsl:call-template name="DisplayKPI">
<xsl:with-param name="Colour" select="@Overall" />
</xsl:call-template>
</td>
</tr>
</xsl:template>

Note that we pass the KPI across to the DisplayKPI template as a parameter. This technique saves us having to write the same code 3 times for each KPI.

DisplayKPI Template – to display an icon for each of our KPIs

<xsl:template name=”DisplayKPI”>
<xsl:param name="Colour" />
<xsl:if test="$Colour = 'Green'">
<img src="/_layouts/images/kpidefault-0.gif" alt="$Colour" />
</xsl:if>
<xsl:if test="$Colour = 'Yellow'">
<img src="/_layouts/images/kpidefault-1.gif" alt="$Colour" />
</xsl:if>
<xsl:if test="$Colour = 'Red'">
<img src="/_layouts/images/kpidefault-2.gif" alt="$Colour" />
</xsl:if>
</xsl:template>

Calling this template from RowView simply displays whatever we include in the DisplayKPI template in the place that we call it from.
I have used standard icons from SharePoint but of course you can change the images to any that you prefer.

As mentioned above, for the sake of simplicity I have left out any CSS but the Dashboard.xsl includes some inline CSS to get it all nicely aligned.

All that is needed now is to link your XSLT to a list view. Add a list view web part to a page through the browser and edit the web part. Under the Miscellaneous section you will find a field for XSL Link – add a path to your XSLT file and click OK.

Dashboard

And that is done – you now have a nice dashboard from a list – easy huh?

Posted in SharePoint | Tagged , , , | 8 Comments

SharePoint List Risk Matrix using XSLT

Representing risks in a matrix or heat map type view is pretty common so lets take a look at how we can do that in SharePoint.

First of all we need to capture our risks in a list

  1. Create a new custom list and rename the default Title column to Risk to capture a label for our risk
  2. Create a new Choice field called Consequence and use the following options:
    1. Minor
    2. Moderate
    3. Severe
    4. Major
    5. Catastrophic
  3. Create a new Choice field called Likelihood and use the following options:
    1. Improbable
    2. Rare
    3. Unlikely
    4. Possible
    5. Likely
    6. Almost
    7. Certain
  4. Create a new Calculated column called ConsequenceScore and use this formula:         =IF(Consequence=”Minor”,1,IF(Consequence=”Moderate”,3,IF Consequence=”Severe”,10,IF(Consequence=”Major”,30,100))))
  5. Create a new Calculated column called LikelihoodScore and use the following formula: =IF(Likelihood=”Improbable”,0.001,IF(Likelihood=”Rare”,0.01,IF(Likelihood=”Unlikely”,0.03,IF(Likelihood=”Possible”,0.1,IF(Likelihood=”Likely”,0.3,1)))))
  6. Both of the above should return a number with no decimal places
  7. Create a new Calculated column called RiskScore with the following formula:  =ConsequenceScore*LikelihoodScore

When you now add a new item to your list you should get a form like this to capture the name of the risk and choose the Consequence (sometimes called impact) and Likelihood (sometimes called probability).

New Risk Form

New Risk Form

Good start but all we can see at the moment is the usual list of letters and numbers.  To make it look pretty we need some custom XSLT.  If you are not familiar with XSLT then take a quick read through my previous little mini series of blogs.

Today I have made it easy and added the files for you to download:

RiskMatrix.xsl

RiskMatrix.css

To grab the files, right click and Save Target As. Note that both of these have a PDF extension which should be removed (rename from RiskMatrix-xsl.pdf to RiskMatrix.xsl.  If anybody knows an easier way to add files for download then please let me know!

The XSL file includes a link to the CSS file.  I have assumed you will put both files in /Style Library/custom.  If this is different then you will need to change the path to the CSS file in the XSL file – it is near the top and commented so will be easy to find.

Now the easy bit.  Add your list as a web part to any page and edit the web part.  Under the Miscellaneous area type in the path to your XSL file (/Style Library/custom/RiskMatrix.xsl) and click OK.

XSL Link

XSL Link

You should now have a nice Risk Matrix – congratulations!

Risk Matrix

Risk Matrix

I will be presenting this and more at the Australian SharePoint Conference on Wednesday 21 March 2012 in Melbourne so do come along and say hello.

Posted in SharePoint | Tagged , , , , , , , | 7 Comments