Thursday, October 23, 2008

Remove link on Lookup Field

After creating List view webpart programmatically we are facing another problem.

For creating list view web part programmatically please check this link.

Scenario:
we have a Master list were the master data like Types, Departments this kind of value are stored.

As we are using custom list view webpart and using lookup filed for this data.

Now our problem is removing link of the lookup fields because it will redirect the user to that master list which we don’t want.

We are also using some other lookup fields which are referring to the title of some other list like contact name or company details etc... But we have to keep that link.

So simple problem statement is
1) remove link from lookup field
2) But keep link of lookup which are mapped with LinktoTitle.

Solution:
We found that if you are selecting any look up field then other then LinktoTitle then your lookup URL will have something like this.
<<your site>>/Lists/<<List name>>/DispForm.aspx?ID=<<ID of that field>>&RootFolder=*

So we can find out that if Link contain RootFolder=* then it is referring to the lookup.

To removing this, what we had done is that something unethical.

We don’t know whether this is correct way or not.
We resolve this problem from java script.

In java script we find all the Anchor element of the document.

Then check the index of href attributes “RootFolder=*” so that we came to know that in this page this field is lookup field.
Just call this java script on the page where you want to remove the link of lookup.

You can use content editor webpart also.

Here is the java script to remove the lookup links.
<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("RemoveLookupLinks");
function RemoveLookupLinks()
{
var oP = document.getElementsByTagName('a');//the collection of <a> tags
var flag = false
for(var i=0;i<oP.length;i++)
{
if(oP[i].attributes["href"].value.indexOf("RootFolder=*")!= -1)
{
var linkvalue = oP[i].innerHTML;//value of the lookup field
oP[i].parentNode.innerHTML = linkvalue;//replacing value of the lookup to whole the Anchor tag
flag = true;
break;
}
}
if(flag)
RemoveLookupLinks();
}
</script>

Check it and let us know that it is working for you guys,

We have not done it for LinkToTile lookup link because the problem with RootFolder=* is that it will not redirect back to this page while LinkToTitle will redirect to the same page from where you traverse.

It solves our problem for the time being (until another requirement will come)

34 comments:

sayitfast said...

man.. I have been looking for somehting to solve this exact issue...

THANKS!

Anonymous said...

That is wonderful. Thank you very much!

Nickname unavailable said...

We have run into a slight problem this does not appear to be working in FireFox

Anonymous said...

WOW, thanks a LOT... Thats exactly what I was looking for! :)

El Duddz said...

Found a small problem if you have multiple choices on the lookup fields, only the first choice was shown.

so I put in a small fix
Replace:

var linkvalue = oP[i].innerHTML;//value of the lookup field

With:

var linkvalue = oP[i].parentNode.innerHTML;
var re = new RegExp("<[aA][^>]+>","gi");
linkvalue = linkvalue .replace(re,"");
var re2 = new RegExp("]*>","gi");
linkvalue = linkvalue .replace(re2,"");

My regex isnt great so there is probably an easier way.

El Duddz said...

Sorry,

the 2nd regex pattern should have read:
var re2 = new RegExp("</[aA][^>]*>","gi");

Think i forgot to encode the brackets

Anonymous said...

This is so cowboy.

Anonymous said...

The script won't work if the view is grouped by the column with the lookup field. The logic also removes the hyperlink to expand/collapse the grouped view.

I found the following blog that works either way.
http://www.myfatblog.co.uk/?p=106

Anonymous said...

Thanks a lot, this fixed a big problem...

Anonymous said...

keep it up, tks

Anonymous said...

Wonderful. If you were not already kings, you earned your crown with this one.

Jayvardhan Patil (Jay) said...

There is an easier way available I think.

In my case, I modified XSL tag to get the value I wanted.
Check this:

Parth Patel said...

Hay Jay, can you pest link again. :)

$!m said...

OMG you are awesome!!

Thank You Thank You !

Anonymous said...

It is not working with FF!

Replace:
if(oP[i].attributes["href"].value.indexOf("RootFolder=*")!= -1)

With:
if(oP[i].href.value.indexOf("RootFolder=*")!= -1)

To support both FF and IE :)

SharePoint Kings said...

Hay Anonymous,

Thanks for your tip.

Indrakeela said...

Really Im new to sharepoint. I don't know where I want to put the above java script?
Please help me to solve it....

SharePoint Kings said...

Indrakeela,

put this script in content editor webpart above your listview webpart

Le Sman said...

hi King, thanks for your code. It worked tho' still got a lil issue. It removed all the other lookup items i put in the field even if it did remove the links as well. I mean i have a country lookup field. So it removed other countries in the lookup field and leaves only the 1st country. Pls how do i sort this out. thanks.

Ola.

SharePoint Kings said...

Le Sman,

it will remove all the link as it was coded.

so no idea regarding your requirement

Aquila Sands said...

An alternative way to remove the links that won't break multi value lookup fields and doesn't require regex.

_spBodyOnLoadFunctionNames.push("RemoveLookupLinks");
function RemoveLookupLinks() {
var links = document.getElementsByTagName('a');
var flag = false
var elem = document.createElement("span");
for(var i=0;i<links.length;i++) {
if(links[i].attributes["href"].value.indexOf("RootFolder=*")!= -1) {
var linkvalue = links[i].innerHTML;
elem.innerHTML = linkvalue;
links[i].parentNode.replaceChild(elem, links[i]);
flag = true;
break;
}
}
if(flag)
RemoveLookupLinks();
}

SharePoint Kings said...

Aquila Sands,

Thanks a lot, this helps a lot.

Anonymous said...

Thanks for marvelous solution. Work like charm, but I have a question though, wouldn't it be some hit on performance, looping so many elements.?

SharePoint Kings said...

yes if you have thousands of data, which is unlikely because in that case SharePoint itself will be slow,
but yes for lot of data it will be a little performance hit.

this script if fire @ client side so not much impact as well

Anonymous said...

You should be able to use this function that I just created and tested:
removeLookupLinks=function(){
var a=document.getElementsByTagName('A'),o;
for(var i=a.length-1;i>=0;i--){o=a[i];
if(o.href&&o.href.indexOf('RootFolder=*')!=-1)o.removeNode()
}
};

It basically iterates in reverse to avoid DOM node removal exceptions. The 'removeNode([false])' method removes the <a> tag without removing the text node within it ('true' would remove it).

Aquila Sands said...

@Anonymous October 27, 2011 5:27 AM. Very nice, that's a really elegant solution. I'll be using that from now on.

General_Mayhem said...

This is great, I've been looking for a solution to this for ages. Thanks very much.

Unknown said...

Hey your code is awesome and I appriciate that. But instead of removing link i want to open that all link in to different window rather to navigate that in to page...I want to open in to different opo up window do you know how can i do that

SharePoint Kings said...

@Unknown,

yes you can change that a(anchor) tag by adding attribute target='_blank' which will open link in new window.

Tracey said...

Is there a way to get this working in a grouped list view?

Walter said...

How would we modify the script so that the links for selected column are removed, yet leaves other columns with the OOTB behavior?

SharePoint Kings said...

Tracey,

to get group by refer this link http://www.sharepointkings.com/2011/05/how-to-get-group-by-html-in-sharepoint.html

SharePoint Kings said...

Walter,

what exactly you are looking for?

Anonymous said...

THANK YOU!!!!!!!!!!!!!




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com