How to get fields of enterprise Task/Project/Resource of SharePoint in JSOM or JavaScript?

Get field of enterprise Task/Project/Resource of SharePoint in JSOM or JavaScript:


Use the below snippet all fields/column of Task using JSOM.

<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="_layouts/15/ps.js"></script>

<script type="text/javascript" >
var Fields = [];

function getFields (from, type) {
    // console.log(type);

    console.log('Getting all fields');
    // Initialize the current client context. 
    var filcontext = PS.ProjectContext.get_current();
    var allFields = filcontext.get_customFields();
    filcontext.load(allFields, "Include(Id, Name, EntityType, InternalName, FieldType, Formula)");
    filcontext.executeQueryAsync(OnRequestSuccess, OnRequestFailed);
    // Callback for request failure
    function OnRequestFailed (sender, args) {
        console.log(args.get_message());
    };
    function OnRequestSuccess(){

        var collection_enumerator = allFields.getEnumerator();
        var count = 0;
        while (collection_enumerator.moveNext()) {
            var id = collection_enumerator.get_current().get_id();
            var name = collection_enumerator.get_current().get_name();
            var nameWOSpace = collection_enumerator.get_current().get_name();
            var entityType = collection_enumerator.get_current().get_entityType().get_name();
            var internalName = collection_enumerator.get_current().get_internalName();
            var fieldTypeVal = collection_enumerator.get_current().get_fieldType();
            var formula = collection_enumerator.get_current().get_formula();
            console.log('Entity Type : '+ entityType);
            if (entityType == type && formula == null) {
                if (fieldTypeVal == '9' || fieldTypeVal == '4' || fieldTypeVal == '15' || fieldTypeVal == '17' || fieldTypeVal == '21') {
                    var fieldType = '';
                    if (fieldTypeVal == '21') {
                        fieldType = 'TEXT';
                    } else if (fieldTypeVal == '15') {
                        fieldType = 'NUMBER';
                    } else if (fieldTypeVal == '17') {
                        fieldType = 'FLAG';
                    } else if (fieldTypeVal == '4') {
                        fieldType = 'DATE';
                    } else if (fieldTypeVal == '9') {
                        fieldType = 'NUMBER';
                    }
                    Fields.push({
                        'id': id,
                        'colInternalName': internalName,
                        'colName': name,
                        'colType': fieldType,
                        'isLookup': false
                    });

                }
            }
        }
        console.log(Fields); //It will give us all field of except formula and lookup type 
    }
 }

getFields('OnLoad','Task');//This is call for Field of Tasks
//getFields('OnLoad','Resource');//This is call for Field of Task of Resource
//getFields('OnLoad','Project');//This is call for Fields of Project

</script>

Comments

Popular posts from this blog

Get list item entity type full name of a SharePoint list using REST API

Get SharePoint list name by GUID

Get Current Web Logged In User of SharePoint Site in jQuery