0

I made search in SPFx, now i need to display only list items. My code example:

pnp.sp.search({
        Querytext: query,
        RowLimit: 20,
        StartRow: 0,
        QueryTemplate: "contentclass:sts_listitem"
    });

But I get not only list items.

0

2 Answers 2

1

Only sts_listitem would not sufficient because document is also considered as ListItem in SharePoint as we can still set metadata there.

Possible solution could use STS_ListItem_GenericList which will gives returns result from only custom lists.

Make sure that by using STS_ListItem_GenericList, it will not give result from special list types e.g. Events, Tasks, Discussion Boards, Announcements, etc. So if you want result from those special lists as well then you can add all possible option in query template.

You can find possible option here. (Unfortunately I couldn't find any official link from Microsoft)

So based on above, below code would give you result from all custom lists and also from all contact lists.

pnp.sp.search({
    Querytext: query,
    RowLimit: 20,
    StartRow: 0,
    QueryTemplate: "(contentclass:STS_ListItem_GenericList OR contentclass:STS_ListItem_Contacts)"
});

You can add additional condition based on your requirement with OR condition in QueryTemplate

0

Please refer to the following code to using pnp.sp.search :

import { spfi } from "@pnp/sp";
import "@pnp/sp/search";
import { ISearchQuery, SearchResults, SearchQueryBuilder } from "@pnp/sp/search";

const sp = spfi(...);

// text search using SharePoint default values for other parameters
const results: SearchResults = await sp.search("test");

console.log(results.ElapsedTime);
console.log(results.RowCount);
console.log(results.PrimarySearchResults);


// define a search query object matching the ISearchQuery interface
const results2: SearchResults = await sp.search(<ISearchQuery>{
    Querytext: "test",
    RowLimit: 10,
    EnableInterleaving: true,
});

console.log(results2.ElapsedTime);
console.log(results2.RowCount);
console.log(results2.PrimarySearchResults);

// define a query using a builder
const builder = SearchQueryBuilder("test").rowLimit(10).enableInterleaving.enableQueryRules.processPersonalFavorites;
const results3 = await sp.search(builder);

console.log(results3.ElapsedTime);
console.log(results3.RowCount);
console.log(results3.PrimarySearchResults);

More information for reference: https://pnp.github.io/pnpjs/sp/search/

Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.