2

I have read the following article which shows how to change a "People or Group" field type from text field to be a drop down, which what i am looking for:-

https://hyankov.wordpress.com/2015/07/15/sharepoint-2013-people-groups-field-as-a-dropdown-with-predefined-choices/

Now i did the following steps:-

1) i added the following .js file inside the SiteAssets:-

jQuery.fn.outerHTML = function (s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};

var Fields_Renderer = Fields_Renderer || {};

Fields_Renderer.FieldName = "GroupsAsDropdownTest";

// Define the allowed options (groups)
Fields_Renderer.DropdownOptions = [
        {
            Group: "Hierarchy Managers",
            DisplayName: "Hierarchy Managers - Display Name"
        },
        {
            Group: "Restricted Readers",
            DisplayName: "Restricted Readers - You can change this"
        },
        {
            Group: "Translation Managers",
            DisplayName: "Translation Managers - To whatever you want"
        }
];

Fields_Renderer.Edit = function (ctx) {
    var currentValue = "";
    if (ctx.CurrentFieldValue.length > 0) {
        currentValue = ctx.CurrentFieldValue[0].Key;
    }

    var dropdownHTML = "<select onChange='Fields_Renderer.SelectionChange(this);'>";
        dropdownHTML += "<option value=''>- Please select -</option>";
    jQuery.each(Fields_Renderer.DropdownOptions, function (idx, option) {
        dropdownHTML += "<option value='" + option.Group + "' " + (currentValue == option.Group ? 'selected' : '') + ">" + option.DisplayName + "</option>";
    });
    dropdownHTML += "</select>";

    var html = jQuery(SPClientPeoplePickerCSRTemplate(ctx));
    html.hide();

    return html.outerHTML() + dropdownHTML;
};

Fields_Renderer.SelectionChange = function (select) {
    var drop = jQuery(select);
    var pickerId = drop.parent().find(".sp-peoplepicker-topLevel").attr("id");
    var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[pickerId];

    // Remove current group(s)
    if (!peoplePicker.IsEmpty()) {
        jQuery("[id = '" + peoplePicker.ResolvedListElementId + "']").children().each(function (index, element) {
            element = jQuery(element);
            if (element.attr("id").indexOf("_ProcessedUser") > -1) {
                peoplePicker.DeleteProcessedUser(element[0]);
            }
        });
    }

    // Enter new value
    var newVal = drop.find("option:selected").val();
    if (newVal != "") {
        var usrObj = { 'Key': newVal };
        peoplePicker.AddUnresolvedUser(usrObj, true);
    }
};

Fields_Renderer.Display = function (fieldCtx, fieldInfo) {
    var displayValue = "";
    var currentValue = "";

    if (fieldCtx.CurrentFieldValue != null) {
        currentValue = fieldCtx.CurrentFieldValue.split(',')[0].split(";#")[1];
    } else {
        if (fieldCtx.CurrentItem[Fields_Renderer.FieldName].length > 0) {
            currentValue = fieldCtx.CurrentItem[Fields_Renderer.FieldName][0].title;
        }
    }

    if (currentValue != "") {
        jQuery.each(Fields_Renderer.DropdownOptions, function (idx, option) {
            if (currentValue == option.Group) {
                displayValue = option.DisplayName;
                return false;
            }
        });
    }

    return "<span>" + displayValue + "</span>";
};

(function () {
    var FieldCtx = {};
    FieldCtx.Templates = {};
    FieldCtx.Templates.Fields = {
        'GroupsAsDropdownTest': {
            'View': Fields_Renderer.Display,
            'NewForm': Fields_Renderer.Edit,
            'EditForm': Fields_Renderer.Edit,
            'DisplayForm': Fields_Renderer.Display,
            'Item': Fields_Renderer.Display
        }
    };

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

2) then i assign the .js to the field as follow :-

$web = Get-SPWeb http://t*****01/
$field = $web.Fields["GroupsAsDropdownTest"]
$field.JSLink = "~sitecollection/Style Library/JS/jquery-1.10.2.js|~sitecollection/siteassets/MyCompany.JSLink.Fields.GroupsAsDropdownTest.js"
$field.update($true)

But when i access the create form, the people or group column is being rendered as empty <td> as follow:-

enter image description here

and here is the markup for the field:-

    <td class="ms-formlabel" nowrap="true" width="113px" valign="top"><h3 class="ms-standardheader">
    <nobr>GroupsAsDropdownTest</nobr>
</h3></td>
    <td class="ms-formbody" width="350px" valign="top">
    <!-- FieldName="GroupsAsDropdownTest"
         FieldInternalName="GroupsAsDropdownTest"
         FieldType="SPFieldUser"-->     
    </td>

so can anyone adivce why the above appraoch did not work ?? and is there any other approaches to change the people or group field type from being free text to be a drop-down list?

Thanks

  • 1
    Maybe a silly question, but did you then put those JS files in the location you're pointing the JSLink registration to? JSLink can be debugged using the bowser tools, I would check that both of your files loaded into the page. – Julie Turner Feb 17 '17 at 14:36
  • @JulieTurner Thanks for the reply.. now your concern is valid,, but in my case the JSLINK is being loaded correctly and i added a test alert(123) into the js link and the alert will be shown when i access the create form.. so yes the jQuery and the JS link are being rendered correctly.. so not sure what is going to .. – john Gu Feb 17 '17 at 15:51
  • Check the people column settings. In the Allow selection of - it should be set to People and Groups – Gautam Sheth Feb 17 '17 at 18:51
  • @GautamSheth i already set it as People and Groups .... but did not solve the problem,,, any additional suggestions ? – john Gu Feb 20 '17 at 9:52
  • Do the groups exist in the site ? I mean Hierarchy Managers should be present in the site, if its not there , try with your site groups like sitename members – Gautam Sheth Feb 20 '17 at 9:54

Your Answer

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

Browse other questions tagged or ask your own question.