3

I am trying to use request to create a SharePoint list with node. I am 90% sure all my headers are okay, but I am not too sure. I know all the auth headers are populated with values too; any help would be.

var inputJSON = {
    "Connection": {
        "SiteUrl": "na",
        "Tennent": "na",
        "Username": "na",
        "Password": "na"
    },
    "Request": {
        "Title": "Test List",
        "TemplateType": "100",
        "Description": "My test list description"
    }
};

var Toptions = {
    auth: {
        username: inputJSON.Connection.Username,
        password: inputJSON.Connection.Password
    },
    host: inputJSON.Connection.Tennent
};


var Sharepoint = require('sharepoint-auth');
Sharepoint(Toptions, function (err, result) {
    if (err) {
        console.log(err);
    } else {
        var requestOptions = {
            url: inputJSON.Connection.SiteUrl + '_api/web/lists',
            method: 'POST',
            body: {
                '__metadata': {
                    'type': 'SP.List'
                },
                'AllowContentTypes': true,
                'BaseTemplate': 100,
                'ContentTypesEnabled': true,
                'Description': 'My list description',
                'Title': 'MY TEST LIST'
            },
            headers: {
                'Accept': 'application/json;odata=verbose',
                'Content-type': 'application/json;odata=verbose',
                'Cookie': 'FedAuth=' + result.cookies.FedAuth + ';rtFa=' + result.cookies.rtFa + ';',
                'X-RequestDigest': result.requestDigest,
                'Authorization': 'Bearer ' + result.accessToken,
                'content-length': 255
            },
            json: true
        };

        var request = require('request');

        function callback(error, response, body) {
            if (!error && response.statusCode == 200) {
                console.log(response);
            } else {
                console.log(error);
            }

        }
        request(requestOptions, callback);

    }
});
2

Consider using sp-request module for that purpose.
Your code will be:

var sprequest = require('sp-request');
var creds = {
    username: 'johndoe@contoso.onmicrosoft.com',
    password: 'mycoolpass'
};
var request = sprequest.create(creds);
request.requestDigest('https://mysharepoint.com/sites/mysite')
    .then(function (digest) {
        return request.post('https://mysharepoint.com/sites/mysite' + '/_api/web/lists', {
            body: {
                '__metadata': { 'type': 'SP.List' },
                'AllowContentTypes': true,
                'BaseTemplate': 100,
                'ContentTypesEnabled': false,
                'Description': 'Test list',
                'Title': 'My List'
            },
            headers: {
                'X-RequestDigest': digest
            }
        });
    })
    .then(function () {
        console.log('list just created!!!');
    }, function (err) {
        if (err.message.indexOf('-2130575342') === -1) {
            console.log('Hmmmmm something went wrong');
            console.log(err);
            return;
        }
        console.log('list already exists!!!');
    })
        .catch(function (err) {
        console.log('Hmmmmm something went wrong');
        console.log(err);
    });
});

Actually I'm the creator, so feel free to ask any further questions.

  • Thanks! I should be able to use the whole API here right? – Curtis Hennessy Nov 9 '16 at 11:10
  • Right, you can use it – Sergei Sergeev Nov 9 '16 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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