Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipharding/8babbc73a53a20c2f583736d7ff34ded to your computer and use it in GitHub Desktop.
Save phillipharding/8babbc73a53a20c2f583736d7ff34ded to your computer and use it in GitHub Desktop.
Search for Modern News Page Content using PnPJS
import { sp, SearchResults, SearchQueryInit, ISearchResult } from "@pnp/sp/presets/all";
console.clear();
function parseUserFormattedString(ownerString) {
const owners = ownerString.split(";")
.map( (owner) => {
const parts = owner.trim().split("|");
const oidstr = parts.splice(2,3).join("").trim();
const oids = oidstr.split(" ");
return {
email: parts[0].trim(), displayName: parts[1].trim(), oid: oids[0].trim(), loginName: oids[1].trim(),
};
});
return owners;
}
(async () => {
const web = await sp.web.select("Title")()
console.log("Web Title: ", web.Title);
// AND (PKCIShowInSpotlight=True)
// AND (PKCINewsCategory="Corporate")
// AND (PKCINewsCategory="Corporate and Democratic Services")
// AND (PKCINewsContentStatus=Active)
const query: SearchQueryInit = {
Querytext: "*",
QueryTemplate: "({searchTerms}) ((PromotedState=2) AND (ContentTypeId:0x0101009D1CB255DA76424F860D91F20E6C411800A99ACF397B2C60429DB5FF8217451DF2*) AND (PKCINewsCategory=\"Corporate\") AND (PKCINewsContentStatus=Active))",
SelectProperties: [
"PKCINewsCategory",
"PKCIShowInSpotlight",
"PKCINewsContentStatus",
"PKCIContentResourceOwner",
"userPKCIContentResourceOwner",
"Title",
"Path",
"Created",
"Filename",
"SiteLogo",
"PreviewUrl",
"PictureThumbnailURL",
"ServerRedirectedPreviewURL",
"ServerRedirectedURL",
"HitHighlightedSummary",
"FileType",
"contentclass",
"ServerRedirectedEmbedURL",
"DefaultEncodingURL",
"owstaxidmetadataalltagsinfo",
"Author",
"AuthorOWSUSER",
"SPSiteUrl",
"SiteTitle",
"UniqueID",
"ParentLink",
"SPWebUrl",
"IsContainer",
"IsListItem",
"HtmlFileType",
"OriginalPath",
"FileExtension",
"IsDocument",
"NormSiteID",
"NormWebID",
"NormListID",
"NormUniqueID",
"SiteId",
"WebId",
"ContentTypeId",
"PromotedStateOWSNMBR",
"PromotedState",
"Description",
"ContentType",
"EditorOwsUser",
"ModifiedBy",
"LastModifiedBy",
"SiteName",
"LastModifiedTime",
"ListID",
"ListItemID",
"UserName",
"ProfileImageSrc",
"Name",
"Initials",
"WebPath",
"IconUrl",
"AccentColor",
"CardType",
"TipActionLabel",
"TipActionButtonIcon",
"ClassName",
"IsExternalContent"
],
Properties: [
{
Name: "TrimSelectProperties",
Value: {
StrVal: "1",
QueryPropertyValueTypeIndex: 1
}
},
{
Name: "EnableDynamicGroups",
Value: {
BoolVal: false,
QueryPropertyValueTypeIndex: 3
}
},
{
Name: "EnableMultiGeoSearch",
Value: {
BoolVal: false,
QueryPropertyValueTypeIndex: 3
}
}
],
TrimDuplicates: false,
StartRow: 0,
RowLimit: 50,
RowsPerPage: 50,
SortList: [
{
"Property": "LastModifiedTime",
"Direction": 1
}
]
};
const results = await sp.search(query);
console.log("News search results: ", results);
console.log("News search results: ", results.PrimarySearchResults);
results.PrimarySearchResults.forEach( (item: ISearchResult) => {
console.log({
Title: item.Title,
Path: item.Path,
Thumbnail: item.PictureThumbnailURL,
Owners: parseUserFormattedString((item as any).userPKCIContentResourceOwner),
NewsCategory: (item as any).PKCINewsCategory,
NewsContentStatus: (item as any).PKCINewsContentStatus,
ShowInSpotlight: !!((item as any).PKCIShowInSpotlight).match(/True/gi),
Filename: (item as any).Filename,
ContentTypeId: item.ContentTypeId,
Author: item.Author,
AuthorEx: parseUserFormattedString((item as any).AuthorOWSUSER)[0]
});
});
})().catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment