Using CAML queries in SPFx or SharePoint PnP

%3CLINGO-SUB%20id%3D%22lingo-sub-745696%22%20slang%3D%22en-US%22%3EUsing%20CAML%20queries%20in%20SPFx%20or%20SharePoint%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-745696%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20attempting%20to%20recreate%20a%20webpart%20I%20made%20for%20SharePoint%20classic%20as%20a%20SharePoint%20modern%20webpart.%20I%20need%20to%20use%20a%20CAML%20query%20to%20get%20the%20one%20person%20who%20is%20on-call%20in%20our%20department%20(kept%20in%20a%20SharePoint%20calendar).%20I%20can%20only%20find%20very%20simple%20query%20examples%20but%20I%20need%20to%20use%20a%20complex%20CAML%20query.%201)%20can%20someone%20show%20me%20where%20to%20find%20documentation%20on%20what%20I%20am%20trying%20to%20do%202)%20can%20someone%20tell%20me%20what%20I%20am%20doing%20wrong%20in%20this%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3Eprivate%3C%2FSPAN%3E%3CSPAN%3EgetOnCall%3C%2FSPAN%3E%3CSPAN%3E()%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3Evoid%3C%2FSPAN%3E%3CSPAN%3E%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Econst%20context%3A%20SP.ClientContext%20%3D%20new%20SP.ClientContext(this.properties.siteUrl)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Econst%3C%2FSPAN%3E%3CSPAN%3Elist%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3ESP%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EList%3C%2FSPAN%3E%3CSPAN%3E%3D%3C%2FSPAN%3E%3CSPAN%3Econtext%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Eget_web%3C%2FSPAN%3E%3CSPAN%3E().%3C%2FSPAN%3E%3CSPAN%3Eget_lists%3C%2FSPAN%3E%3CSPAN%3E().%3C%2FSPAN%3E%3CSPAN%3EgetByTitle%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3E'On%20Call'%3C%2FSPAN%3E%3CSPAN%3E)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Econtext%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EloadQuery%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3Elist%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3E'%3CVIEW%3E%3CVIEWFIELDS%3E%3CFIELDREF%20name%3D%22%26quot%3BTitle%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CFIELDREF%20name%3D%22%26quot%3BEventDate%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CFIELDREF%20name%3D%22%26quot%3BEndDate%26quot%3B%22%3E%3C%2FFIELDREF%3E%3C%2FVIEWFIELDS%3E%3CQUERY%3E%3CWHERE%3E'%3C%2FWHERE%3E%3C%2FQUERY%3E%3C%2FVIEW%3E%3C%2FSPAN%3E%3CSPAN%3E%2B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E'%3CAND%3E%3CCONTAINS%3E%3CFIELDREF%20name%3D%22%26quot%3BTitle%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CVALUE%20type%3D%22%26quot%3BText%26quot%3B%22%3EOn-Call%3C%2FVALUE%3E%3C%2FCONTAINS%3E%3CAND%3E%3CLEQ%3E%3CFIELDREF%20name%3D%22%26quot%3BEventDate%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CVALUE%20includetimevalue%3D%22%26quot%3BTrue%26quot%3B%22%20type%3D%22%26quot%3BDateTime%26quot%3B%22%3E'%3C%2FVALUE%3E%3C%2FLEQ%3E%3C%2FAND%3E%3C%2FAND%3E%3C%2FSPAN%3E%3CSPAN%3E%2B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E'%3CTODAY%3E%3C%2FTODAY%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CGEQ%3E%3CFIELDREF%20name%3D%22%26quot%3BEndDate%26quot%3B%22%3E%3C%2FFIELDREF%3E%3CVALUE%20includetimevalue%3D%22%26quot%3BTrue%26quot%3B%22%20type%3D%22%26quot%3BDateTime%26quot%3B%22%3E%3CTODAY%3E%3C%2FTODAY%3E%3C%2FVALUE%3E%3C%2FGEQ%3E'%3CSPAN%3E)%3B%3C%2FSPAN%3E%3CDIV%3E%3CSPAN%3Econtext%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EexecuteQueryAsync%3C%2FSPAN%3E%3CSPAN%3E((%3C%2FSPAN%3E%3CSPAN%3Esender%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3Eany%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3Eargs%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3ESP%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EClientRequestSucceededEventArgs%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3Evoid%3C%2FSPAN%3E%3CSPAN%3E%3D%26gt%3B%3C%2FSPAN%3E%3CSPAN%3E%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Econst%3C%2FSPAN%3E%3CSPAN%3ElistEnumerator%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3EIEn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3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Eget_item%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3E'Title'%3C%2FSPAN%3E%3CSPAN%3E)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%2C%20(%3C%2FSPAN%3E%3CSPAN%3Esender%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3Eany%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3Eargs%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3ESP%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3EClientRequestFailedEventArgs%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3CSPAN%3Evoid%3C%2FSPAN%3E%3CSPAN%3E%3D%26gt%3B%3C%2FSPAN%3E%3CSPAN%3E%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CLINGO-LABS%20id%3D%22lingo-labs-745696%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ecaml%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-747440%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20CAML%20queries%20in%20SPFx%20or%20SharePoint%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-747440%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288947%22%20target%3D%22_blank%22%3E%40smithme%3C%2FA%3E%26nbsp%3BYour%20top%20level%20view%20tag%20should%20be%20lower%20case%20i.e.%20%3CVIEW%3E.%20There's%20a%20good%20app%20on%20the%20Microsoft%20Store%20called%20SmartCAML%20that%20excellent%20for%20test%20CAML%20queries%20in%20SharePoint%3C%2FVIEW%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-748208%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20CAML%20queries%20in%20SPFx%20or%20SharePoint%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-748208%22%20slang%3D%22en-US%22%3EThank%20you%20for%20pointing%20me%20to%20SmartCAML.%20My%20problem%20has%20been%20with%20the%20syntax%20for%20calling%20a%20load%20query%20because%20the%20examples%20I%20looked%20at%20don't%20seem%20to%20use%20CAML.%3C%2FLINGO-BODY%3E
Contributor

I am attempting to recreate a webpart I made for SharePoint classic as a SharePoint modern webpart. I need to use a CAML query to get the one person who is on-call in our department (kept in a SharePoint calendar). I can only find very simple query examples but I need to use a complex CAML query. 1) can someone show me where to find documentation on what I am trying to do 2) can someone tell me what I am doing wrong in this code.

 

private getOnCall(): void {
const context: SP.ClientContext = new SP.ClientContext(this.properties.siteUrl);
const list: SP.List = context.get_web().get_lists().getByTitle('On Call');
context.loadQuery(list, '<View><ViewFields><FieldRef Name="Title" /><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /></ViewFields><Query><Where>' +
'<And><Contains><FieldRef Name="Title" /><Value Type="Text">On-Call</Value></Contains><And><Leq><FieldRef Name="EventDate" /><Value IncludeTimeValue="True" Type="DateTime">' +
'<Today /></Value></Leq><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="True" Type="DateTime"><Today /></Value></Geq></And></And></Where></Query></View>');
context.executeQueryAsync((sender: any, args: SP.ClientRequestSucceededEventArgs): void => {
const listEnumerator: IEnumerator<SP.ListItem> = list.getEnumerator();

var title: string;
while (listEnumerator.moveNext()) {
const item: SP.ListItem = listEnumerator.get_current();
title = item.get_item('Title');
}
}, (sender: any, args: SP.ClientRequestFailedEventArgs): void => {
 
}
}

 

2 Replies

@smithme Your top level view tag should be lower case i.e. <view>. There's a good app on the Microsoft Store called SmartCAML that excellent for test CAML queries in SharePoint

Thank you for pointing me to SmartCAML. My problem has been with the syntax for calling a load query because the examples I looked at don't seem to use CAML.