Home > SharePoint 2010 > SharePoint 2010: List column that renders as a dynamic hyperlink

SharePoint 2010: List column that renders as a dynamic hyperlink


A while back I worked on a project where we needed to add a column to a SharePoint List that would display a link based on values in other columns. For example: “/myWeb/pages/mycustomPage.aspx?Reference=[ValueToComeFromAnotherColumn] i.e. similar to the way the Title field link is rendered where the URL is dynamic and based on the ID of the row in question.

In MOSS 2007 we did this by adding a calculated column that constructed the HTML using our specified formula. However, the problem was that the link would appear as:

 <a href="myWeb/pages/mycustomPage.aspx?Reference=45">View</a>

To ensure it rendered as a nice and friendly hyperlink we used a script created by Christophe from PathToSharePoint.com (his blogs on the subject dont seem to exist anymore). It worked pretty well in MOSS 2007 but we found that in MOSS 2010 it didnt work if your list view had grouping by any column.

Initially I tried to get the script to work but then realised that there was a much better way to do this in SharePoint 2010. In 2010 you can control how any field is rendered by creating a custom XSLT stylesheet. In our scenario we created an XSL file which was deployed at “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL”. The XSL file must be named in the following format fldtypes_*.xsl, where * is any valid filename string value for example fldtypes_MyCustomDefinitions.xsl. Our XSL file looked something like this:

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:template match="FieldRef[@Name='FormLink']" mode="Computed_body">
    <xsl:param name="thisNode" select="."/>
	    <a target="_blank" href="/appeal/pages/appealapplication.aspx?ApplicationRef={$thisNode/@ApplicationRef}">View Application</a>
  </xsl:template>
 </xsl:stylesheet>

The important bit is the match attribute in the xsl:template element where we specify the internal name of the Field we want this custom rendering applied to. What this basically does is that where ever a field with an internal name of FormLink is being rendered in a view it will render it as a hyperlink. Below is the definition for our SharePoint List Field:

<Field ReadOnly="TRUE" ID="{2DD3638A-56A0-4021-B2B9-7BBB19E191F1}" Name="FormLink" StaticName="FormLink" DisplayName="Application" Type="Computed" Group="My Custom Group" SourceID="http://schemas.microsoft.com/sharepoint/v3" />

That is all there is to it, now when ever and where ever our field is rendered in a view it renders it in our custom way.

Update: For this to work, the column you are referencing must exist in the view. In my example above I am referencing a column called “ApplicationRef” therefore for it to work in my scenario the SharePoint List View I want it to work in must have “ApplicationRef” column otherwise the value wont be picked up. Hope this makes sense.

I have created a sample Visual Studio SharePoint Project that demonstrates this functionality. Please click on the link below to download it. Just deploy the solution from Visual Studio and it will create a list with a dynamic hyperlink column.

zip DynamicHyperLink.zip

[Update: 08/10/2012] Depending on your scenario you might find this simpler solution more feasible: http://trainerms.wordpress.com/2012/10/06/dynamic-hyperlinks-in-sharepoint-lists/

About these ads
  1. Robin
    July 19, 2011 at 11:47 am | #1

    could you provide a solution package .wsp for this?

    • jasear
      December 27, 2011 at 12:34 am | #2

      I have added a sample Visual Studio project.

  2. July 26, 2011 at 3:37 pm | #3

    I followed this guide, but the text is still just rendered in all views (not the link to Google as should be displayed from the xsl below). I checked and verified the internal field name. I created a calculated column “Test” to build the hyperlink using the SharePoint interface. Do I need to add the path to the file fldtypes_MyCustomDefinitions.xsl in additional config file or any specific security settings on the xsl file?

    Testing Overwriting the Column

  3. jasear
    September 8, 2011 at 7:46 pm | #4

    Hi Wendy,

    Sorry for the late response as I havent blogged in a while.

    There isnt any additional security setting and your file doesnt need to be specified in any other config file it just needs to be in that XSL folder I mentioned. SharePoint picks up all the files in that folder.

    Is your field rendering at all? If so how is it rendering? I would also try an IIS Reset as I have noticed when you make any changes to the XSL file they dont take effect until you do an IIS Reset.

  4. Anthony Poole
    January 3, 2012 at 1:38 pm | #5

    I came up with an alternative solution using a simple SPD workflow, with the following steps (set to run once when an item is created):

    1. Set a workflow variable called URL and use the string builder to create a string like this:

    http://myurl.com?ID=%5BCurrentItem:ID%%5D, FriendlyName

    2. Set the value of the Hyperlink column to equal the URL variable

    Now when an item is created in the list, the Hyperlink column will be set to the relevant URL, with a friendly name (in this case all items have the same friendly name, but you could use variables to adjust so each is unique).

    • Drew
      June 27, 2012 at 4:01 pm | #6

      The workflow approach worked perfectly for me….5 minutes and voila! Thanks Anthony.

      Drew

  5. ..
    February 6, 2012 at 4:08 pm | #7

    Hello,

    I have 2 Sites Pages in Sharepoint, with 2 different lists but with one column (ID) that join both tables.

    After I filtered page one (e.g. ID =1) I can see just one row, when I select this I would like to go to other page and see more information about this ID.

    I know that you explain above but I don’t understan how I can do it in my case.
    Sorry i’m begginer in Sharepoint.

    • jasear
      February 8, 2012 at 11:19 am | #8

      If I understand your issue correctly you want to link two lists by an ID column. Why dont you link them by using a lookup column?

      If you cant use this for some reason then just look at the pattern of your URL for the “second page” and following the post above you can construct this dynamically. For example lets say the “second page” filters the record based on the ID you pass via a query string then all you have to do in your “first page” is to construct a link that adds the Query string.

      Hope this makes sense.

  6. Pepersview
    March 1, 2012 at 10:27 am | #9

    Hi,

    I was implementing something similar in SharePoint 2010. I have an External list using BCS. Then I created an application page in visual Studio. In that page I present the BCS list and other list views depending on the user role, to avoid that the user can see the url of each view.

    ContentPlaceHolder ContentPlaceHolder = (ContentPlaceHolder)this.Master.FindControl(“PlaceHolderMain”);
    XsltListViewWebPart lvwp = new XsltListViewWebPart();
    SPList SecondList = SPContext.Current.Web.Lists["Teachers"];
    lvwp.ListId = SecondList.ID;
    lvwp.ViewGuid = SecondList.Views["Teachers"].ID.ToString();
    ContentPlaceHolder.Controls.Add(lvwp);

    Originally on the BCS External List I have added an extra column. That column has a “SharePoint Form Action Hyperlink”, it is just a link to another page, with a different Id for each of the rows. My problem is when seeing this external list on my XsltListViewWebPart I can’t see that personalized column.

    Best regards,

  7. Sepid
    March 2, 2012 at 5:04 pm | #10

    Hi,

    Would you please help me? I have a calculated column in my custom list as named “test”.
    I followed this guide, but the text is still just rendered like “http:\\servername\page.aspx?ID=3″. I checked and verified the internal field name. Ans also I added a fldtypes_custom.xsl file in the mentioned path and the restart the IIS. But It doesn’t work. What should I do? Maybe it doesn’t deploy to SharePoint 2010

    Thanks

    • jasear
      March 7, 2012 at 5:10 pm | #11

      Hi Sepid,

      It is really hard to say why it is not working for you. Did you try downloading the example solution I provided a link for in the blog post above? Does that work for you? Perhaps you can compare it to your solution to see where it has gone wrong for you?

      Regards

  8. April 24, 2012 at 2:22 pm | #12

    Hi Jasear,
    Thanks, it works.
    But what if I need to add it to existing List (that I created from the browser) ?

  9. June 24, 2012 at 2:22 am | #13

    How do you get it to work on dsipForm.aspx?

  10. Florin Florea
    October 6, 2012 at 12:29 pm | #14

    Just Edit the view for the list in SharePoint Designer, add an extra column to the list, drag and drop a LinkButton from the toolbox and add the PostBackUrl attribute like this:

    Whatever you want to display as text

    “$thisNode” identifies the current item and “@ID” identifies the column (field) you want to use as a parameter. You could use “{$thisNode/@ApplicationRef}” for the example in the article.

    • jasear
      October 6, 2012 at 2:37 pm | #15

      Florin,

      Thanks for sharing your solution. I am not sure why you laughed out loud at the other approaches because they all have their own merits and might be more feasible to use in different scenarios just as your solution might be the most feasible option in your scenario.

      • Florin Florea
        October 6, 2012 at 3:54 pm | #16

        Dear jasear,
        I can see why you and other people that come to this blog might feel offended by my post and I assure you that this was not my intention. Unfortunately, I cannot edit that post, please feel free to do so, if you want, and remove that first line. It was a case of “hand faster than the brain” thing. I offer you and the other people involved my sincere apologies.

  11. Florin Florea
    October 6, 2012 at 12:31 pm | #17

    For some reason the code for the button didn’t show.
    It is:
    <asp:LinkButton runat=”server” id=”LinkButton2{generate-id()}” PostBackUrl=”http://www.google.com?q={$thisNode/@Name}”>Adeverinta</asp:LinkButton>

  12. April 2, 2013 at 2:57 am | #18

    Hello There. I found your blog using msn. This is an extremely well written
    article. I will be sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I’ll certainly comeback.

  13. Livin
    June 3, 2013 at 11:48 am | #19

    what is dynamic list in sharepoint?

  14. joao lopes
    June 20, 2013 at 11:14 am | #20

    Is very good solution, but I need to put de hyperlink to show in the dispform.aspx.
    And I’m not able to do.
    Can you please give a example?

    thanks

  1. July 4, 2012 at 6:45 pm | #1
  2. October 6, 2012 at 4:17 pm | #2

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: