CAML query with custom view

Jan 28, 2010 at 6:18 PM

When I'm using getSPItemsWithQuery it seems to use the default view for that list; is there a way to specify a specific view, or another method to query a list for a custom set of fields with a query?

This way for larger lists you don't have to pull down all the data and then filter it.

Coordinator
Jan 28, 2010 at 7:41 PM

List object has a property called getSPView

/*
* name: getSPView
* purpose: populate jPoint object with items for particular view
* default: default view name is "All Items"
* visibility: public
* input: View Name (string)
* return: void
*/
getSPView:function (ViewName)

 

note default: default view name is "All Items"

getSPView('ViewName')

So you create a view on a Sharepoint List.
In jPoint you set up a list object by calling:
jP.Lists.getList("SiteURL", "ListName") /*setSPObject is deprecated as of v0.8*/
jP.Lists["ListName"].getSPView("ViewName");                  <---------- returns a list of items defined in the view, iterate trough the items to display them on screen
$.each(jP.Lists["ListName"].Items, function(idx, itemData){
//Your code goes here
});
You can also use getSPItemsWithQuery and specify CAML query, ViewFields, rowLimit and even paging token (A bit more advanced topic then just calling a view).

 

 

Jan 28, 2010 at 8:41 PM

I've seen getSPView, but the issue I ran into was I couldn't seem to filter it before the results come in with a CAML query  the way you can with getSPItemsWithQuery.

The problem I'm running into currently is that there are multiline fields I don't show on the default list view, but I want to include in the local javascript logic for results. When I try to run a caml query to get them using getSPItemsWithQuery they are left off the results... Is there something I'm missing?

Coordinator
Jan 28, 2010 at 9:05 PM

Ok I was in your shoes few months ago until I discovered ViewFields property :)

Default view brings back only columns which are showing in the view and plus few system columns...

In order to bring in few columns you need you need to set ViewFields property before you call getSPItemsWithQuery function.

Example:

jP.Lists["ListName"].ViewFields = [{title:"ID", internalName:"ID"},

{title:"Title", internalName:"LinkTitle"},

{title:"Event Time Zone", internalName:"Event_x0020_Time_x0020_Zone"},

{title:"Event Date (UTC)", internalName:"Event_x0020_Date_x0020__x0028_UT"},

{title:"Event Time (UTC)", internalName:"Event_x0020_Time_x0020__x0028_UT"},

{title:"Received By", internalName:"Received_x0020_By"}

];

var CamlQuery = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>" + myID + "</Value></Eq></Where><OrderBy><FieldRef Name='LinkTitle' Ascending='False' /></OrderBy>";

jP.Lists["ListName"].getSPItemsWithQuery(CamlQuery, 50); //Where 50 is the row limit

 

SPObj.ViewFields = [{title:"Title", internalName:"DRATitle"},
{title:"Region", internalName:"Title"},
{title:"Event Time Zone", internalName:"Event_x0020_Time_x0020_Zone"},
{title:"Event Date (UTC)", internalName:"Event_x0020_Date_x0020__x0028_UT"},
{title:"Event Time (UTC)", internalName:"Event_x0020_Time_x0020__x0028_UT"},
{title:"Received By", internalName:"Received_x0020_By"},
{title:"Reporting Facility", internalName:"Reporting_x0020_Facility"},
{title:"Event Type", internalName:"Event_x0020_Type"},
{title:"Location", internalName:"Location"},
{title:"Airline / Flight #", internalName:"Airline_x0020__x002f__x0020_Flig"},
{title:"N-Number", internalName:"N_x002d_Number"},
{title:"Aircraft Type", internalName:"Aircraft_x0020_Type"},
{title:"POB", internalName:"POB"},
{title:"Injuries", internalName:"Injuries"},
{title:"Fatalities", internalName:"Fatalities"},
{title:"Damage", internalName:"Damage"},
{title:"Description of Event", internalName:"Description_x0020_of_x0020_Event"},
{title:"Other Relevant Information", internalName:"Other_x0020_Relevant_x0020_Infor"},
{title:"DRAID", internalName:"DRAID"},
{title:"DRAVersion", internalName:"DRAVersion"}
];

 

 

Jan 29, 2010 at 12:51 PM

Awesome, that's just what I was looking for; I was playing with ViewFields when you mentioned it above, but I couldn't get the syntax right.

Thank you, that was very helpful.