Tip Unpublish a Language with the Sitecore API

Created: 9 Nov 2024, last update: 9 Nov 2024

Tip: Unpublish a Language with the Sitecore API

Nearly a year ago, I wrote a blog post, Tip: Unpublish a Language from a Sitecore SXA Website, where I discussed how to remove a language from a Sitecore SXA site using Sitecore PowerShell. However, it’s also possible to achieve this with the Sitecore API.

When dealing with multiple versions in the same language, you’ll need to update all versions individually. If you want to apply this change across an entire item tree, automating the process with code becomes essential, as it requires numerous API calls. For this purpose, tools like SitecoreCommander already offer code solutions that can streamline the process.

After modifying the publish restrictions on the items, remember to publish the changes to unpublish the items from the Edge.

Example Update a single item version:

mutation UpdateItem {
        updateItem(
            input: {
                version: 1,
                itemId: "BFE61EFBA41C4BE2853ACFDB695DEF96",
                language: "en",
                fields: [
                    {
                    name: "__Hide version", value: "1"
                    }
                ]
            }
        ) {
            item {
                itemId
            }
        }
}

Replace itemId with the specific item ID you want to update. Set version and language to the appropriate values for the item and language version you’re targeting

Since the code is already made in SitecoreCommander you can easily modify it to your liking, see the code on GitHub. To run it you only need this:

// Example: Set all items in a tree as unpublishable for a specific language (the language should exist for the root item).
var result = await UnpublishLanguageFromSubtree.EditAsync(env, "/sitecore/content/Home","es");

Using the Sitecore API provides a flexible alternative to PowerShell for unpublishing specific language versions across your Sitecore site. Whether you need to apply these changes to a single item or an entire content tree, SitecoreCommander offers pre-built solutions to simplify the process. By leveraging this API approach, you can efficiently automate tasks without PowerShell, ensuring smoother content management. For more details, explore the SitecoreCommander GitHub repository to customize the code for your needs.