Function

$().SPServices

Web Service

Views

Operation

UpdateView

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 DesOwen. Thanks, Des!

Usage:

UpdateView("http://sharepoint/sites/mysite", "project contacts","All Items")

The function below looks for the GUID based name of the list view with a title "All Items" and uses that GUID to update the view with a list of the columns we would like to use.

function UpdateView(url,list,view)
{
    var viewname = "";
    $().SPServices({
        operation: "GetViewCollection",
        webURL: url,
        async: false,
        listName: list, 
        completefunc: function (xData, Status) 
        {
            $(xData.responseXML).find("[nodeName='View']").each(function() 
            {
                 var viewdisplayname = $(this).attr("DisplayName");
                 if (viewdisplayname==view)
                 {
                       viewname = $(this).attr("Name");
                       return false;
                 }
        });
    } 
    }); 

 
  var viewfields = "<ViewFields><FieldRef Name=\"Contacts_x0020_Full_x0020_Name\" /><FieldRef Name=\"Associated_x0020_With\" /><FieldRef Name=\"Contacts_x0020_Job_x0020_Title\" /><FieldRef Name=\"Email_x0020_Address\" /><FieldRef Name=\"Mobile_x0020_Telephone\" /><FieldRef Name=\"Business_x0020_Telephone\" /></ViewFields>";
  $().SPServices({
        operation: "UpdateView",
        webURL: url,
        async: false,
        listName: list, 
        viewName: viewname,
        viewFields: viewfields,
        completefunc: function (xData, Status) 
        {
        } 

    }); 

}

Last edited May 5, 2012 at 3:42 PM by sympmarc, version 1

Comments

williamnevarez Jun 3, 2013 at 9:37 PM 
Ive tried adding to the list view as a script to run at document ready and it does not update the view. How do you cause this script to run?