Show Required Fields in Custom SharePoint Page Layout

http://www.mavention.com/blog/showing-required-fields-when-editing-a-page-sharepoint-2013

This guy had a great solution for getting and displaying required fields on a SharePoint custom page.  Thanks!

showRequiredFields = function() {
    var context = new SP.ClientContext();
    var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
    var item = list.getItemById(_spPageContextInfo.pageItemId);
    fieldCollection = item.get_contentType().get_fields();

    context.load(fieldCollection);
    context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, failed));

    function success() {
        var occurences = 0;
        var fe = fieldCollection.getEnumerator();
        while (fe.moveNext()) {
            var field = fe.get_current();
            var xmlSchemaString = field.get_schemaXml();
            var fieldXml = $.parseXML(xmlSchemaString);
            var fieldElement = fieldXml.getElementsByTagName('Field')[0];

            var required = fieldElement.getAttribute('Required');
            if (required != null && fieldElement.getAttribute('Required').toLowerCase() === 'true') {
                var displayName = fieldElement.getAttribute('DisplayName');
                $("[title='" + displayName + "']").closest('tr').children('td.ms-formlabel').append(' *');
            }
        }

        if (occurences > 0) {
            $('#DeltaPlaceHolderMain').prepend('* = required'); } } function failed(sender, args) { console && console.log('failed. Message:' + args.get_message()); } }

Leave a Reply

Your email address will not be published. Required fields are marked *