Retrieve data from SharePoint list via PnP JS and CAML Query
-
08-02-2021 - |
Domanda
I wanted to display all files which have the property "Job" = "Job1".
How to retrieve list view items using PnP JS and CAML Query?
Here is the CAML Query?
<ViewFields>
<FieldRef Name='Job' />
<FieldRef Name='FileRef' />
<FieldRef Name='FileLeafRef' />
<FieldRef Name='ParentUniqueId' />
</ViewFields>
<QueryOptions>
<ViewAttributes Scope='RecursiveAll' />
<Lists ServerTemplate='101' />
</QueryOptions>
<Where>
<And>
<And>
<And>
<Eq>
<FieldRef Name='job' />
<Value Type='MultiChoice'>Job1</Value>
</Eq>
<Eq>
<FieldRef Name='lastName' LookupId='True' />
<Value Type='Lookup'>1</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='LastName2' LookupId='True' />
<Value Type='Lookup'>1</Value>
</Eq>
</And>
<Eq>
<FieldRef Name='FSObjType' />
<Value Type='Integer'>0</Value>
</Eq>
</And>
</Where>
Soluzione
If you have figured out the correct CAML query for your requirements then you can get items from SharePoint using a CAML Query like below:
import { ICamlQuery } from "@pnp/sp/lists";
// build the caml query object (in this example, we include Title field and limit rows to 5)
const caml: ICamlQuery = {
ViewXml: "<View><ViewFields><FieldRef Name='Title' /></ViewFields><RowLimit>5</RowLimit></View>",
};
// get list items
const r = await list.getItemsByCAMLQuery(caml);
// log resulting array to console
console.log(r);
Reference:
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange