Knowledge.ToString()

SharePoint: Remove URL from Lookup Columns Using JavaScript

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 = " <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>

Share

Comments

2 responses to “SharePoint: Remove URL from Lookup Columns Using JavaScript”

  1. Ashwat Avatar
    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() + “”); });

  2. Rudy Avatar
    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?

Leave a Reply

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