0

How can I get the options for some LookUp field, in JSLink?

Code:

SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    Templates: {
        Fields: {
            Initiative: {
                EditForm: CustomFieldInitiative,
                NewForm: CustomFieldInitiative
            }
        }
    }
});

function CustomFieldInitiative(ctx) {
    if (!ctx) return '';

    var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);

    var fldName = "cbo_" + formCtx.fieldName;
    var fldValue = parseInt(formCtx.fieldValue);

    formCtx.registerGetValueCallback(formCtx.fieldName, function () {
        var cbo = document.getElementById(fldName);
        return cbo.options[cbo.selectedIndex].value;
    });

    var html = '<select id="' + fldName + '">'+
                // HOW I PUT THE OPTIONS OF LOOKUP FIELD INITIATIVE HERE?
                //<option value="{Lookup.Id}" data-parent="{Lookup.LookupPropertyPillar.Id}">{Lookup.Title}</option>
               '</select>';

    return html;
}

How can I get the options (lookup values) from field?

2

At the point in your code where you want to get the choices,

ctx.CurrentFieldSchema.Choices

is an array of lookup choice objects, which each have two properties, LookupId and LookupValue.

Here is a screenshot illustrating what I mean:

choices array

In order to find out what is available to you, I highly recommend setting a breakpoint in your script, and just clicking around to explore the ctx object, and see what you can find.

  • Yeah, i found this property too, but my problems is a little bit bigger. I need get another lookup field from choice. Something like <option value="choice.id" data-parent="choice.lookupfield.id">choice.title</option>. I'm solving this with the REST API. – Carlos Jun 29 '16 at 15:06
  • But the problem is I've to do REST request sync, and perform goes down. – Carlos Jun 29 '16 at 15:07
1
SPFieldLookup_Edit( ctx );

produces the default HTML for an SP Lookup field, you could take that HTML and work it.
If you want the individual options you have to dig into the Fieldschema (in your formCtx)

SPFieldLookup_Edit is from clientforms.js:

function SPFieldLookup_Edit(b) {
    if (b == null )
        return "";
    var a = SPClientTemplates.Utility.GetFormContextForCurrentField(b);
    if (a == null || a.fieldSchema == null )
        return "";
    if (a.fieldSchema.Throttled) {
        var d = String(a.fieldSchema.MaxQueryResult)
          , f = a.fieldSchema.Required ? STSHtmlEncode(StBuildParam(Strings.STS.L_LookupFieldRequiredLookupThrottleMessage, d)) : STSHtmlEncode(StBuildParam(Strings.STS.L_LookupFieldLookupThrottleMessage, d))
          , c = '<span style="vertical-align:middle">';
        c += f;
        c += "</span>";
        return c
    }
    if (a.fieldSchema.Required) {
        var e = new SPClientForms.ClientValidation.ValidatorSet;
        e.RegisterValidator(new SPClientForms.ClientValidation.RequiredValidator);
        a.registerClientValidator(a.fieldName, e)
    }
    return a.fieldSchema.AllowMultipleValues ? SPFieldLookupMulti_Edit(b) : SPFieldLookup_DropDown_Edit(b)
}

All HTML producing Template functions:

'Text': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPFieldText_Edit,
    'NewForm': SPFieldText_Edit
},
'Number': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPFieldNumber_Edit,
    'NewForm': SPFieldNumber_Edit
},
'Integer': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPFieldNumber_Edit,
    'NewForm': SPFieldNumber_Edit
},
'Boolean': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_DefaultNoEncode,
    'EditForm': SPFieldBoolean_Edit,
    'NewForm': SPFieldBoolean_Edit
},
'Note': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldNote_Display,
    'EditForm': SPFieldNote_Edit,
    'NewForm': SPFieldNote_Edit
},
'Currency': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPFieldNumber_Edit,
    'NewForm': SPFieldNumber_Edit
},
'File': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldFile_Display,
    'EditForm': SPFieldFile_Edit,
    'NewForm': SPFieldFile_Edit
},
'Calculated': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPField_FormDisplay_Empty,
    'NewForm': SPField_FormDisplay_Empty
},
'Choice': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPFieldChoice_Edit,
    'NewForm': SPFieldChoice_Edit
},
'MultiChoice': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPFieldMultiChoice_Edit,
    'NewForm': SPFieldMultiChoice_Edit
},
'Lookup': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldLookup_Display,
    'EditForm': SPFieldLookup_Edit,
    'NewForm': SPFieldLookup_Edit
},
'LookupMulti': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldLookup_Display,
    'EditForm': SPFieldLookup_Edit,
    'NewForm': SPFieldLookup_Edit
},
'Computed': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPField_FormDisplay_Default,
    'EditForm': SPField_FormDisplay_Default,
    'NewForm': SPField_FormDisplay_Default
},
'URL': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldUrl_Display,
    'EditForm': SPFieldUrl_Edit,
    'NewForm': SPFieldUrl_Edit
},
'User': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldUser_Display,
    'EditForm': SPClientPeoplePickerCSRTemplate,
    'NewForm': SPClientPeoplePickerCSRTemplate
},
'UserMulti': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldUserMulti_Display,
    'EditForm': SPClientPeoplePickerCSRTemplate,
    'NewForm': SPClientPeoplePickerCSRTemplate
},
'DateTime': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldDateTime_Display,
    'EditForm': SPFieldDateTime_Edit,
    'NewForm': SPFieldDateTime_Edit
},
'Attachments': {
    'View': RenderFieldValueDefault,
    'DisplayForm': SPFieldAttachments_Default,
    'EditForm': SPFieldAttachments_Default,
    'NewForm': SPFieldAttachments_Default
}

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.