locked
How to create an Enterprise custom field programmatically for Project Server RRS feed

  • Question

  • Hello!

    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.set_name("New_one");       
            object_to_add.set_description("test description");  
            projContext.CustomFieldCollection.add(object_to_add);
    }



    Any help would be appreciated!


    • Edited by Lorant-DLD Monday, November 17, 2014 12:10 PM edit in code
    Monday, November 17, 2014 12:10 PM

All replies

  •  projContext.CustomFieldCollection.Update();
     projContext.ExecuteQuery();

    Tuesday, November 18, 2014 7:32 AM
  • Thank you for replying, but this wouldn't help : 

    My code looks like this now : 

    'use strict';
    
    var projContext;
    var resources;
    
    // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
    $(document).ready(function () {
        projContext = PS.ProjectContext.get_current();
        projContext.set_isPageUrl(SP.ClientContext.get_current().get_isPageUrl());
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', AddCustomField); //when the page loads, we execute this method
    });
    
    function AddCustomField() {
        $('#message').text('Adding Custom Field...');
        var object_to_add = new PS.CustomFieldCreationInformation();
        //object_to_add.set_fieldType(CustomFieldType.Text)
        object_to_add.set_name("New_one");
        object_to_add.set_description("test description");
        projContext.CustomFieldCollection.add(object_to_add);
        projContext.CustomFieldCollection.Update();
        projContext.ExecuteQuery();
        $('#message2').text('Done');
        
    }

    Am I missing something?

    Tuesday, November 18, 2014 11:16 AM
  • I like this work on C#

    CustomFieldCreationInformation customFieldCreationInformation = new CustomFieldCreationInformation();
                customFieldCreationInformation.Id = new Guid();
                customFieldCreationInformation.Name = nameField;
                customFieldCreationInformation.LookupTable = table;
                customFieldCreationInformation.FieldType = fieldType;
                customFieldCreationInformation.EntityType = entityType;
                _projectContext.CustomFields.Add(customFieldCreationInformation);

    may need to set the values "Id", "EntityType", "FieldType"

    Wednesday, November 19, 2014 4:56 AM
  • Hi!

    Since the PS javascript SDK is incredibly bad, and poorly documented, I have to come to you guys with my questions.

    I want to programmatically create an Enterprise calendar using a Sharepoint 2013 app.

    I have the following code which is called from the main function of the javascript file : 

    function CreateCalendar() {
        var eCalColl = projContext.get_calendars(); //get calendar collection
        var ccinfo = new PS.CalendarCreationInformation();
        ccinfo.set_id(SP.Guid.newGuid());
        ccinfo.set_name("NewCalendar");
        ccinfo.set_originalId(SP.Guid.newGuid());
        eCalColl.add(ccinfo);
        eCalColl.update();
        projContext.executeQueryAsync(Function.createDelegate(this, function () {
    
            // Display the results and remove the progress msg
            alert("Calendar added successfully");
    
        }), Function.createDelegate(this, function (call, error) {
            alert("Error : " + error.get_message());
        }));
    }
    

    It executes the query, but has the following error message: 

    CalendarInvalidUniqueIdToDuplicate

    which, according to the error message list : Value=13038. The GUID is not valid to duplicate a calendar.

    I don't want to duplicate anything, I set every field of the CalendarCreationInformation object accordingly, and it's still not working.

    I even tried duplicating a calendar, but it's not working.


    Wednesday, November 19, 2014 8:42 AM
  • Thank you for the reply!

    I understand the C# code, but I can't really translate it to javascript, since the functions have different names, and well, intellisense is not working for JSOM.

    I have the following concerns : 

    - How can I create a new GUID in javascript programmatically?

    - After I add the nnew CustomFieldCreationInformation object to the custom field "collection", do I have to update, or run a query or what?

    The documentation on this is so incredibly poor...

    Wednesday, November 19, 2014 9:14 AM
  • try http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript

    update and run a query

    Wednesday, November 19, 2014 12:15 PM
  • Hi Lorant,

    I merged your duplicated threads.


    Hope this helps,


    Guillaume Rouyre, MBA, MVP, P-Seller |

    Wednesday, November 19, 2014 1:03 PM