2

I am trying to create a list in SP2013 REST. This is what I get

        var url = "http://mysite/_api/web/lists";

        $.ajax({
            url: url,
            type: "POST", 
            contentType: "application/json;odata=verbose",
            body: { 
                '__metadata': { 
                    'type': 'SP.List' 
                }, 
                'AllowContentTypes': true, 
                'BaseTemplate': 100,
                'ContentTypesEnabled': true, 
                'Description': 'My list description', 
                'Title': 'Test123' 
            },
            cache: false,
            headers: {
                "Authorization" : "Bearer123",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "content-length" : 800
            },
            beforeSend : function(jqXHR, settings) {

            },
            success: function (data) {
                alert(data);
            },
            error: function (jqxr, errorCode, errorThrown) {
                alert("Failed");
            }
        });

But it doesn't seem to work. The MSDN page is not that helpful since it doesn't give a jquery example, and I can't find any good examples anywhere.

https://msdn.microsoft.com/en-us/library/office/dn292552.aspx

Does anyone know to do it?

with the above, it seems to be returning

"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected.\"}}}"

Thanks

1 Answer 1

1

Few things are missing, JSON.stringify in request payload and send payload in data property of $.ajax. I am using following

var url = "/_api/web/lists";

var data = {
    '__metadata': {
        'type': 'SP.List'
    },
    'AllowContentTypes': true,
    'BaseTemplate': 100,
    'ContentTypesEnabled': true,
    'Description': 'My list description',
    'Title': 'Test123'
};

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + url,
    type: "POST",
    headers: {
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "content-Type": "application/json;odata=verbose"
    },
    data: JSON.stringify(data),
    success: function(data) {
        console.log(data);
    },
    error: function(error) {
        alert(JSON.stringify(error));
    }
});

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.