How to create an Enterprise custom field programmatically for Project Server

942 views Asked by At

I want a Sharepoint 2013 app to create programmatically an Enterprise Custom Field when it runs for the first time.

I fiddled around with the following code snippet, but it's not working

var projContext = PS.ProjectContext.get_current();
function AddCustomField() {
    $('#message').text('Adding Custom Field...');
    var object_to_add = new PS.CustomFieldCreationInformation();
    object_to_add.FieldType = CustomFieldType.Text;
    object_to_add.Name = "New_one";
    object_to_add.Description = "test description";
    projContext.CustomFieldCollection.add(object_to_add);
}

Any help would be appreciated!

1

There are 1 answers

1
Vanessa On
var projContext = PS.ProjectContext.get_current();
var fieldType = PS.CustomFieldType.TEXT;
var customfields = projContext.get_customFields();
var entityTypes = projContext.get_entityTypes();
var projEntity = entityTypes.get_projectEntity();
var resourceEntity = entityTypes.get_resourceEntity();
var taskEntity = entityTypes.get_taskEntity();

projContext.load(customfields);
projContext.load(entityTypes);
projContext.load(projEntity);
projContext.load(resourceEntity);
projContext.load(taskEntity);

projContext.executeQueryAsync(QuerySucceeded, QueryFailed);

CreateField("Test", "Test", fieldType, projEntity);

function CreateField(name, description, fieldtype, entitytype) {
    var customfieldInfo = new PS.CustomFieldCreationInformation();
    customfieldInfo.set_description(description);
    customfieldInfo.set_name(name);
    customfieldInfo.set_fieldType(fieldtype);
    customfieldInfo.set_entityType(entitytype);

    customfields.add(customfieldInfo);
    customfields.update();
    projContext.load(customfields);
    projContext.executeQueryAsync(QuerySucceeded, QueryFailed);

}