0

Lets say I have a list view called "Old View Name" with a server relative URL of:

/sites/MySite/Lists/MyList/Old View Name.aspx

Now I want to update the view to be called "New View", and I want the URL to match:

/sites/MySite/Lists/MyList/New View.aspx

How can I get that done using the REST API?

I can update the view title / display name no problem:

var viewQuery = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/lists/getbytitle('My List')/views?$filter=ServerRelativeUrl eq '${oldViewUrl}'`;
var viewResponse = await $.ajax({
    url: viewQuery,
    method: 'GET',
    headers: {
        accept: 'application/json;odata=verbose'
    }
});
var view = viewResponse.d.results[0];

// update the display name of the view
var updateTitle = {
    __metadata: {
        type: view.__metadata.type
    },
    Title: newViewName
};

// nothing is returned from an update
await $.ajax({
    url: view.__metadata.uri,
    method: 'POST',
    headers: {
        accept: 'application/json;odata=verbose',
        'content-type': 'application/json;odata=verbose',
        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
        'X-HTTP-Method': 'PATCH',
        'IF-MATCH': '*'
    },
    data: JSON.stringify(updateTitle)
});

// get the view with the updated title
var updatedView = await $.ajax({
    url: view.__metadata.uri,
    method: 'GET',
    headers: {
        accept: 'application/json;odata=verbose'
    }
});

At this point, the view name is "New View", but the URL is still

/sites/MySite/Lists/MyList/Old View Name.aspx

so updating the name does not automatically update the URL. (Expected, because if you go in through the web UI to update a view, there are separate inputs for the view name and the view URL.)

I've tried inserting the new page name that I want in the ListViewXml string and updating the ListViewXml property of the view, and that just doesn't work (fails to update, but no error thrown).

I've also tried to get the .aspx page directly and update the Name property of the SP.File:

var viewPageQuery = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/GetFileByServerRelativeUrl('${updatedView.d.ServerRelativeUrl}')`;
var page = await $.ajax({
    url: viewPageQuery,
    method: 'GET',
    headers: {
        accept: 'application/json;odata=verbose'
    }
});

var pageUpdate = {
    __metadata: {
        type: page.d.__metadata.type
    },
    Name: `${newViewName}.aspx`
}

// this fails with 400 Bad Request
await $.ajax({
    url: page.d.__metadata.uri,
    method: 'POST',
    headers: {
        accept: 'application/json;odata=verbose',
        'content-type': 'application/json;odata=verbose',
        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
        'X-HTTP-Method': 'PATCH',
        'IF-MATCH': '*'
    },
    data: JSON.stringify(pageUpdate)
});

That fails with a 400 - Bad Request.

So... how can I update the .aspx page that corresponds to the view to reflect the new view name?

I'd prefer to do it using REST, but if I have to resort to JSOM I will.

Your Answer

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

Browse other questions tagged or ask your own question.