locked
Create contenttype using REST - Need help RRS feed

  • Question

  • Hello Experts,

    Below are my questions:

    1. I am creating contenttype using REST. Initially i have started with fiddler where i am able to create a contenttype with default parent contenttype ITEM but I dont know how should i refer the PARENT Contenttype like FOLDER @

    { '__metadata': { 'type': 'SP.ContentType'}, 'Name': 'NewFolder', 'Group': 'Custom Content Types'};

    Going forward I am going to create custom site columns and attach it to the content type.

    Why it is so damn difficult to figure out the right metadata to POST anything into SharePoint. ECMAScript (SP.js) is little easy but still the documentation @ MSDN is not as helpful as it should be. 

    2. What is the right approach of getting complex things done in SharePoint via REST (not create/update/delete list/item - thats easy). I do not see lot of documentation/ blogs/ information around it.

    Experts please help!

    Thanks

    Rahul


    Thanks Rahul Prabhune

    Thursday, March 12, 2015 1:59 PM

Answers

  • I would not use REST to do anything with contenttypes. The REST API does not work with these even though the documentation would lead you to believe this. The example code below will not work because the SP.ContentTypeEntityData type which is required when posting does not exist in the EDM (Entity Data Model) which is used for validation when posting.

    function createContentType() {
        appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
        hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
        var restSource = appweburl + "/_api/SP.AppContextSite(@target)/web/contenttypes/?@target='" +
                  hostweburl + "'";
    
        //it appears that SP.ContentTypeEntityData does not exist in the EDM model, this does not work
        
        $.ajax(
        {
            'url': restSource,
            'method': 'POST',
            data: JSON.stringify({
                    '__metadata': {
                        'type': 'SP.ContentTypeEntityData'
                    },
                    'Description': 'My Custom Folder',
                    'Group': 'Custom Content Types',
                    'Name': 'MyCustomFoler',
                    'ParentContentTypeId': '0x0120'
                }
            ),
            'headers': {
                'accept': 'application/json;odata=verbose',
                'content-type': 'application/json;odata=verbose',
                'X-RequestDigest': $('#__REQUESTDIGEST').val()
            },
            'success': function (data) {
                var d = data;
            },
            'error': function (err) {
                alert(JSON.stringify(err));
            }
        }
         );
    
    }

    If you use SP.ContentType, this will not work either because the ParentContentTypeId property does not exist on this type.

    To help me understand the API I use the Visual Studio extension SPRemoteAPIExplorer. This requires Visual Studio and SharePoint on-premises to be installed locally. However, it has saved me countless hours in finding exactly what I am supposed to post and how to read REST responses.

    https://visualstudiogallery.msdn.microsoft.com/26a16717-0c9a-4367-8dfd-bb09e7e2deb5


    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer

    • Marked as answer by RahulPrabhune Monday, March 23, 2015 2:19 PM
    Saturday, March 14, 2015 3:23 PM

All replies

  • I would not use REST to do anything with contenttypes. The REST API does not work with these even though the documentation would lead you to believe this. The example code below will not work because the SP.ContentTypeEntityData type which is required when posting does not exist in the EDM (Entity Data Model) which is used for validation when posting.

    function createContentType() {
        appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
        hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
        var restSource = appweburl + "/_api/SP.AppContextSite(@target)/web/contenttypes/?@target='" +
                  hostweburl + "'";
    
        //it appears that SP.ContentTypeEntityData does not exist in the EDM model, this does not work
        
        $.ajax(
        {
            'url': restSource,
            'method': 'POST',
            data: JSON.stringify({
                    '__metadata': {
                        'type': 'SP.ContentTypeEntityData'
                    },
                    'Description': 'My Custom Folder',
                    'Group': 'Custom Content Types',
                    'Name': 'MyCustomFoler',
                    'ParentContentTypeId': '0x0120'
                }
            ),
            'headers': {
                'accept': 'application/json;odata=verbose',
                'content-type': 'application/json;odata=verbose',
                'X-RequestDigest': $('#__REQUESTDIGEST').val()
            },
            'success': function (data) {
                var d = data;
            },
            'error': function (err) {
                alert(JSON.stringify(err));
            }
        }
         );
    
    }

    If you use SP.ContentType, this will not work either because the ParentContentTypeId property does not exist on this type.

    To help me understand the API I use the Visual Studio extension SPRemoteAPIExplorer. This requires Visual Studio and SharePoint on-premises to be installed locally. However, it has saved me countless hours in finding exactly what I am supposed to post and how to read REST responses.

    https://visualstudiogallery.msdn.microsoft.com/26a16717-0c9a-4367-8dfd-bb09e7e2deb5


    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer

    • Marked as answer by RahulPrabhune Monday, March 23, 2015 2:19 PM
    Saturday, March 14, 2015 3:23 PM
  • Hi Steve, Thank you so much for your time and effort for helping me with this. SPRemoteAPIExplorer looks awesome and i will definitely try it very soon. Between, is CSOM vai ECMA (SP.js) more matured and should be used more often then REST? Can you please share a business case where i would use REST and where i would use CSOM SP.js? Can you please share your thoughts? Thanks Rahul

    Thanks Rahul Prabhune

    Sunday, March 15, 2015 7:45 AM
  • I use REST as much as possible because of it's simplicity. However, CSOM has more capabilities. If you are doing anything with Taxononomy (metadata) fields or ContentTypes you must use CSOM.

    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer

    Monday, March 16, 2015 2:56 PM