Home > Javascript, SharePoint > SharePoint: Remove URL from Lookup columns using JavaScript

SharePoint: Remove URL from Lookup columns using JavaScript

January 15th, 2011 Leave a comment Go to comments

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>
Categories: Javascript, SharePoint Tags:
  1. Rudy
    November 14th, 2011 at 13:13 | #1

    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?

  1. No trackbacks yet.