viewName for GetListItems

Jan 20, 2010 at 4:54 PM

Hello!

I'm currently working on a project in SP where I'm using GetListItems to pull through a "Calendar-like" Custom List. Currently, GetListItems doesn't let you set the viewName attribute, and I think that would be really handy. My problem is: looking at the default Calendar view, SPServices only sees list items from the current month. I'd like all of the events. On top of that, I'd like to pull from a view I made that has fewer fields and events are filtered by date (in place of CAML).

I made a small change to GetListAndView (see below). However, now my method call needs the view GUID, but it would be nice to just be able to use the name, but I don't know where those changes go.

theListItems = $().SPServices({
	operation:'GetListItems',	
	listName: 'MyList',
	viewName: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',	
	//...
	completefunc: function (xData, Status){		
		//...
	}
});

I know that I've probably commited some pretty heinous offenses in customizing my SPServices lib, but these changes worked for me to get the above to work. Forgive me, I'm new :-)

 case "GetListItems":
SOAPEnvelope.payload += wrapNode("listName", opt.listName);
SOAPEnvelope.payload += wrapNode("viewFields", opt.CAMLViewFields);
SOAPEnvelope.payload += wrapNode("query", opt.CAMLQuery);
SOAPEnvelope.payload += wrapNode("rowLimit", opt.CAMLRowLimit);
SOAPEnvelope.payload += wrapNode("queryOptions", opt.CAMLQueryOptions);

----Revised----
//jquery.SPServices-0.4.7.js LINES: 275-281

case "GetListItems":
     SOAPEnvelope.payload += wrapNode("listName", opt.listName);
     SOAPEnvelope.payload += wrapNode("viewName", opt.viewName);
     SOAPEnvelope.payload += wrapNode("viewFields", opt.CAMLViewFields);
     SOAPEnvelope.payload += wrapNode("query", opt.CAMLQuery);
     SOAPEnvelope.payload += wrapNode("rowLimit", opt.CAMLRowLimit);
     SOAPEnvelope.payload += wrapNode("queryOptions", opt.CAMLQueryOptions);   
     break;

Thoughts?

Thanks!

Andrew

Jan 20, 2010 at 5:09 PM
Edited Jan 20, 2010 at 6:21 PM

I found a JavaScript API that has viewName pulled through for its getListItems:

SPAPI

http://darrenjohnstone.net/2008/07/22/a-cross-browser-javascript-api-for-the-sharepoint-and-office-live-web-services/

Coordinator
Jan 20, 2010 at 5:10 PM

Yikes! I can't believe that I missed that parameter and that no one else has had this issue yet!

Looking at the SDK for the GetListItems operation, the value does indeed need to be the GUID.

I've just updated my working version to include the viewName just like you have.  (You may be new, but you got this one right.) I also reordered the values to match the SDK.

case "GetListItems":
    SOAPEnvelope.payload += wrapNode("listName", opt.listName);
    SOAPEnvelope.payload += wrapNode("viewName", opt.viewName);
    SOAPEnvelope.payload += wrapNode("query", opt.CAMLQuery);
    SOAPEnvelope.payload += wrapNode("viewFields", opt.CAMLViewFields);
    SOAPEnvelope.payload += wrapNode("rowLimit", opt.CAMLRowLimit);
    SOAPEnvelope.payload += wrapNode("queryOptions", opt.CAMLQueryOptions);
    break;

M.

Coordinator
Jan 20, 2010 at 5:14 PM

p.s. Did you try setting the rowLimit to 0 in your initial call? If you specify it, the value will override the default for the view; 0 pulls back all of the items.  This is true for viewFields and query as well.

Jan 20, 2010 at 8:38 PM

Cool - I will definitely have to keep looking into the intricacies of your library. So far it has been really useful and a good learning experience. On this note, maybe you could offer some insight on another topic I've been thinking about...

I'm pretty concerned about how sluggish things can easily become in SharePoint if there is a lot of content being managed. As it stands now, I call SPServices once on page load and store the xml response (ranging from 25-100 items). Each time a click event happens, I parse through the entire xml using jQuery's .each function and some other logic.

i.e.

$(xData.responseXML).find("z\\:row").each(function(i) {
    if(...) {
        //do something
    }
});

But, I was thinking there might be an advantange in slapping together a CAML Query on the fly and asking for smaller chunks of data as needed on each event. So, I realize there's going to be a trade off, jQuery xml parsing vs. CAML query efficiency. Any insight would be great because I'm not sure whether I should be calling more smaller SOAP requests with less client parsing vs. fewer, larger SOAP requests with more client parsing.

I suppose posting this somewhere on EUSP might be a good idea?

Thanks,

Andrew 

Coordinator
Jan 20, 2010 at 8:46 PM

Seems like a lot of work to get around a performance issue.  It might be better to get to the root cause of the sluggishness.  That said, GetListItems supports server side paging, just like regular Web Parts.  You can see how this works toward the bottom of the SDK page for GetListItems.

M.

Mar 26, 2010 at 7:45 PM
Hi asporter and Marc,
I need help with using viewname in getlistitems. I am suing Marc's Jquery library and I am using "viewname" in my call to GetListItems.
I got the correct GUID in there and I expected that my CAML query would look into my view items rather than list items.
But the results are coming from my list rather than view. Is my understanding wrong?

My view has 2 records where as my list (All Items view) has 3 records.
Now since I used viewname (GUID) in my getlistitems call, should I not be seeing only 2 records? Why do my query returns all 3 records? Please help.
Coordinator
Mar 26, 2010 at 7:51 PM

it2me:

It depends what else you are specifying in your call.  Basically, if you provide other parameters, you are going to override the view.

If you could paste in your code, I can take a look.

M.

Mar 29, 2010 at 6:38 PM

Hi Marc,

Thanks for your response. Here is my code. I tried commenting "CAMLRowLimit", but no change in the outcome.

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "<listname>",
    viewName: "{BF4C2E40-32A3-4992-992A-D376A160A1A9}",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name=\"field1\"/><Value Type=\"Text\">val1</Value></Eq></Where></Query>",
    CAMLRowLimit: 0,
    completefunc: function(xData, Status)
         {
           $(xData.responseXML).find("z\\:row").each(function()
       {
               $("#html").append("<br>");
               queryResult = $(this).attr("ows_Handoff_x0020_Name");
               queryResult+=$(this).attr("ows_Software_x0020_Release");
               queryResult+= $(this).attr("ows_Target_x0020_Biz_x002f_Region");
               queryResult+= $(this).attr("ows_Handoff_x0020_request_x0020_rece");
               $("#html").append(queryResult);
                                 });
         },
        debug: true
});

Coordinator
Mar 29, 2010 at 7:15 PM

it2me:

Because you are specifying both:

CAMLQuery: "<Query><Where><Eq><FieldRef Name=\"field1\"/><Value Type=\"Text\">val1</Value></Eq></Where></Query>",
CAMLRowLimit: 0,

you're effectlvely overriding the view. (Setting CAMLRowLimit: 0 is basically saying "send me all the items regardless of the view settings".)

BTW, you can avoid escaping the quotes (and make your CAML a little more readable) by using single quotes:

CAMLQuery: "<Query><Where><Eq><FieldRef Name='field1'/><Value Type='Text'>val1</Value></Eq></Where></Query>",

M.

Mar 29, 2010 at 7:40 PM

Marc, I already tried removing the "CAMLRowLimit" (and also setting it to a non zero value) but still it returned all the records from the main list. See below code that I tried.

Any input is greatly appreciated. Thanks.

 

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "<listname>",
    viewName: "{BF4C2E40-32A3-4992-992A-D376A160A1A9}",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='Field1'/><Value Type='Text'>Val1</Value></Eq></Where></Query>",
    completefunc: function(xData, Status)
         {
           $(xData.responseXML).find("z\\:row").each(function()
             {

               queryResult = $(this).attr("ows_Handoff_x0020_Name");
               queryResult+=$(this).attr("ows_Software_x0020_Release");
               queryResult+= $(this).attr("ows_Target_x0020_Biz_x002f_Region");
               queryResult+= $(this).attr("ows_Handoff_x0020_request_x0020_rece");
              });
         },
        debug: true
});

 

Coordinator
Mar 29, 2010 at 7:47 PM

Try removing the CAMLQuery as well.

M.

Mar 29, 2010 at 8:20 PM

It is working, but defeats the purpose. I want to run a query on my view rather than a complete list.

I should be able to do a CAML query on my view items.

Coordinator
Mar 29, 2010 at 8:40 PM

Well, by specifying the CAMLQuery, you're overriding the view settings.  You can simply have the CAMLQuery reflect the same filtering which you are doing in the view.

This is the way the Web Service works, not my implementation of it, AFAIK.

M.

Mar 29, 2010 at 8:55 PM

That is fine. Thanks for all your help! I will probably think of a work around.

 

Oct 18, 2012 at 12:38 PM

The following page explains how to extract the GUID for a View

http://www.dhirajranka.com/?p=50

It's the GUID that is the value for viewName