1. @sitecore-cloudsdk/search/server

getWidgetData

Version:

Type

Function

Import path

@sitecore-cloudsdk/search/server

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 74. 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 type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { CloudSDK } from "@sitecore-cloudsdk/core/server";
import {
  Context,
  getWidgetData,
  SearchWidgetItem,
  WidgetRequestData,
} from "@sitecore-cloudsdk/search/server";

export default async function middleware(request: NextRequest) {
  const response = NextResponse.next();

  // Initialize the SDK and the package...

  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);

  return response;
};

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

import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { CloudSDK } from "@sitecore-cloudsdk/core/server";
import {
  Context,
  getWidgetData,
  SearchWidgetItem,
  WidgetRequestData,
  RecommendationWidgetItem
} from "@sitecore-cloudsdk/search/server";

export default async function middleware(request: NextRequest) {
  const response = NextResponse.next();

  // Initialize the SDK and the package...

  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);

  return response;
};


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