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

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

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:

Report Server has encountered a SharePoint error. ( rsSharePointError)

I was receiving this error on the "Report Server Web Service URL" on our SharePoint farm: Report Server has encountered a SharePoint error. ( rsSharePointError) Access to this Web Site has been blocked. Please contact the administrator to resolve this problem. This site URL is configured via Central Administration > Configure Reporting Services Integration > Reporting Services Integration if Reporting Server feature/Add-in is installed on the farm. After doing some research I found out that this page somehow enumerates through entire site collections before it throws this error. As part of troubleshooting step I checked if the domain user configured for reporting service is has sufficient privileges in the farm and also as local admin of the sql box, but this did not solve the issue. We raised this issue with the vendor and they made us update our SQL Server 2005 to latest SP and CU; still we had the error. The reason was nobody was sure if this er