Function

$().SPServices

Web Service

Views

Operation

AddView

Notes

  • This operation accepts a webURL option. This allows you to change the context for the operation to a different site. Using a GUID for the listName does NOT change the context as it does with the Object Model, so you need to use the webURL option as well if your list is in a different site.

Examples

This example comes from a discussion thread with whiskers1978.

$().SPServices({
    operation: "AddView",
    listName: "Test List 2",
    viewName: "Test2", 
    viewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='State' /></ViewFields>", 
    query: "<Query><Where><Eq><FieldRef Name='State' /><Value Type='Text'>">Massachusetts</Value></Eq></Where></Query>", 
    rowLimit: "<RowLimit paged='True'>100</RowLimit>",
    type: "HTML",
    makeViewDefault: false,
    completefunc: function (xData, Status) {
        alert(xData.responseText);
    }
}); 

Note that if you omit the Type in the Where clause, your view will be created, but it will throw an error. See the thread for more details.

Last edited Jul 10, 2013 at 4:59 PM by sympmarc, version 1

Comments

gkoliver Aug 27, 2013 at 8:13 PM 
Marc--this code was quite helpful, especially considering the MSDN site identifies the 'viewName' parameter in a confusing manner. However, there's a minor typo in the sample above. Notice, in the 'query' parameter, there is an extra "> just before the word Massachusetts.