SharePoint REST moveviewfieldto endpoint

Anna 0 Reputation points
2023-05-19T13:45:39.6766667+00:00

Hello I was able to successfully use many of the endpoints in REST API (get, update, create, delete), but I cannot get to work "/_api/Web/lists/getByTitle('listTitle')/views/getByTitle('viewTitle')/viewfields/moveviewfieldto"

I am using it the following way:

$.ajax({
	url: endpointUrl,
	method: "POST",
	contentType: "application/json;odata=verbose",
	body: "{'field': 'fieldInternalName', 'index': 1}",
	headers: {
		"Accept": "application/json;odata=verbose",
		"X-RequestDigest": $("#__REQUESTDIGEST").val()
	} 
})

The error I am getting:

{
    "readyState": 4,
    "responseText": "{\"error\":{\"code\":\"-2147467261, System.ArgumentNullException\",\"message\":{\"lang\":\"en-US\",\"value\":\"Value cannot be null.\\r\\nParameter name: field\"}}}",
    "responseJSON": {
        "error": {
            "code": "-2147467261, System.ArgumentNullException",
            "message": {
                "lang": "en-US",
                "value": "Value cannot be null.\r\nParameter name: field"
            }
        }
    },
    "status": 400,
    "statusText": "Bad Request"
}

Could you please advise? Thanks!

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-05-22T06:52:36.1166667+00:00

    Hi @Anna,

    The error message suggests that the "field" parameter in your request is null or empty, which is causing the "ArgumentNullException" exception. 

    To resolve this issue, make sure you provide a valid value for the "field" parameter in the request body.

    Please replace 'FieldName' with the actual internal name of the field you want to move within the view.

    Make sure to provide a valid field name that exists in the view.

    Here is a test for your reference:

    User's image

    <button onclick="SetListViewTitle();" type="button" unselectable="on">Click me​</button>
    <script src="https://MyServer/sites/SiteCollection/style library/js/ScriptFile.js"></script>
    <script>
    function SetListViewTitle() {
    var listTitle = 'Fctest List';
    var viewTitle = 'test1';
    var fieldInternalName = 'DisplayImage';
    var fieldIndex = 1;
    
    // Get the view by title
    var viewUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/views/getByTitle('" + viewTitle + "')";
    $.ajax({
        url: viewUrl,
        method: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (viewData) {
            // Update the field within the view
            var viewFieldsUrl = viewData.d.__metadata.uri + "/ViewFields/MoveViewFieldTo";
            $.ajax({
                url: viewFieldsUrl,
                method: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify({ "field": fieldInternalName, "index": fieldIndex }),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()
                },
                success: function (data) {
                    console.log("Field moved successfully within the view.");
                },
                error: function (xhr, status, error) {
                    console.log("Failed to move the field within the view. Error: " + error);
                }
            });
        },
        error: function (xhr, status, error) {
            console.log("Failed to retrieve the view. Error: " + error);
        }
    });
    
    }</script>
    
    
    
    

    User's image

    Here is test result:

    User's image

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in [our documentation] to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.