Skip to main content

XSLT - Learned the hard way

I never really cared about learning or using XSL and XSL style sheets in my projects. But then I was wrong, I should have mastered this, it is an important weapon in a SharePoint consultant's arsenal.

In this blog post I try to note down my experience with XSL so that I can refer back to this again later.

1. If you see characters like ampersand (&) appearing as & in your List view with custom XSL template then you got to disable output escaping.

disable-output-escaping="yes"

2. For one of my projects I had to dynamically bind a URL field in a DataView webpart. The requirement was to display different views (InfoPath) of a list based on two column values in the list. One of these values was set by a work flow.

There are different ways to do this and there may be better ways of achieving this but this is how I did it:

  <xsl:choose>
           <xsl:when test="@Status1 = 4 and @Status2 = 4">
            <a  onclick="javascript: NewItem2(event, &quot;/sites/Intranet/Lists/Change/Item/editifs.aspx?ID={@ID}&amp;DefaultView=View2&amp;IsDlg=1&quot;); return false;" href="/sites/Intake/Change/" title="{@Title}">
       <xsl:value-of select="substring(@Title,1,30)" disable-output-escaping="yes" /></a>
          </xsl:when>
          <xsl:when test="@Status1 = 5 and @Status2 = 5">
            <a  onclick="javascript: NewItem2(event, &quot;/sites/Intranet/Lists/Change/Item/editifs.aspx?ID={@ID}&amp;DefaultView=View1&amp;IsDlg=1&quot;); return false;" href="/sites/intranet/" title="{@Title}">
          <xsl:value-of select="substring(@Title,1,30)" disable-output-escaping="yes" /></a>
          </xsl:when>
        </xsl:choose>

In the above code I'm choosing between View1 or View2 based on the value of the fields Status1 and Status2.

It would have been better to just set a variable and not the entire Anchor in the xsl:when, much more to learn!

3. Format Date in DataView Webpart

The following XSL can be used to format date in DataView web part:

ddwrt:FormatDateTime(string(@SomeDate),4105,'dd-MMM-yyyy') 

To get format date and time in 12 hour format:

ddwrt:FormatDateTime(string(@SomeDate),4105,'dd-MMM-yyyy hh:mm tt')

4105 is the LCID more of which can be found here: http://support.microsoft.com/default.aspx?id=221435

4. There is no Modified By available within XSL, you have to use @Editor.title to get the display name.

5. In a DataView Webpart you could do grouping and this is how it appears on browser:


If you want to get rid of that Group By column name you need to remove the following XSL from the webpart:

<b>
<xsl:value-of select="$fieldtitle" />
</b>
<xsl:if test="$fieldtitle">: </xsl:if>
And this is how it appears after:



To be continued..

Comments

What if I didn't want to hard-code the LCID in? I want to respect the user's current locale setting (the one you can set from My Settings > Regional settings).
Manoj said…
Hi Matteo,

I'm not sure if you can call this function without LCID as a parameter. And OOB I don't see how we can get the current locale of the user for the dataview webpart.

If you can make a connection to select the date in DataView webpart you could use a Date Filter webpart and connect it to your DataView webpart

Popular posts from this blog

Difference between the architectures of SSPs and SharePoint Service Application

SharePoint 2010 has some new features and components, which help to overcome difficulties such as redundancy and service-sharing that we faced while using SharePoint 2007.  Services in MOSS 2007 were implemented with the help of Shared Service Providers or SSPs. But we cannot use the services of one SSP for another SSP. So each SSP has to have its own set of services and applications, which increases redundant data in the farm.  For example, different managers in the same organization use a SharePoint site to maintain official data associated with an SSP. This SSP is associated with different services, such as Search service, BDC service, and a user profile service. A project manager in this organization requests an exclusive user profile service to maintain project data security. To provide this, we first need to create a separate SSP for the project manager and then a separate service called user profile service - project name. Next, we need to associate the SSP with ...

SharePoint Designer Workflow Failed to Start - Cancelled by System Account

I was working on a List workflow the other day in SharePoint Designer 2010. This list already had a workflow which was working perfectly fine and I needed another manual workflow in it. No matter what I tried the workflow would fail and would log a message in the history list that Workflow failed to start and it was cancelled by System Account . I observed that the same happened with new workflows in that list, new workflows in other lists and I even tried creating new subsites and workflows were behaving the same way. I googled for a while and there were many suggestions to clear SPD cache which did not help me. I played with SPD 2010 for a while and observed that the workflow xml and xoml files were checked out. I read in some blogs that this could cause this issue as well. I tried to check those files in but then I was greeted with a new error! " Cannot perform this operation. The file is no longer checked out or has been deleted ." I had no clue what was happening...

"Cannot impersonate user for data source" - SSRS reports

We were getting this strange error while viewing our reports in SharePoint that was deployed using BIDS 2005: An error has occurred during report processing. Cannot impersonate user for data source 'datasource' Logon failed I tried many things but nothing worked. I did some research and found many blog posts which suggested me to reapply the password on the datasource file specified in the error message and save it again. That didn't work for me. I saw some check boxes in the data source file that read "Use as Windows Credentials" and "Set Execution Context to this report" and I didn't know what they meant. I unchecked those in my data source and then when the refreshed the page the report started displaying.