UpdateView

Apr 20, 2012 at 10:34 AM

Hi Marc

I've enjoyed the benefit of many examples posted by generous users so I thought I'd better start posting a few of my own.  Here is how I update views on a particular list with the new set of columns that I'd like to see in the view...

Calling example:

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) 
        {
        } 

    }); 

}


Hope it helps someone.

Des

Apr 20, 2012 at 1:26 PM

Nice!!!

*** Challenge ***

Do you think you can you add a GetList call to grab the <ViewFields> so they aren't hardcoded?

 

:-P

Cheers,

Matt

Apr 20, 2012 at 2:48 PM
Hi Matt
 
You could of course grab the field names available within the list by using GetList (you have an example posted I see) and parse them into an array but you'd still need to select the fields you want in the view and include them in the right order.  I usually do something like that to grab the internal names so I can create the ViewFields list I need.  The same UpdateView service also allows you to set several other view properties including the query to fetch the data.

Des

From: iOnline247 [email removed]
Sent: 20 April 2012 13:27
To: des.owen@thefoxgroup.co.uk
Subject: Re: UpdateView [SPServices:352952]

From: iOnline247

Nice!!!

*** Challenge ***

Do you think you can you add a GetList call to grab the <ViewFields> so they aren't hardcoded?

:-P

Cheers,

Matt

Coordinator
May 5, 2012 at 4:43 PM

Des:

Thanks a lot for the example. It's taken me a while, but I've added it into the docs:
http://spservices.codeplex.com/wikipage?title=UpdateView

M.