Question

I have a getItems function that accepts a filter, but I need to know how to pass in two different filters so that I can retrieve items filtered by both Age and First Name.

Here is the definition of the function:

public getItems(list: string, id: string, filters?: string[]): Promise<any[]> {

    let filterStr: string = 'startswith(ContentTypeId,\''+ id +'\')';
      return new Promise((resolve, reject) => {
        let query:any = pnp.sp.web.lists.getById(list).items;
        if(filters)
            filters.map((rowFilter: string) => { filterStr += ' and '+ rowFilter; });
            console.log(filters)
                query.filter(filterStr).get().then(Items => {

                resolve(Items);
            }).catch(e => {
                reject(e);
            });
    });
}`
Was it helpful?

Solution

You don't need to change the definition of the getItems function at all. As it is written, you actually have two different options. The way that method is concatenating filter strings together, you can just specify to criteria in a single parameter, for example:

this.getItems(mylistguid, myctid, "firstnamecol eq 'Alice' and agecol eq 27");

But that method is already set up to accept an array of filter strings, and concatenate them together with and's, so you could also call the method with a declared array (e.g. [filterString1, filterString2]) like this:

this.getItems(mylistguid, myctid, ["firstnamecol eq 'Alice'", "agecol eq 27"]);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top