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

Users do not show up in SharePoint People Search or People Picker

I had this issue with people picker in a classic mode web application in SharePoint 2013 and this site is in 2010 mode - users in certain sub-domains would not show up in People Picker. I was aware of stsadm commands to fix this and we ran the stsadm command to hook up people picker with another domain some time back. The latest issue was that people picker was not returning users from the root domain and few sub domains. After researching on the internet I found (contrary to my thoughts) that we could use PowerShell and not just stsadm to map People Picker to domains. It is a good idea to first check what domains are added/mapped to the web application using the following commands: $wa = Get-SPWebApplication -Identity http://mywebapp.com #List the Domains $wa.PeoplePickerSettings.SearchActiveDirectoryDomains This will list the domains currently People Picker is looking up for that web application. I used the following script to map our AD forest to People Picker: ...

Page layout HTML changes not reflected in associated aspx

I had this issue for quite sometime with a SharePoint Online project I was working on. I use Design Manager to create a new layout page and add snippets to the html layout. I would create webpart zones, add my webpart snippets on to the html, save and publish but the resulting aspx page wouldn't have any of my webparts in it. I checked it from SPD 2013 and everything looks perfect, I can see all the code snippets in there. It was really strange and I had no clues. I had some content search webparts in the layout and one content editor webpart with a link to a text file with some css in it. After countless hours on the internet I read about few other people having similar issues when they had custom css on the layout pages to hide quick launch. I tried removing my content editor webpart and everything seems to be normal. A new page created using the layout had all the webparts in it and even the layout preview was displaying fine. I needed this css somewhere on the page and ...

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 ...