Modify Column Headers in XSLTListViewWebPart (SP2010)

The Situation

You would like to use an out-of-the-box view of a list. But, the list’s column names are not the most end-user friendly. You’d like to modify the column names without losing any of the functionality of a list view web part, such as sorting and filtering. While you theoretically can use SharePoint Designer 2010′s design view to change the column header text, I’ve found it to be fairly unreliable. In other words, when I’ve tried it, I’ve had less than successful results. Fortunately, it’s easy to modify the XSLT to accomplish what we want to do.

The Set Up

In my example, I’m using the “My Tasks” view of a task list that’s associated with an approval workflow. I want my users to be able to quickly get to the the item they need to approve before being asked to approve or reject it. The column name for the associated item is “Related Content” – not exactly an intuitive title for most users. Since I know that in this site, the only thing stored in this task list is the tasks for the approval workflow, I can make this view more specific. We’re going to change “Related Content” to “Review Funding Request” and change “Title” to “Approve/Reject Request.”

To follow along you can use the out of the box approval workflow on any list, using the task list to track tasks.

The Step-By-Step

Assuming you’ve set up your approval workflow, the first step is to get the “Related Content” column in our “My Task”s view. The “My Tasks” view is an out-of-the-box view on the task list. So, navigate to your tasks list, switch to the “My Tasks” view and choose “Modify this View.”

Modify the My Tasks View in the Browser

Modify the My Tasks View in the Browser

Add the “Related Content” column to the view. I made it the first column.

Add the Related Content column to get the link to the associated item

Add the Related Content column to get the link to the associated item

Save your view and open the site in SharePoint Designer 2010. Navigate to the Tasks list, and choose to edit the the “My Tasks” view. Switch to the Design tab and choose to “Customize XSLT” and select “Customize Entire View.”

Switch to Design Tab and Choose to Customize the XSLT

Switch to Design Tab and Choose to Customize the XSLT

Behold you will now have a couple thousand lines of XLST code in your code view. Don’t panic! Most of it can be ignored. You are going to do 2 things – add a custom template, and replace 18 instances of a specific line of code with a call to your custom template. (Find and Replace in SPD2010 is awesome.)

The Custom Template

Basically, instead of just outputting the field’s title, we want to output our custom text if the current field title matches the one we’re trying to replace. I’m replacing 2 field titles, so I’m passing in the current field title as a parameter, and then using a choose statement to do the comparison. If the current field title matches either of the 2 I’m replacing, I return my custom text. If not, I return the field title. Pretty simple. (Note that I begin the name of my custom XSLT templates with “CES” because that’s the organization I work for. You should have your own naming convention that ensures your template names will not conflict with Microsoft’s.)

<xsl:template name="CESDisplayCustomTitle">
<xsl:param name="fieldtitle" />
<xsl:choose>
<xsl:when test="$fieldtitle = 'Title'" >
<xsl:value-of select="'Approve/Reject Request'" />
</xsl:when>
<xsl:when test="$fieldtitle = 'Related Content'">
<xsl:value-of select="'Review Funding Request'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$fieldtitle" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

I put my custom templates at the end of the existing XSLT code. To find the right spot, put your cursor in the code view in SPD2010 and search for

</xsl:stylesheet></Xsl>

Insert the custom template just before this code.

(Note: don’t be shocked when you open the listview later and your code has moved. SPD2010 does that. No worries.)

Now, we need to call our template. You’re looking for the following code to replace:

<xsl:value-of select="$fieldtitle" />

And you’re going to replace it with this code:

<xsl:call-template name="CESDisplayCustomTitle">
<xsl:with-param name="fieldtitle" select ="$fieldtitle" />
</xsl:call-template>

(Note: Update the template name to match your custom template name. Also, ┬ámake sure to highlight all of the XSLT EXCEPT your custom template when you do your find and replace selection. If you do the find and replace and include your custom template, you’ll get an infinite loop error.)

You should be able to run a find and replace procedure, and then click into the design view to have SPD2010 update the design view with your new column headers.

Perform the Find and Replace

Perform the Find and Replace

And, the finished view of the list, with all functionality intact.

New Column Headers with all Functionality Intact

New Column Headers with all Functionality Intact

Caveat

This doesn’t work for attachment columns. But, it would be easy to handle those, as well. Attachments are output with disable-output-escaping=”yes.” You’d need to account for that in your custom template and then replace all the instances of:

<xsl:value-of select="$fieldtitle" disable-output-escaping="yes"/>

with your call to the custom template. I’m not changing that column name, so I didn’t include that.

Happy coding!

About these ads
Leave a comment

6 Comments

  1. Hi Deanna

    I can see that this will work if you are in the list, and you are viewing the modified view.

    I have xsltlistview webparts on the main page of my sharepoint site, and need to be able to change the column headings on these webparts. In this scenario I have not been able to get the column names to update by changing the xsl of the view…

    Any ideas on how to get this to work?
    Mahendran Govender

    Reply
    • You’d follow the exact same steps. One you put an xlstlistview webpart on the mainpage, you’ll still have access to edit the XSLT via SharePoint Designer, just like you would on the page itself. The one thing to remember is that when you do your find/replace, you don’t want to replace the instances of


      that are in your custom template, or you’ll get an infinite loop.

      Reply
      • Hi Deanna

        Thanks for responding! :)

        Do you mean editing the xslt of the webpart itself?

        I tried editing the view, and then assigning the view to the webpart, but that had no effect…the headings displayed on the webpart were the old ones.

        M

        PS. I am Very impressed with the amount of detail in your posts!!

      • Thanks. I try to do the posts so that I can remember what the heck I was thinking when I have to do it again. :)

        What I mean is, just add the xlstlistview webpart to your main page, choosing whichever view you want to show. Open that page in SPD. Click on the webpart itself. Click on “Design” in the SPD ribbon. Click on “Customize XSLT.” Now follow the same steps as if you were on the list page. I’ve found that the customized XSLT doesn’t really stick with the view when you move it from page to page. You could do the XSLT externally, though, if you want to use the view on multiple pages. That’s covered in other people’s blogs, so I’m not going to repeat that here. Here’s a basic post on it:

        http://www.glynblogs.com/2011/04/overriding-the-presentation-of-an-xslt-list-view-web-part.html

        You’d, of course, need to save your customized XSLT instead of just pulling in the main and internal ones.

      • Thanks!

        I will give that a try tomorrow :)

        M

  2. Thanks ,
    you know the problem is that SPD2010 very often show an error message that

    ….

    i know very easy solution .
    when you click on the header in design mode in SPD2010

    there is an option format as (Text or Label ) you can choose either one.

    Type the text you want

    you will have all functionality like sorting .

    but sometimes if change the header text without this option you will have NO sort.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: