0

So I'm trying to get value from my result sources by using a GET request but I'm having trouble iterating to the items as they are under: "PrimaryQueryResult.RelevantResults.Table.Rows"

And I don't know how to retrieve from there so that I can display them

Get request :

export interface ProjectInformations {
PrimaryQueryResult: ProjectInformation[];
}

public render(): void {
this._renderPersonAsync();
}

private _renderPersonAsync(): void {
this._getData()
  .then((response) => {
    this._getSearchData(response.PrimaryQueryResult);
  });
}

private _getData(): Promise<ProjectInformations> {
  return 
this.context.spHttpClient.get(this.context.pageContext.site.absoluteUrl +
   `/_api/search/query?querytext='(GetDocument:"docx")'
  &selectproperties='Title%2cPath%2cProjectStatus%2cUrl'
  &sortlist='refinablestring08:ascending'
    &properties='SourceLevel:SPSiteSubscription,SourceName:Project'`, 
  SPHttpClient.configurations.v1, {
      })
    .then((response: SPHttpClientResponse): 
    Promise<ProjectInformations> => {
    return response.json();
    });
}

Solved it by using alot of Export interfaces:

export interface ISearchResults {
PrimaryQueryResult: IPrimaryQueryResult;
}

 export interface IPrimaryQueryResult {
 RelevantResults: IRelevantResults;
 }

 export interface IRelevantResults {
 Table: ITable;
  }

 export interface ITable {
  Rows: Array<ICells>;
  }

 export interface ICells {
 Cells: Array<ICellsValue>;
 }

 export interface ICellsValue {
 Key: string;
 Value: string;
 ValueType: string;
 }
1

chain one more promise ie .then(()=>{}) and form an result array and return the result array

 if (data.PrimaryQueryResult.RelevantResults.Table.Rows) {
        var restResults = 
data.PrimaryQueryResult.RelevantResults.Table.Rows;
        var popularRestResults = new Array();

        for (var i = 0; i < restResults.length; i++) {
            var propertyResults: any = {};
            for (var j = 0; j < restResults[i].Cells.length; j++) {
                propertyResults[restResults[i].Cells[j]["Key"]] = restResults[i].Cells[j]["Value"];
            }
            popularRestResults[i] = propertyResults;
        }

        //create the ProjectInformations array by looping over popularRestResults and return the array
        return popularRestResults;
}   

hope this helps!

1

You can use this example: Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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