Privacy Info

Saturday, October 15, 2022

Sitecore PowerShell Script - Item Creation from API


This post describes how can you convert API response from URL to Sitecore item. We need to specify folder item path, Template ID and API URL. After running the script items will be created with API details. Also create template with id, userid, title and completed field and specify template ID for this new template in $templateID variable.  

Script

$itemPath="master:\content\home\DemoTestFolder"
$templateID="{46F57F90-B447-4E8A-8CA1-69B1AD08ACD2}" 
$ApiUrl = "https://jsonplaceholder.typicode.com/todos" 

$response = Invoke-RestMethod -Uri $ApiUrl -Method Get -ContentType "application/json";

foreach ($row in $response)
{
    if (-not ([string]::IsNullOrEmpty($row.id)))
    {
	    $itemName = $row.id

	    #Create Item
	    $newItem = New-Item -Path $itemPath -Name $itemName -ItemType $templateID;

	    #Add values in fields
            $newItem.Editing.BeginEdit()
	    $newItem["id"] = $row.id
	    $newItem["userId"] = $row.userId
	    $newItem["title"] = $row.title
	    $newItem["completed"] = $row.completed
	    $newItem.Editing.EndEdit()

    	Write-Host "Item Created: " $itemName 
    }
}

API URL Reference: https://jsonplaceholder.typicode.com

 

Hope you find this information useful.

No comments:

Post a Comment

Sitecore PowerShell Script - Remove item from Workflow

We came across scenario where we need to remove multiple items from workflow. If items would be less then there would not be any issue but i...