5

I have content type say "Departments" and applying to the list named "All Departments", there is one field in content type that I want to apply js link to render it differently

I found some solution to render the fields differently using js link but in those articles they are suggesting to specify the property of the field.

But I want to render the field in the single form's new form only. In other content types where the field is reference there should be normal rendering.

What should I do?

11

You should do this steps (for example):

Create New Field

<Field ID="{FFCDA660-279E-4581-B451-49365B002B1A}"
     Name="RequestStatus"
     DisplayName="Request status"
     Type="Choice"
     Required="TRUE"
     Overwrite="TRUE">
<CHOICES>
  <CHOICE>Open</CHOICE>
  <CHOICE>In Progress</CHOICE>
  <CHOICE>Resolved</CHOICE>
  <CHOICE>Closed</CHOICE>
  <CHOICE>Reopened</CHOICE>
</CHOICES>
<Default>Open</Default>

Create new content type

  <ContentType ID="0x0100F1541A19429846C599622A6F63255D65"
               Name="Base Request Content Type"
               Overwrite="TRUE"
               Inherits="TRUE">
    <FieldRefs>
      <FieldRef ID="{FFCDA660-279E-4581-B451-49365B002B1A}" Name="RequestStatus" Required="TRUE"/>
    </FieldRefs>
  </ContentType>

Customize your list schema. Set JSLink attribute on the Form tags.

    <List xmlns:ows="Microsoft SharePoint"
      Title="Requests"
      DisableGridEditing="TRUE"
      FolderCreation="FALSE"
      NavigateForFormsPages="FALSE"
      EnableContentTypes="TRUE"
      Direction="$Resources:Direction;"
      Url="Lists/Requests"
      BaseType="0"
      xmlns="http://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <ContentTypes>
            //Cointent types
    </ContentTypes>
    <Fields>
            //Fileds
    </Fields>
    <Views>
            //Views
    </Views>
    <Forms>
      <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" JSLink="~site/Lists/Files/RequestFormCRS.js" />
    </Forms>
  </MetaData>
</List>

Js File sample

(function () {
    if (typeof SPClientTemplates === 'undefined')
        return;
    var fldCtx = {};
    fldCtx.Templates = {};
    fldCtx.Templates.Fields = {
        'RequestStatus': { 'NewForm': renderRequestStatus }
    };

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

function renderRequestStatus(ctx) {
    var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);
    // logic for render html
    var controlHtml = "<input/>";

    formCtx.registerGetValueCallback(formCtx.fieldName, function () {
    // get value logic
       var controlCurrentValue = "my value"; // extract value from control
       return controlCurrentValue;
    });

    return controlHtml;
}
  • Thank you for reply sir, but I am not finding the JSLink property in <Form Type="NewForm.../> what am I missing? – users1100 Nov 22 '13 at 10:14
  • and also I am not getting the properties DisableGridEditing & NavigateForFormsPages – users1100 Nov 22 '13 at 10:30
  • Visual studio 2012 IntelliSense don't know about this property (not documented properties). But it's work fine in my project. – Artur Kukharevich Nov 22 '13 at 10:30
  • Above two mentioned properties are required to set? those I am not finding? – users1100 Nov 22 '13 at 10:33
  • DisableGridEditing & NavigateForFormsPages are not required. It'is copy-paste from my code.:) You can remove it. – Artur Kukharevich Nov 22 '13 at 10:35
1

I guess one solution, not super good though, would be to add JSLink to the field. Then in the JSLink file you make a check on for example List name or URL of the current context (ctx).

It would allow you to change rendering only on the specific list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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