How to Create and Retrieve Sitecore Items in Sitecore XM Cloud Using Content Authoring and Delivery API

When working with Sitecore XM Cloud, especially for client applications where you need to create or retrieve items from Sitecore Content Management (CM), understanding how to interact with Sitecore APIs is crucial. This guide will walk you through the process of creating and retrieving Sitecore items using Sitecore XM Cloud, using tools like Postman.

Why Use the Sitecore API?

The Sitecore API allows you to programmatically create, retrieve, and update items in Sitecore. This is especially helpful when building client-facing applications that require integration with Sitecore’s content management system. Think of it as the "backend" way to create and manage your Sitecore content, much like how a button in Sitecore's Content Editor can be used to create items manually.

Steps to Connect to Sitecore XM Cloud  Content Authoring API

To call the API and interact with your Sitecore XM Cloud instance, you’ll need a few pieces of information from your Sitecore XM Cloud project.

1. Get Your Sitecore XM Cloud Credentials

To interact with Sitecore XM Cloud’s APIs, you first need to authenticate using a client_id and client_secret. Here's how to get them:

  • Client ID and Client Secret:

    1. Go to your Sitecore XM Cloud project.

    2. Navigate to the Credentials section.

    3. Once there, you’ll see a button to create a new credential. Click on it to generate a client_id and client_secret.



  • Environment Host Name:

    1. Open your XM Cloud project.

    2. Navigate to the Details tab.

    3. Here you’ll find the Environment Host Name, which is crucial for making API calls.



2. Postman Setup

Once you have your credentials, the next step is setting up Postman to make API calls. The first API call you need to make is to obtain an access token, which is necessary for further interaction with the Sitecore APIs.

Step 1: Get an Access Token

Before you can retrieve or create items, you need an access token. This token will authenticate your subsequent API calls.

  • API Endpoint:

    https://auth.sitecorecloud.io/oauth/token
  • Request Type: POST

  • Request Body:
    In the request body, you’ll need to pass the client_id and client_secret obtained earlier.

    { "client_id": "YOUR_CLIENT_ID", "client_secret": "YOUR_CLIENT_SECRET", "audience": "https://api.sitecorecloud.io", "grant_type": "client_credentials" }
  • Response:
    This will return an access token that you’ll use for authenticating subsequent API calls.



Step 2: Retrieve Sitecore Item

Now that you have the access token, you can use it to fetch item details from your Sitecore XM Cloud instance.




  • API Endpoint:

    {environmentHostName}/sitecore/api/graph/edge
  • Request Type: POST

  • Authorization:
    Use the Bearer token type and pass the access token you received in the earlier step.

  • Request Body:
    To fetch an item, you need to specify its path and language. Here’s an example query to retrieve a specific item:

    query GetTenantRoot {
      item(path: "/sitecore/content/your-website/ChandanTest", language: "en") {
        id
        name
       
        children {
          results {
            id
            name
           
          }
        }
      }
    }

Step 3: Create Sitecore Item

To create a new item in Sitecore XM Cloud, you'll make another API call, passing in details like the item’s parent, template ID, and field values.



  • API Endpoint:

    {environmentHostName}/sitecore/api/authoring/graphql/v1
  • Request Type: POST

  • Authorization:
    Again, use the Bearer token for authentication.

  • Request Body:
    The body will contain a GraphQL mutation to create a new item. Below is an example of how to create a new item:


mutation CreateItem {
  createItem(
    input: {
      database: "master"
      language: "en"
      name: "new-Test"
      parent: "C85F63F25B854D83A24D8C6681C98589"(This is the parent folder item id where items need to be created
      templateId: "{76036F5E-CBCE-46D1-AF0A-4143F9B5589A}"(template id which will be used to create the items
      fields: [
        { name: "Title", value: "New Test Title" }
        { name: "Text", value: "New Test Text Content" }
      ]
    }
  ) {
    item {
      itemId
      name
      path
    }
  }
}

Conclusion

Working with Sitecore XM Cloud API is a powerful way to programmatically manage content within your Sitecore instance. By following the steps outlined above, you can easily create, retrieve, and update items in Sitecore, making it easier to build robust, dynamic web applications that integrate with your Sitecore CMS.

Whether you're building a simple form or a more complex solution, understanding the API calls and how to authenticate and interact with Sitecore XM Cloud will help you unlock the full potential of Sitecore’s headless CMS capabilities.

Have any questions or need further help with Sitecore XM Cloud APIs? Feel free to ask in the comments below!

Comments

Popular posts from this blog

Solrcloud With Zookeeper -Single server setup

Render Sitecore Experience Forms Using Sitecore XP 10.4 with a Headless Approach (Next.js + JSS SDK)

Next.js with XM Cloud EDGE and GraphQL