Configure the external component
Before you upload your JavaScript bundle, ensure the interface includes the following functions:
render, which runs when the component is rendered and receives one object as a parameter with data and interaction options.unmount, which runs when the component is unmounted.
External components have access to a pre-authenticated Content Hub JavaScript SDK client for sending HTTP requests to Sitecore Content Hub. The SDK provides dedicated clients for common operations, and a raw client for custom requests. For requests to third-party servers, you can use standard browser APIs such as Fetch.
Initialize
The createExternalRoot function receives the HTMLElement (container parameter) and can be used to mount code. To initialize the External component, put the following initialization logic inside the root of the component, outside the render function:
Render
The render function renders the contents of the External component inside a div in Content Hub and mounts the UI of the external component to the Document Object Model (DOM). This function receives an object containing the following data:
-
name- string. -
theme- material user interface theme object providing styling properties, such as fonts and colors. -
client- due to issues with the pre-authenticated JavaScript SDK client's capability to serialize and deserialize complex JSON, we recommend you create your own client instance when using the Querying client. To access the client include npm: @sitecore/sc-contenthub-webclient-sdk in your bundle and use the second argument that is passed tocreateExternalRootto create an authenticated client instance. The new signature ofcreateExternalRootis:NotePlease note that not all clients can be instantiated correctly. An example is the
entityFactoryclient. To use this client, do the following:await client.entityFactory.createAsync("definitionName");
-
config- optional configuration passed to the external component (defaults to an empty object). -
api- read-only value (ExternalApi). -
entity-entity | null. -
user- read-only value (ExternalUser) with the structure as described in the next section. -
options- object with the structure as described in the next section. -
icon- an object with a method to add Content Hub icons to and HTML element.
Do not pass the theme directly to the customer's Material UI components. The Material UI version might differ from the one used in Content Hub and, therefore, might not be fully compatible. Instead, use CSS variables for styling.
Get smaller bundle sizes
To get smaller bundle sizes when including the JS SDK in your external component bundle, instantiate the desired subclients using the authenticated client returned by the clientBuilder. For example, this approach prevents bundlers from identifying and eliminating unreachable code:
To inform bundlers that you only need a specific part of the client, such as the queryClient in the following example, do the following:
Calling clientBuilder without arguments returns the authenticated instance that is already used by the application.
Structure of the user object
The user object has the following data.
Structure of the options object
The options object has the following data.
Entity property
The entity property is passed on the same level as the config and the theme objects. It consists of the following information:
Available objects and methods include:
systemProperties
This object holds the system properties for the current entity.
properties
This object holds the properties for the current entity.
relations
This object holds the properties for the current entity.
permissions
This object holds the permissions for the current entity.
renditions
This object holds the renditions for the current entity.
setPropertyValue
This is a method to change or set the value of an entity's property. It receives the name of the property, the value, and an optional culture.
IconComponent
This is a method to load SVG icons used by Content Hub. An icon is an object passed to the main object received by the render function. This object has a method called insertContentHubIconInElement, which requires an icon name and an HTMLElement to render the icon.
setRelatedIds
This is a method to change or set the value of an entity's relation. It receives the relation name, the array of entityIds, and an optional relation role.
The relation role that can be passed to the setRelatedIds method is a number.
When the External component is not used on a details page, the entity is null. In custom code make sure the property exists and is not null.
API object
The api object holds methods to interact with the Search, Selection, and Details components and to display notifications:
The following methods let you add or remove filters:
updateQuery: (searchIdentifier: string, query: string) => void;- receives the Search component identifier and the new query as parameters; triggers a new search request with an updated query.addFilters: (searchIdentifier: string, filters: Array FieldFilterRequestResource>) => void;- receives as parameters the Search component identifier and an array of filters to set; triggers a new search request with the updated filters.clearAllFilters: (searchIdentifier: string) => void;- clears all filters, including any visual search filters added through an external Search component.updateFullTextFilter: (searchIdentifier: string, text: string) => void;- receives the Search component identifier and a new text value as parameters; triggers a new search request with the updated full-text filter.clearFullTextFilter: (searchIdentifier: string) => void;- receives the Search component identifier as parameter; triggers a new search request with an empty full-text filter.getEventSearchIdentifier: (searchIdentifier: string) => string;- receives the Search component identifier as parameter; returns the identifier to check when subscribed to the SEARCH_FINISHED event.activate(searchIdentifier: string): void- activates the Search component with the specified identifier and begins to send search requests.addListener: (searchIdentifier: string, eventName: SearchEvent, listener: SearchEventListener) => DisposeSearchEventListener- allows external users to add a listener that executes when the event it is listening to occurs. The only event currently available is BEFORE_SEARCH. The listeners for this event receive a payload object in the form{ searchRequest: SearchRequest }and the code inside the listener modifies thisSearchRequest. Whenfalseis returned by any listener, the search request will be canceled. TheaddListenermethod returns a dispose function that users can call when needed. This dispose method, when called, removes the listener that was added.
The following methods are available with a Selection component:
addToSelection: (ids: Array number>, selectionPoolIdentifier: string, subPoolId?: number) => void;- receives the array of entityidsto add, the identifier of the selection pool, and an optionalsubPoolIdrepresenting the selection of assets.removeFromSelection: (ids: Array number>, selectionPoolIdentifier: string, subPoolId?: number) => void;- receives the array of entityidsto remove, the identifier of the selection pool, and an optionalsubPoolIdrepresenting the selection of assets.clearSelection: (selectionPoolIdentifier: string, subPoolId?: number, definitionNames?: Array string>) => void;- receives the identifier of the selection pool, an optionalsubPoolIdrepresenting the selection of assets, and an optional array of definition names for which to clear the selection. If no definition names are passed, all selections are cleared.
If you don't provide a subPoolId property, the items are added to the standard pool with the identifier selectionPoolIdentifier.
This method loads a specified entity:
setEntitySource: (identifier: string, entityId: number) => void- accepts a details component identifier as the first argument and an entity ID as the second argument; it has no return value. If the entity source of a details component is set toFrom event, the specified entity is loaded whenever thesetEntitySourcemethod is executed from an external component using the component identifier.
The following method is used to reload an entity. If the entity is currently being edited in a details component, the reload does not happen. To force a reload, you can add a second Boolean parameter to the method:
-
reloadEntity: (entityId: number, forceReload?: boolean) => Promise<void>;- forces the reload of a specific entity when it is being edited if set totrueas shown in the following example:
The following notifier events are available:
notifySuccess- receives the text to show in the notification (for example, Entity saved).notifyError- receives the text to show in the notification (for example, Save failed).notifyWarning- receives the text to show in the notification (for example, Download partially created).notifyInfo- receives the text to show in the notification (for example, Saved search results changed).
Examples on how to implement external page components are available to get you started.