Forum Discussion
smithme
Jul 09, 2019Copper Contributor
Using CAML queries in SPFx or SharePoint PnP
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
Sort By
- Toby StathamCopper Contributor
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
- smithmeCopper ContributorThank 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.