首页 > 解决方案 > 在 Sharepoint 中使用 pnp 搜索检索列表元素

问题描述

这是在 Sharepoint 中使用 pnp 搜索检索列表元素的正确方法吗

 pnp.sp.search("ListName").then((r: SearchResults) => {
        console.log(r.ElapsedTime);
        console.log(r.RowCount);
        console.log(r.PrimarySearchResults);
    });

标签: sharepoint

解决方案


搜索是直接从根 sp 对象访问的,可以采用表示查询文本的字符串、与SearchQuery接口匹配的普通对象或SearchQueryBuilder实例。前两个如下所示。

import pnp, { SearchQuery, SearchResults } from "sp-pnp-js";

// text search using SharePoint default values for other parameters
pnp.sp.search("test").then((r: SearchResults) => {

    console.log(r.ElapsedTime);
    console.log(r.RowCount);
    console.log(r.PrimarySearchResults);
});

// define a search query object matching the SearchQuery interface
pnp.sp.search(<SearchQuery>{
    Querytext: "test",
    RowLimit: 10,
    EnableInterleaving: true,
}).then((r: SearchResults) => {

    console.log(r.ElapsedTime);
    console.log(r.RowCount);
    console.log(r.PrimarySearchResults);
});

搜索 Office 365 组。为确保您在 Office 365 中搜索所有组,请参阅下面的示例,其中包括 " EnableDynamicGroups" 属性。

import pnp, { SearchQueryBuilder, SearchResults, SearchQuery } from "sp-pnp-js";

const _searchQuerySettings: SearchQuery = {
    TrimDuplicates: false,
    RowLimit: 500,
    SelectProperties: ["Title", "SPWebUrl", "projectID"],
    Properties: [{
      Name: "EnableDynamicGroups",
      Value: {
        BoolVal: true,
        QueryPropertyValueTypeIndex: 3
      }
    }]
}

let q = SearchQueryBuilder.create("ContentType:ProsjektInformasjon", _searchQuerySettings).rowLimit(500);

pnp.sp.search(q).then(res => { 
    console.log(res.PrimarySearchResults.length);
    console.dir(res.PrimarySearchResults)
});

推荐阅读