3

I have a site with 150+ libraries that are tied to a business process. Each has the default view configuration from the template I used. Now the business process is evolving and users want to see additional fields in the default view. I was able to add the necessary columns via content type, no problem. Now I need to change the default view (all have the same name) to assist users in sorting and filtering files.

In the past I have use REST to add fields, change field names, etc... However, I can't seem to change the view settings. I have tried using a workflow and javascipt but I keep getting "Bad Request" no matter what I try.

I followed the MSDN directions here: https://msdn.microsoft.com/EN-US/library/office/dn531433.aspx#bk_View

As well as directions on StackOverflow on how to get the X-RequestDigest value. Which is working as expected.

Here is the error I'm getting (shortened here):

"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."

and

"status":400,"statusText":"Bad Request"

Here is the script I'm using.

(function () {
    var payload = "{'__metadata': {'type': 'SP.View'}, 'Title': 'NEW-TITLE'}";
    var xRequestDigest = $("#__REQUESTDIGEST").val();
    console.log(payload);
    console.log(xRequestDigest);
    $.ajax({
        url: "WEBSITE-URL/_api/web/lists/getbytitle('LIBRARY-NAME')/views/getbytitle('VIEW-NAME')",
        method: "POST",
        body: payload,
        headers: {
            "content-type": "application/json;odata=verbose",
            "X-Requestdigest": xRequestDigest,
            "accept": "application/json; odata=verbose",
            "X-HTTP-Method": "MERGE"
        },
       success: printInfo,
       error: logError
    });
})()

function printInfo() {
    console.log('View Updated');
}

function logError(error) {
    console.log(JSON.stringify(error));
}
2

You are using it wrong. In the MSDN samples, a POST is carried out using SP.RequestExecutor. Since you are using jQuery, use data instead of body. Because of that, nothing is being posted. Now, your ajax portion should look like below:

$.ajax({
    url: "WEBSITE-URL/_api/web/lists/getbytitle('LIBRARY-NAME')/views/getbytitle('VIEW-NAME')",
    method: "POST",
    data: payload,
    headers: {
        "content-type": "application/json;odata=verbose",
        "X-Requestdigest": xRequestDigest,
        "accept": "application/json; odata=verbose",
        "X-HTTP-Method": "MERGE"
    },
   success: printInfo,
   error: logError
});
  • Is there a difference between data and body ? – Swayam Siddha Sep 21 '15 at 17:44
  • 1
    No. It's just the difference in implementation. jQuery uses data while requestexecutor uses body. When you try posting this without using javascript ie using advanced rest client you'll notice that the request goes through. – Akhoy Sep 21 '15 at 17:50
  • I have verified that using "data" when using jQuery solved my issue. Thanks @Arkhoy. I found a note about this on MSDN in the Fields Reference page: MSDN Page – Arknev Sep 22 '15 at 12:42

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.