Friday, July 16, 2010

Using View Styles to add document link to Title column

First of all, do not do anything in the following post. It is unsupported and most likely fall under the "Not Best Practices" column.

OK, if you are still reading you must be a rebel and are willing to take a few risks in order to satisfy your users. Read on, but be sure to back up any files that you change, and document the process so that you can repeat it when you upgrade because these changes will definitely be overwritten by Service Packs and upgrades.

Still with me? OK, so here we go...

One of the frustrations I have is that the maximum length for an URL is 255 characters and so it is wise make the names of files in document libraries as short as possible. In order to do this, I routinely drop spaces and vowels in the names of files. While the file name is still readable, it usually ends up a little confusing for users who are browsing my document libraries.

The answer of course is to use the Title field to provide a nice descriptive title for the document. Great answer, however someone failed to provide a Title field that is linked to the document. I find it less than satisfactory that I need to display the Name field (linked to document), or rely on the user to figure out that the Icon is linked to the document in order to provide the user with a link to the document.

Andrew Connell describes a method through which he adds a new site definition and then within this site definition, he creates a new column for creating a link that opens in a new window.
His method could quickly be modified to create a linked title column.

If you are like me however, you already have sites created, and you want to add the linked Title to existing document library views.
The approach that I will describe below uses a custom View Style to modify the existing Title field as it is being rendered in order to encapsulate it with an <a> tag linking to the document.

Background

Each Document Library view uses a View Style to define how the view is
rendered. OOTB view styles are:
  • Basic Table
  • Document Details
  • Newsletter
  • Newsletter, no lines
  • Shaded
  • Preview Pane
  • Default
These View Styles are defined in a file called: VWSTYLES.XML that is found in the 12 hive at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL\XML.

Before you make any changes to this document, MAKE BACKUP COPIES. I highly recommend that you add the file to Source Safe or some other code repository under a SharePoint Customization project. If you mess this file up and do not have a backup, you will be sad, and possibly unemployed.

OK, now that you have safely backed up the VWSTYLES.XML file you may proceed. Open the VWSTYLES.XML file in both a browser window, and a text editor such as notepad. The browser is a great place to read the file because it allows you to collapse different elements and to easily view the hierarchy of the file.

The OOTB VWSTYLES.XML file defines 14 view styles, each with a unique ID.
You can easily create a view style by copying the CAML for an existing view style using the text editor, and then making changes to suit your needs. For this example, copy the the Basic Table view style ( ViewStyle, ID="0" ) to use as a template and paste it at the bottom of the file, just before the closing </ViewStyles> tag. Be sure to get all the CAML code from the opening <ViewStyle ID="0" ... tag and up to and including the closing </ViewStyle> tag.

Since each view style must have a unique ID, change the ID in the new view style from 0 to 50. You will also want to change the DisplayName attribute to "Default with Linked Title", the resulting <ViewStyle> tag should now read as follows:
<ViewStyle ID="50" DisplayName="Default with Linked Title" Preview="_layouts/images/prvbasic.gif" Description="$Resources:core,ViewStyleBasicTableDesc;">
You also need to set the ID value in the g_RequiredFields array declaration found in the <SCRIPT> element. The <Script> element is the last child element of <ViewStyle>.

<Script>g_RequiredFields[50] = new Array;</Script>
While you are in this section, add the Title field as a required field by
adding the following line:
g_RequiredFields[50] ["Title"] = true;
The final script tag should read as:
<Script>g_RequiredFields[50] = new Array; g_RequiredFields[50] ["Title"] =
true;</Script>
Save your changes and run iisreset on your SharePoint Front End server. Open
up a Document Library and either create a new view, or edit an existing view. The new View
Style should now appear in the list under the Styles section as show in the
following example.


Adding to Code to Create a Linked Title Field

So far all we've done is created a new view style that is identical to the Basic Table view style. In order to make the desired changes, we'll have to go a little deeper into the child elements of the <ViewStyle> element.

The following shows the hierarchy of the ViewStyle element that we copied
from the Basic Table ViewStyle with the <ViewBody> and <Fields> elements
expanded:

  • <GroupByHeader>
  • <GroupByFooter>
  • <ViewHeader>
  • <ViewBody>
    • <HTML>
    • <GetVar Name="AlternateStyle" />
    • <HTML>
    • <IfEqual>
    • <Fields>
      • <HTML>
      • <FieldSwitch>
      • <HTML>
      • <Field/>
      • <HTML>
    • </Fields>
    • <HTML>
  • </ViewBody>
  • <ViewFooter>
  • <PagedRowset>
  • <PagedClientCallbackRowset>
  • <PagedRecurrenceRowset>
  • <ViewEmpty>
  • <Script>

The ViewBody element defines how the rows of data are going to be rendered. Feel free to investigate the different elements, but for this post, we'll be focusing on the <Fields> element.

Within the <ViewBody> element, locate the Fields child element. The Fields element is used in rendering the list for the browser. When used inside a view, it iterates over the view fields.

Within this element, we want to introduce some logic that will add the document link while iterating over the Title field.

Modify the Fields element to add the IfEqual structure shown in the following example:

  • <Fields>
    • <HTML>
    • <FieldSwitch>
    • <HTML>
    • <IfEqual>
      • <Expr1><Field Name="Title" /></Expr1>
      • <Expr2><Field/></Expr2>
      • <Then>
        • <HTML><![CDATA[<a href="]]></HTML>
        • <Field Name="EncodedAbsUrl"/>
        • <HTML><![CDATA[">]]></HTML>
        • <Field/>
        • <HTML><![CDATA[</a>]]></HTML>
      • </Then>
      • <Else>
        • <Field/>
      • </Else>
    •  </IfEqual>

  • </Fields>

Save your changes and run iisreset on your SharePoint Front End server. Open up a your Document Library and either create a new view, or edit an existing view. Set the View Style to Default with Linked Title, click Ok and voila, you should now see the document titles displayed as hyperlinks to the document.

Conclusion

As stated previously, the changes that you make to the VWSTYLES.XML file will be overwritten when you apply Service Packs or upgrades to SharePoint. It is very important that you not only make backups of the original file, but also backups of the file after you have made these changes. When, not if, VWSTYLES.XML is overwritten during an update, you will be able to retrieve the backup and restore this functionality.

It is also recommend that you add this procedure to your SharePoint deployment configuration documentation in the form of release notes. These notes should be easy to follow so that anyone in your organization can make these updates after you have won the lottery and moved to Belize : )