0

Following method works properly to create list view but I am not able to(don't know) add/select columns in that view, in the same REST API call.

Please Help.

$.ajax
({url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('List Name')/views",
type: "POST",
data: "{'__metadata':{'type': 'SP.View'},'ViewType': 'HTML','Title':'view name','PersonalView':false}",            
            headers:
            {
        "Accept": "application/json;odata=verbose", 
            "Content-Type": "application/json;odata=verbose", 
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data, status, xhr) {               
                  console.log("Success");
            },
            error: function (xhr, status, error) {
                console.log("Failed");
            }        
});

3 Answers 3

0

In the body/data you have to pass CAML Query like given below on which your view will be created.

"{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView':   true, 'ViewQuery':'" + query + "'  }";

Reference:

Create List View using SharePoint REST API.

1
  • ViewQuery is used to filter the view. I want to specify the set of columns for the view. Mar 19, 2019 at 17:14
0

you can use addviewfiled in the API URI much like : _api/web/lists(guid)/views(guid)/viewfields/addviewfield('Title')

0

You can do this using the SP.ViewCollection.add method. Here's some sample code.

<div>
  <button type="button" onclick="buttonClickHandler()">Create View</button>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<script type="text/javascript">
  function buttonClickHandler() {
    var url = _spPageContextInfo.webAbsoluteUrl +
      "/_api/Web/Lists/GetByTitle('Products')/Views/Add";

    var data = {
      "parameters": {
        "__metadata": { type: "SP.ViewCreationInformation" },
        "Title": "New View",
        "ViewFields": {
          "__metadata": {
            "type": "Collection(Edm.String)"
          },
          "results": [
            "LinkTitle",
            "UnitPrice"
          ]
        },
        "Query": "<OrderBy><FieldRef Name='ID' /></OrderBy>"
      }
    };

    var call = jQuery.ajax({
      url: url,
      type: "POST",
      headers: {
        "Accept": "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
      },
      data: JSON.stringify(data),
    });
  }
</script>

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.