pnp.sp.search > Add property to searchresults

%3CLINGO-SUB%20id%3D%22lingo-sub-115026%22%20slang%3D%22en-US%22%3Epnp.sp.search%20%26gt%3B%20Add%20property%20to%20searchresults%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-115026%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20this%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Eprivate%20GetSearchresults()%3A%20void%20%7B%0A%20%20%20%20%20pnp.sp.search(%26lt%3BSearchQuery%26gt%3B%7B%0A%20%20%20%20%20%20Querytext%3A%20%22ContentType%3ATEST_matters%22%2C%0A%20%20%20%20%20%20RowLimit%3A%2010%2C%0A%20%20%20%20%20%20EnableInterleaving%3A%20true%2C%0A%20%20%20%20%20%20SelectProperties%3A%20%5B%22RefinableString10%22%2C%20%22CreatedBy%22%2C%22Created%22%5D%2C%20%0A%20%20%20%20%20%20RefinementFilters%3A%20%5B%22RefinableString18%3Aequals('false')%22%5D%2C%0A%20%20%20%20%20%20%7D).then((result%3A%20SearchResults)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20var%20props%20%3D%20result.PrimarySearchResults%3B%0A%20%20%20%20%20%20%20%20debugger%3B%0A%20%20%20%20%0A%20%20%20%20%20var%20propValue%20%3D%20%22%22%3B%0A%20%20%20%20%20var%20counter%20%3D%201%3B%0A%20%20%20%20%20props.forEach(function(object)%20%7B%0A%20%20%20%20%20propValue%20%2B%3D%20counter%2B%2B%20%2B'.%20Title%20-%20'%20%2Bobject.Title%20%2B%22%26lt%3Bbr%2F%26gt%3B%22%2B%22Rank%20-%20%22%20%2B%20object.Rank%0A%20%20%20%20%20%20%2B%22%26lt%3Bbr%2F%26gt%3B%22%2B%22File%20Type%20-%20%22%20%2B%20object.FileType%2B%22%26lt%3Bbr%2F%26gt%3B%22%2B%20%22Original%20Path%20-%20%22%0A%20%20%20%20%20%20%2Bobject.OriginalPath%20%2B%22%26lt%3Bbr%2F%26gt%3B%22%2B%22%20Summary%20-%20%22%2B%20object.HitHighlightedSummary%20%2B%0A%20%20%20%20%20%20%22%26lt%3Bbr%2F%26gt%3B%22%2B%22%26lt%3Bbr%2F%26gt%3B%22%3B%0A%20%20%20%20%20%7D)%3B%3C%2FPRE%3E%3CP%3ENow%20I%20want%20to%20have%20the%26nbsp%3B%20%22RefinableString10%22%20field%20like%20this%20%22object.RefinableString10%22.%20But%20I%20get%20an%20error%20that%20%22RefinableString10%22%20does%20not%20exist%20in%20%22SearchResults%22.%20However%2C%20in%20debug%2C%20I%20can%20see%20%22RefinableString10%22%20which%20is%20proper%20filled.%20I%20suppose%20I%20need%20to%20declare%20something%3F%20But%20I%20do%20not%20have%20any%20clue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%20Mike%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-115026%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExtensibility%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-272108%22%20slang%3D%22en-US%22%3ERe%3A%20pnp.sp.search%20%26gt%3B%20Add%20property%20to%20searchresults%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-272108%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20what%20I%20did%20to%20fix%20this%20problem.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20I%20create%20other%20Interface%20that%20extends%20from%20the%20SearchResult%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3Eexport%3C%2FSPAN%3E%20%3CSPAN%3Einterface%3C%2FSPAN%3E%3CSPAN%3E%3CSTRONG%3E%20MySearchResult%3C%2FSTRONG%3E%20%3C%2FSPAN%3E%3CSPAN%3Eextends%3C%2FSPAN%3E%3CSPAN%3E%3CSTRONG%3E%20pnp.SearchResult%3C%2FSTRONG%3E%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3ERefinableString10%3C%2FSPAN%3E%3CSPAN%3E%3F%3A%3C%2FSPAN%3E%3CSPAN%3Estring%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EThan%20instead%20of%20use%20the%20parameter%20%3CSTRONG%3Eresult%3A%20SearchResult%3C%2FSTRONG%3E%20i%20used%20%3CSTRONG%3Eresult%3A%20MySearchResult%3C%2FSTRONG%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EIn%20the%20example%20should%20be%20instead%20of%20%3CSTRONG%3Efunction(object%3C%2FSTRONG%3E)%20you%20could%20use%20%3CSTRONG%3Eresult%3AMySearchResult%3C%2FSTRONG%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-271736%22%20slang%3D%22en-US%22%3ERe%3A%20pnp.sp.search%20%26gt%3B%20Add%20property%20to%20searchresults%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-271736%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20having%20this%20same%20problem%20right%20now%20with%20one%20search%20solution.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDid%20you%20found%20other%20way%20to%20make%20it%20work%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-115042%22%20slang%3D%22en-US%22%3ERe%3A%20pnp.sp.search%20%26gt%3B%20Add%20property%20to%20searchresults%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-115042%22%20slang%3D%22en-US%22%3E%3CP%3EFor%20now%20I%20added%20the%20%22RefinableString10%22%20to%20the%20%22SearchResult%22%20declaration%20in%20%22search.d.ts%22.%20It%20works%20but%20is%20this%20the%20way%20to%20go%3F%20Ain't%20it%20possible%20to%20add%20a%20property%20to%20the%20SearchResults%20in%20the%20webpart.ts%20file%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Regular Contributor

I have this code:

 

private GetSearchresults(): void {
     pnp.sp.search(<SearchQuery>{
      Querytext: "ContentType:TEST_matters",
      RowLimit: 10,
      EnableInterleaving: true,
      SelectProperties: ["RefinableString10", "CreatedBy","Created"], 
      RefinementFilters: ["RefinableString18:equals('false')"],
      }).then((result: SearchResults) => {
        var props = result.PrimarySearchResults;
        debugger;
    
     var propValue = "";
     var counter = 1;
     props.forEach(function(object) {
     propValue += counter++ +'. Title - ' +object.Title +"<br/>"+"Rank - " + object.Rank
      +"<br/>"+"File Type - " + object.FileType+"<br/>"+ "Original Path - "
      +object.OriginalPath +"<br/>"+" Summary - "+ object.HitHighlightedSummary +
      "<br/>"+"<br/>";
     });

Now I want to have the  "RefinableString10" field like this "object.RefinableString10". But I get an error that "RefinableString10" does not exist in "SearchResults". However, in debug, I can see "RefinableString10" which is proper filled. I suppose I need to declare something? But I do not have any clue.

 

Thanks, Mike 

 

3 Replies

For now I added the "RefinableString10" to the "SearchResult" declaration in "search.d.ts". It works but is this the way to go? Ain't it possible to add a property to the SearchResults in the webpart.ts file?

I'm having this same problem right now with one search solution.

 

Did you found other way to make it work?

 

 

This is what I did to fix this problem.

 

First I create other Interface that extends from the SearchResult:

 

export interface MySearchResult extends pnp.SearchResult{
RefinableString10?:string;
}
 
Than instead of use the parameter result: SearchResult i used result: MySearchResult
 
In the example should be instead of function(object) you could use result:MySearchResult