1

How do you remove hyperlinks in the Look up field using jQuery in SharePoint?

8
  • SharePoint 2013 or 2010? Commented Nov 12, 2015 at 12:51
  • And what code did you try sofar Commented Nov 12, 2015 at 13:17
  • $('a[href*="mylink").contents().unwrap();
    – Dheeraj
    Commented Nov 13, 2015 at 13:51
  • I need it in 2010....I have tried $('a[href*="mylink").contents().unwrap(); so far...Its working fine when the view is normal..BUt when I try to group fields then the code is not working..Can somebody help on this
    – Dheeraj
    Commented Nov 13, 2015 at 13:53
  • I have tried lot of methods ....They are not working in a grouped view
    – Dheeraj
    Commented Nov 13, 2015 at 14:53

1 Answer 1

3

Using jQuery: You can try using Content Editor Web Part. Find the element and unwrap().

$('#myLink').contents().unwrap();

There are many ways to remove hyperlink using jQuery. Use which one suites you best.

For SharePoint 2013: You can use CSR. Let's say I have following list with a lookup column which allows multiple selection.

enter image description here

Following code will remove the hyperlink from lookup column.

"Internal Name of the lookup column": {
                   View: function(ctx) { 
                     var lookUpValues = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
                     var lookUpDisplayTitles = [];
                     lookUpValues.forEach(function(lookup){
                       lookUpDisplayTitles.push(lookup.lookupValue);
                     });
                     return lookUpDisplayTitles.join("; "); 
                   }

ctx.CurrentItem[ctx.CurrentFieldSchema.Name] returns lookup values as array of objects. It will always return lookup value as array. It does not matter whether you have allowed multiple selection or not. Object inside the array looks like

{
    lookupId: 2,
    lookupValue: "Active Directory",
    isSecretFieldValue: false
}

So I have pushed all lookupValue in an array named lookUpDisplayTitles. Finally, I have joined them by "; " and returned it to the view. So my current view looks like

enter image description here

Find the full code below and use Cisar(developed by Andrey Markeev) for CSR

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

    function getBaseHtml(ctx) {
        return SPClientTemplates["_defaultTemplates"].Fields.default.all.all[ctx.CurrentFieldSchema.FieldType][ctx.BaseViewID](ctx);
    }

    function init() {

        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({

            // OnPreRender: function(ctx) { },

            Templates: {

                //     View: function(ctx) { return ""; },
                //     Header: function(ctx) { return ""; },
                //     Body: function(ctx) { return ""; },
                //     Group: function(ctx) { return ""; },
                //     Item: function(ctx) { return ""; },
                Fields: {
                    "Internal Name Of The Lookup Column": {
                        View: function(ctx) {
                            var lookUpValues = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
                            var lookUpDisplayTitles = [];
                            lookUpValues.forEach(function(lookup) {
                                lookUpDisplayTitles.push(lookup.lookupValue);
                            });
                            return lookUpDisplayTitles.join("; ");
                        },
                        //             EditForm: function(ctx) { return ""; },
                        //             DisplayForm: function(ctx) { return ""; },
                        //             NewForm: function(ctx) { return ""; }
                    }
                },
                //     Footer: function(ctx) { return ""; }

            },

            // OnPostRender: function(ctx) { },

            ListTemplateType: 100

        });
    }

    RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/HyperLinkRemove.js"), init);
    init();

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.