Streamlining Sitecore Integration: PowerShell-Based Automation for Importing API Data into Sitecore Templates Data Sources

Sometimes, we need to fetch data from an external API commonly in JSON format and insert it into a Sitecore data source item. Traditionally, this process involves writing C# code to connect to the API, retrieve the data, and then create or update Sitecore items. However, this task can be streamlined significantly using PowerShell scripts.

Using PowerShell enhances efficiency, especially when working with large datasets such as product catalogs, event listings, or personalized content. It simplifies the integration workflow, reduces development time, and makes data synchronization into Sitecore faster and more manageable.

For demonstration purposes, we created three sample APIs, each returning JSON data with the following fields:

  • EventName

  • Year

  • Link

  • Coordinator

  • Email

  • ID


We also created three corresponding data source items in Sitecore, each configured with the appropriate API URL. These serve as the reference points from which the PowerShell script retrieves data.




Created a PowerShell utility to perform the integration by selecting

  1. Template for Field Mapping – A Sitecore template that matches the fields returned by the API. We created templates with identical field names for consistency.

  2. Parent Data Source Item – A parent folder where the new data items will be created. Each API has its own designated parent folder.

  3. API Source Item – A Sitecore item that stores the API URL. You choose one of these items to indicate which API to pull data from.







Once the script runs using PowerShell utility, it:

  • Reads the selected API URL.

  • Fetches the JSON data.

  • Maps the data to the selected template fields.

  • Creates corresponding Sitecore items under the specified parent folder.


For instance, the selected parent folder is "Calls," all data items retrieved from the API is inserted under Calls folder as depicted above.

Complete Script:

PowerShellScriptInputs from user
# ***TemplateItem - List to select Template
# ***ParentItem - List to select data source item folder
# ***Apiitem - List to select API
$dialogParameters = @(
    @{ Name = "TemplateItem"; Title = "Select Template"; Source = "/sitecore/templates/Project/DataSourceTemplates"; Editor = "droplist";Mandatory = $true},
    @{ Name = "ParentItem"; Title = "Select Data Source"; Source = "/sitecore/content/Home/Data"; Editor = "droplist"; Mandatory = $true },
	@{ Name = "Apiitem"; Title = "Select Api"; Source = "/sitecore/content/Home/APIs"; Editor = "droplist"; Mandatory = $true }
)
# Model dialog configuration
$dialog = Read-Variable -Parameters $dialogParameters `
    -Description "This module is used to Sync data from an API to Sitecore items." `
    -Width 400 -Height 200 `
    -Title "Sitecore API Data Sync" `
    -OkButtonName "Sync Data" `
    -CancelButtonName "Cancel"
	
# Define the paths from User Input
$parentPath = $ParentItem.Paths.FullPath
$templatePath = $TemplateItem.Paths.FullPath
$apidetailsPath=$($Apiitem.Paths.FullPath)

# Get the API URL to fetch data
$apiitem = Get-Item -Path $apidetailsPath
$apiUrl=$($apiitem["url"])

# Fetch data from the API
try {
    $response = Invoke-RestMethod -Uri $apiUrl -Method Get
    if ($response) {
        Write-Host "Data retrieved successfully!"
        
        # Process and display each record
        foreach ($item in $response) {
            # Ensure eventname is not empty to avoid creating invalid items
            if ($item.eventname) {
                try {
                    # Create a new item under the specified parent data spurce folder
                    $newItem = New-Item -Path $parentPath -Name $($item.eventname) -ItemType $templatePath -Force
                    
                    # Begin editing the new item
                    $newItem.Editing.BeginEdit()
                    try {
                        # Set field values from the API response
                        $newItem["eventname"] = $item.eventname
                        $newItem["year"] = $item.year
                        $newItem["link"] = $item.link
                        $newItem["coordinator"] = $item.coordinator
                        $newItem["email"] = $item.email
                        # End the editing session
                        $newItem.Editing.EndEdit()
                        Write-Host "Item created successfully: $($item.eventname)"
                    } catch {
                        # Cancel the editing if there is an error updating the item
                        $newItem.Editing.CancelEdit()
                        Write-Host "Error updating item: $($item.eventname)"
                    }
                } catch {
                    Write-Host "Error creating item for: $($item.eventname)"
                }
            } else {
                Write-Host "Skipping item with missing 'eventname'"
            }
        }
    } else {
        Write-Host "No data found from the API."
    }
} catch {
    Write-Host "Error retrieving data from API: $_"
}
To create PowerShell utility, please follow the steps mentioned in blow blog post
https://panerinidhi.blogspot.com/2025/03/create-sitecore-ribbon-button-using.html

Comments

Popular posts from this blog

Setting Up Sitecore Headless with Next.js in Disconnected Mode

Step-by-Step Guide: Sitecore Headless GraphQL Filtering, Sorting & Pagination

Building a Headless Integration Between Sitecore and Azure Functions