0

I am working on an on-premises sharepoint 2013. now i know that i can hide certain columns from New list forms using this power-shell script:-

$web = Get-SPWeb http://*******/
$field = $web.Fields["Approve"]
$field.ShowInNewForm = $false 
$field.update($true)

this will always hide the site column named "Approve" from the New list forms. but in my case i have 2 site content types, which are using the Approve site column.but i want the column to be shown inside the New form for Content Type A, while to be hidden inside the New form for Content Type B .. is this possible? or hiding/showing site columns based on the list form type (New,Edit,View), can only be achieved @ the site column itself? and not at the Content type level ? keeping in mind that in both cases i want the Approve site column, to be shown inside the Edit form..so hiding the column completely from the Content Type will not work...

1 Answer 1

0

You can use Client side rendering(jsLink) to control its display based on Content Type.

Just add the below jslink in your newform and editform, modify it to use your content type. Should work.

(function () {

    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.Templates.Fields = {        
        "Approve": {
            "NewForm": hideField,
            "EditForm": hideField
        }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();


function hideField(ctx) {

    if(ctx.CurrentItem.ContentType == "Content Type B"){
        // get the span that represents the field's actual edit control
        var span = $get(ctx.FormUniqueId + ctx.FormContext.listAttributes.Id + ctx.CurrentFieldSchema.Name);

        // still need to go up two levels to hide the entire <tr>
        span.parentNode.parentNode.setAttribute("style", "display:none");
        return "";
    }else{
        return SPFieldBoolean_Edit(ctx);
    }
}

Here, i am assuming that Approve is a site column of boolean type.

Modified from - Hiding Forms; fields using standard jquery versus using JSlink. which approach is better

1
  • hey @johng- did you try this approach ? Feb 6, 2018 at 6:41

Your Answer

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

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