SharePoint: Remove URL from Lookup columns using JavaScript

By | January 15, 2011

If you want to remove URL from the Lookup columns in SharePoint, just copy and paste the following code into the page.

<div id="scriptdiv"></div>
<script language="javascript">
var CTXMAX = 5;
function RemoveLookupAfterPageLoad()
{
var table, i=1, ctx;
// Remove lookup links
while(i<=CTXMAX)
{
eval("if(typeof ctx" + i + " != 'undefined') ctx = ctx" + i +"; else ctx = null");
i++;
if(!ctx)
break;

table = document.getElementById(ctx.listName + "-" + ctx.view);
if(table)
{
table = table.parentNode;
table.innerHTML = RemoveLookupLinks(table.innerHTML);
}
}
}

function CreateDynamicFunction()
{
// Create a function "ExpGroupCallServerg_GUID"
var scriptdiv = document.createElement("div");
var i=1, funcExpGroup ="", ctx, val;
if(browseris.ie)
document.getElementById("scriptdiv").appendChild(scriptdiv);

while(i<=CTXMAX)
{
eval("if(typeof ctx" + i + " != 'undefined') ctx = ctx" + i +"; else ctx = null");
i++;
if(!ctx)
break;
funcExpGroup = funcExpGroup + "function ExpGroupCallServerg_{0}(arg, context){\n" +
"globalArg_g_{0} = arg; globalContext_g_{0} = context;\n" +
"setTimeout(\"WebForm_DoCallback('ctl00$m$g_{0}',globalArg_g_{0},Interceptor,globalContext_g_{0},ExpGroupOnError,true)\", 0);\n}\n";
funcExpGroup = funcExpGroup.replace(/\{0\}/g,ctx.view.toLowerCase().replace("{","").replace("}","").replace(/-/g,"_"));
}

scriptdiv.innerHTML = "&nbsp;<script" + " defer>" + funcExpGroup + "</" + "script>";
if(!(browseris.ie))
document.getElementById("scriptdiv").appendChild(scriptdiv);
scriptdiv.style.display="none";
}

CreateDynamicFunction();

//Ajax function interceptor
function Interceptor(htmlToRender, groupName)
{
ExpGroupReceiveData(RemoveLookupLinks(htmlToRender), groupName);
}
//Function which removes links using regex
function RemoveLookupLinks(htmlToRender)
{
return htmlToRender.replace(/<a[^>]*RootFolder=\*[^>]*>([^<]*)<\/a>/gi,"$1");
}
//Queue up the function to execute after page load
_spBodyOnLoadFunctionNames.push("RemoveLookupAfterPageLoad");
</script>
Vishal Monpara is a full stack Solution Developer/Architect with 12 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1’s and 0’s from geographically dispersed hard disks to geographically dispersed user’s mind leveraging geographically dispersed team members.

2 thoughts on “SharePoint: Remove URL from Lookup columns using JavaScript

  1. Rudy

    Thanks for sharing this! Is it possible to turn this on for only a few columns? LIke if I have 5 columns that lookup from a list, adn I only want 2 of them to be links, can I do this wht the code?

  2. Ashwat

    Here’s a simpler solution for use in forms. Add this to an onload/document.ready function

    $(“td.ms-formbody[id$=’SPFieldLookup’] a”).each(function (i,n) { $(n).replaceWith(“” + $(n).text() + “”); });

Leave a Reply

Your email address will not be published. Required fields are marked *