Tuesday, 9 January 2018

Building SharePoint Search Queries using PnP JavaScript Core Library

PnP JavaScript core library provides the interfaces required for building the queries for searching SharePoint content.

There are multiple ways of building the queries for PnP JS core search.
  • One way is to use the Search Query interface directly with necessary query parameters
  • The other way is to build the query using Search query builder syntax along with other query parameters.
  • The default way passing just the query text to the search method.

PnP JS core acts as a wrapper for the interacting with the SharePoint data over the object models or the REST APIs available. The samples or syntax explained below are compatible for JavaScript editors.

Search Query Interface

The query parameters are used for filtering the data using PnP JS. These parameters are composed using the search query builder object and then the object is injected into SharePoint to retrieve the results. The parameters can be query text, query template, trim duplicates boolean value, row limit, source ID, select properties, refinement filters, refiners, query tag, etc.
    Querytext?: string,  
    QueryTemplate?: string;
    EnableInterleaving?: boolean;
    EnableStemming?: boolean;
    TrimDuplicates?: boolean;
    EnableNicknames?: boolean;
    EnableFQL?: boolean;
    EnablePhonetic?: boolean;
    BypassResultTypes?: boolean;
    ProcessBestBets?: boolean;
    EnableQueryRules?: boolean;
    EnableSorting?: boolean;
    GenerateBlockRankLog?: boolean;
    SourceId?: string;
    RankingModelId?: string;
    StartRow?: number;
    RowLimit?: number;
    RowsPerPage?: number;
    SelectProperties?: string[];
    Culture?: number;
    RefinementFilters?: string[];
    Refiners?: string;
    HiddenConstraints?: string;
    SortList?: Sort[];
    Timeout?: number;
    HitHighlightedProperties?: string[];
    ClientType?: string;
    PersonalizationData?: string;
    ResultsUrl?: string;
    QueryTag?: string[];
    Properties?: SearchProperty[];
    ProcessPersonalFavorites?: boolean;
    QueryTemplatePropertiesUrl?: string;
    ReorderingRules?: ReorderingRule[];
    HitHighlightedMultivaluePropertyLimit?: number;
    EnableOrderingHitHighlightedProperty?: boolean;
    CollapseSpecification?: string;
    UIlanguage?: number;
    DesiredSnippetLength?: number;
    MaxSnippetLength?: number;
    SummaryLength?: number;

The following sample shows building the search filter queries with query interface. The query has the parameters like query text, row limit, properties, and trim duplicating Boolean value.

Search Query Builder

Search Query Builder class has multiple ways for building the queries. It helps building queries in fluent ways. It has methods and properties, which matches the properties of search query interface.

The following sample shows building the search filter using the search query builder class present with PnP JavaScript core library.

The following syntax shows the same query but with different way of using the Search Query Builder instance.

Default Search

The following snippet shows the default search with search text using pnp javascript core library.