1

We are working on Sitecore 10.3 Headless.

For one of the components we are developing, we need the title field from the context item and rest of the content from Datasource item.

enter image description here

In this case if we select Context Item Resolver then we miss content from Datasource item and if we select Datasource Resolver then we miss content from context item.

Do we need to create a Custom Item resolver which has content from context item and also from Datasource item ?

2 Answers 2

2

A custom content rendering resolver is one option. But also, if you are using Next.js you can always use the useSitecoreContext hook and access the context item fields from that object. This will save you writing custom code.

For an example you can look at the Headless SXA Page Content component. This will use a datasource item if provided, otherwise it will use the context item:

const { sitecoreContext } = useSitecoreContext();

const field = (props.fields && props.fields.Content
  ? props.fields.Content
  : sitecoreContext?.route?.fields?.Content) as RichTextField;

Ref: https://github.com/Sitecore/jss/blob/7d2b47bf9495b4301ecc07f5f62f852fb2e34f87/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/PageContent.tsx#L35C21-L35C21

1

Yes, you need to create Custom Contents Resolver.

You can access Datasource and also Context Item without any problem in Custom Contents Resolver.

Just create custom resolver with Use Context Item checkbox checked:

enter image description here

Then in Custom Contents Resolver code, you can access both:

  • item - will give you access to Context Item
  • rendering - will give you access to datasource

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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