Saturday, April 2, 2011

Remove hyperlink for lookup columns in SharePoint

I tried to look for how to remove the hyperlink for the lookup columns in SharePoint. I was expecting some kind of an XSL transformation. But all the posts i found in the internet were using JavaScript for this functionality.  So i decided to work on the XSLT myself. I know this is a much cleaner and efficient solution. And this is how it is done.

I have a list with a lookup column called "DeptLookup". To remove the hyperlink follow the below mentioned steps


Open this list in SharePoint designer. Click on the AllItems.aspx to open it.
Select the lookup column


Click on the formula on the ribbon
This will open a dialog for you called Insert formula
The default formula for the lookup column is

$thisNode/@*[name()=current()/@Name]

Change it to

substring-before(substring-after($thisNode/@*[name()=current()/@Name],'>'),'<')


Click on OK and save the changes. Now when you go to the list, you will see the lookup values without the hyperlinks.



If you want to remove the hyperlink for the item display form, create a new display form in SharePoint designer and follow the same procedure until you get the Insert Formula dialog.
In the dialog give the value as

substring-before(substring-after(@DeptLookUp,'>'),'<')

where DeptLookup is the name of the lookup column.

What we have done here is a very simple concept. The lookup columns are nothing but hyperlinks

<a href=something>LookupValue</a>

We have chopped down the portion after >
LookupValue</a>
From the resulting string we have removed the part after <
which will leave us with just the value we want LookupValue

4 comments:

  1. If we have single lookup item, its working. But if we have multiple item its not working

    ReplyDelete
    Replies
    1. Try to use this alternative solution: http://www.sparqube.com/SharePoint-Lookup-Column. It has option to display lookup values as links or as generic text.

      Delete
  2. Yes this logic works only for a single lookup value. If your column is configured to have multiple lookup values, this logic does not hold good.

    ReplyDelete
  3. Works well! Just what I need. Will add a slight modification. The LessThan sign needs to be escaped (at least for SP2016) with <

    Here's the code I use to remove the link from Department lookup:

    ReplyDelete