1. @sitecore-cloudsdk/search/browser

getWidgetData

Version:

Type

Function

Import path

@sitecore-cloudsdk/search/browser

Retrieves data for one or more widgets.

Signature

export async function getWidgetData(
  widgetRequestData: WidgetRequestData,
  contextRequestData?: Context
): Promise<SearchEndpointResponse | null>

Parameters

Name

Type

Description

widgetRequestData

WidgetRequestData

Required.

Data about the widget or widgets to retrieve.

contextRequestData

Context

Optional.

Details about the site visitor's search request, such as locale, location, device, and web browser information. Sitecore Search uses this data for personalizing search results.

Return value

Returns a promise.

Examples

Example 67. Running the getWidgetData function

Next.js

Note

To run this function, you have to first initialize the Cloud SDK.

Here's an example of how to use the getWidgetData function to retrieve data for one widget:

import {
  Context,
  getWidgetData,
  SearchWidgetItem,
  WidgetRequestData,
} from "@sitecore-cloudsdk/search/browser";

const context = new Context({
  language: "en",
  country: "us",
});

// Create a widget request with the entity "product" and widget ID "rfkid_7":
const searchWidget = new SearchWidgetItem("product", "rfkid_7", {
  query: {
    keyphrase: "shoes",
  },
  content: {
    fields: ["name", "price", "brand", "image_url"],
  },
  limit: 10,
});

// Call the getWidgetData function with the widget request and the context to retrieve the data:
const apiData = await getWidgetData(new WidgetRequestData([searchWidget]), context);

Here's an example of how to use the getWidgetData function to retrieve data for multiple widgets:

import {
  Context,
  getWidgetData,
  RecommendationWidgetItem,
  SearchWidgetItem,
  WidgetRequestData,
  RecommendationWidgetItem
} from "@sitecore-cloudsdk/search/browser";

const context = new Context({
  language: "en",
  country: "us",
});

// Create a widget request with the entity "product" and widget ID "rfkid_7":
const searchWidget = new SearchWidgetItem("product", "rfkid_7", {
  query: {
    keyphrase: "shoes",
  },
  content: {
    fields: ["name", "price", "brand", "image_url"],
  },
  limit: 10,
});

// Create a second widtget request from a different type of widget (recommendation widget):
const recWidget = new RecommendationWidgetItem("product", "rfkid_2", {
  content: {
    fields: ["name", "price", "brand", "image_url"],
  },
  limit: 5,
});

// Call the getWidgetData function to get all the widget information in the same call:
const apiData = await getWidgetData(new WidgetRequestData([searchWidget, recWidget]), context);


If you have suggestions for improving this article, let us know!