1

I am wondering if there is a way that I can use the REST API to create a content type in SharePoint online. I am trying to do this from a Node.js application that is sat inside an Azure function.

I have managed to get all the authentication keys that I am ever going to need I believe but I cannot find anyway to do it. Any help would be great. Thanks.

1 Answer 1

4

Something like this (note I'm using sp-request)

  spr.requestDigest('https://sp2013dev/sites/dev')
  .then(function (digest) {
    return spr.post('https://sp2013dev/sites/dev/_api/web/contenttypes', {
      body: {
            '__metadata': {'type': 'SP.ContentType'},
           'Description': 'TestDesc',
           'Name': 'ContentType New',
           'Group': 'Custom Group',        
            'Id':{
                '__metadata':{
                    'type':'SP.ContentTypeId'
                },
                'StringValue':'0x01978768sdfs456df'
            }
      },
      headers: {
        'X-RequestDigest': digest
      }
    });
  })
  .then(function (response) {
    console.log('Created!');
  })
  .catch(function (err) {
      console.log(err.message);
  });

UPD
BTW, I recommend you to try pnp-js-core with nodejs. Recently I created a demo repository with setup required in order to use pnp-js-core from nodejs: node-pnpjs-sample.
With pnp-js-core you can simply write:

let web: pnp.Web = new pnp.Web(settings.siteUrl);
web.contentTypes.add('ct id', 'name', 'description')
.then(() => {
    console.log('created!');
});
4
  • I have been using sp-request and it is really good! With this code here I get a 500 error. Nov 11, 2016 at 15:37
  • Any error messages? Nov 11, 2016 at 15:39
  • 2016-11-11T15:36:14.886 { StatusCodeError: 500 - {"error":{"code":"183, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"A duplicate content type \"ContentType New\" was found."}}} This makes me think it worked but isn't appearing in SharePoint UI Nov 11, 2016 at 15:42
  • That's mean you already created content type with following name. Nov 11, 2016 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.