Sitecore Content Migrating with GraphQL

Created: 2 Nov 2023, last update: 3 Oct 2024

Content Migrating with GraphQL

Seamles Content Migrating with GraphQL

In this blog, we'll explore how to create content in Sitecore XM Cloud using the GraphQL API with C# code examples. If you're migrating to Sitecore XM Cloud and need to automate content migration from your previous non-SXA headless solution, the Sitecore Authoring and Management GraphQL API is a powerful tool. It allows you to create content items, media items, and perform tasks such as publishing and indexing.

In a previous blog, "Migrating Your Content Seamlessly: A Comprehensive Tech Guide from WordPress to Sitecore XM Cloud," we discussed various options for importing content into XM Cloud. This blog, however, focuses on the Sitecore Authoring and Management GraphQL API. Notably, it simplifies media handling, eliminating the need for workarounds required with the RESTful API. It's also a future-proof solution, suitable for repetitive tasks beyond one-time imports.

The Edge Preview GraphQL IDE (CM) API

In addition to the Sitecore Authoring and Management GraphQL API for mutations, the Edge Preview GraphQL IDE (CM) API is available. You can use it to request items, check for item existence, or retrieve item details. Note the Edge preview GraphQL endpoint is using the master database so you can also work with not published item. So use this for import instead of the live Sitecore Edge endpoint.

Setting Up the Sitecore Authoring and Management GraphQL API

Optionally, you can enable the GraphQL IDE, providing a user-friendly interface for executing queries. It's advisable to enable the GraphQL IDE only in non-production environments. To enable it locally with Docker, add "Sitecore_GraphQL_ExposePlayground: "true" in the CM environment of the docker-compose.override.yml file.

To obtain an access token, besides the method described in the documentation using 'curl', you can also retrieve it from the .sitecore/user.json file if you have a valid connection in PowerShell with XM Cloud (using 'dotnet sitecore login'). The access token, bearer token is a string of about 5KB in length.

Setting Up the Edge Preview GraphQL IDE

To set up the Edge Preview GraphQL IDE, you'll need an API key (GUID), which can be found at /sitecore/system/Settings/Services/API Keys in Sitecore. More details can be found here.

Query Examples Sitecore Authoring and Management GraphQL API

For examples of queries related to authoring operations, including media uploads, refer to query examples.

Performance

The GraphQL API offers performance similar to the RESTful API, allowing you to create 2 to 3 items per second. For media insertion, you need 2 requests, with data count varying based on the specific media item, usually around 1 media item per second.

C# Code

For tasks that require significant computational power, C# outshines Node.js. It seamlessly integrates with the Sitecore API, making it indispensable for intricate layout modifications. C# excels in handling heavy workloads, enduring long-duration tasks, and providing robust debugging capabilities.

Demo Code

You can find demo code on my GitHub repository for working with the Sitecore GraphQL API in C#. The console app demonstrates various functionalities, including getting Sitecore items, retrieving all websites, inserting sample items, and uploading media items. To run the demo, simply provide your XM Cloud hostname, API key, and access token in the app.config file, and you're good to go.

Creating a Content Migrator

On Sitecore 10.3 and later, you can enable the GraphQL API to read data from your old Sitecore platform, process the data, restructure the content tree, and insert it into XM Cloud. You can also use content from other sources, such as JSON or XML exports from platforms like WordPress or Umbraco.


Let's get started!