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:
man.. I have been looking for somehting to solve this exact issue...
THANKS!
That is wonderful. Thank you very much!
We have run into a slight problem this does not appear to be working in FireFox
WOW, thanks a LOT... Thats exactly what I was looking for! :)
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.
Sorry,
the 2nd regex pattern should have read:
var re2 = new RegExp("</[aA][^>]*>","gi");
Think i forgot to encode the brackets
This is so cowboy.
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
Thanks a lot, this fixed a big problem...
keep it up, tks
Wonderful. If you were not already kings, you earned your crown with this one.
There is an easier way available I think.
In my case, I modified XSL tag to get the value I wanted.
Check this:
Hay Jay, can you pest link again. :)
OMG you are awesome!!
Thank You Thank You !
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 :)
Hay Anonymous,
Thanks for your tip.
Really Im new to sharepoint. I don't know where I want to put the above java script?
Please help me to solve it....
Indrakeela,
put this script in content editor webpart above your listview webpart
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.
Le Sman,
it will remove all the link as it was coded.
so no idea regarding your requirement
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();
}
Aquila Sands,
Thanks a lot, this helps a lot.
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.?
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
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).
@Anonymous October 27, 2011 5:27 AM. Very nice, that's a really elegant solution. I'll be using that from now on.
This is great, I've been looking for a solution to this for ages. Thanks very much.
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
@Unknown,
yes you can change that a(anchor) tag by adding attribute target='_blank' which will open link in new window.
Is there a way to get this working in a grouped list view?
How would we modify the script so that the links for selected column are removed, yet leaves other columns with the OOTB behavior?
Tracey,
to get group by refer this link http://www.sharepointkings.com/2011/05/how-to-get-group-by-html-in-sharepoint.html
Walter,
what exactly you are looking for?
THANK YOU!!!!!!!!!!!!!
Post a Comment