Updated: CRUD Operation in Sitecore using PowerShell


In one of my previous post I have shared a PowerShell script which is used to perform CRUD Operations in Sitecore.

But there is one issue with that, it creates multiple items with same name when the script is executed again.

Here is the link of the old post for CRUD Operation in Sitecore using PowerShell.


Updated Script for CRUD Operations

## Specify the Path where the Items are to be kept
$Item_Path = "master:\content\Home"
$Selected_RootItem = "/sitecore/content/home"

## Path of your CSV data file
$importList = Import-CSV "D:\EmployeeRecords.csv"

## Template to be used for creating Items
$template = "{4B97093D-7C1F-4ADD-BE44-31F5661A074E}"
$bulk = New-Object "Sitecore.Data.BulkUpdateContext"
try {
    # Accessing row wise data from CSV data file
    foreach ($row in $importList) {
        # Comparing different operation values
        if ($row.Operation -eq "Create") {
            $name = $row.Name
            if ($name.Trim() -eq "") {
                write-host "Item name should not be blank: " $name
                continue
            }
            $itemPath = $Selected_RootItem + "/" + $name
            # Create Item Path
            $currentItem = Get-Item -Path $itemPath -ErrorAction SilentlyContinue
            if ($currentItem -eq $null) {
                # Check if Item is null then Create a new item
                $New_Item = New-Item -Path $Item_Path -Name $row.Name -ItemType $template
                $New_Item.Editing.BeginEdit()
                $New_Item["Name"] = $row.Name
                $New_Item["Designation"] = $row.Designation
                $New_Item["Age"] = $row.Age
                $New_Item["Salary"] = $row.Salary
                $New_Item["Description"] = $row.Description
                $New_Item.Editing.EndEdit()
                Write-Host "Item created $row" -NoNewline
                Write-Host $item.ID
            }
        }
        if ($row.Operation -eq "Update") {
            # Update an existing item
            $Items = Get-ChildItem -Path $Item_Path
            foreach ($item in $Items) {
                if ($item.Name -eq $row.Name) {
                    $item.Editing.BeginEdit()
                    $item["Name"] = $row.Name
                    $item["Designation"] = $row.Designation
                    $item["Age"] = $row.Age
                    $item["Salary"] = $row.Salary
                    $item["Description"] = $row.Description
                    $item.Editing.EndEdit()
                    Write-Host "Item Updated $row" -NoNewline
                    Write-Host $item.ID
                }
            }
        }
        if ($row.Operation -eq "Delete") {
            # Delete an item
            $Items = Get-ChildItem -Path $Item_Path
            foreach ($item in $Items) {
                if ($item.Name -eq $row.Name) {
                    $item | Remove-Item
                    Write-Host "Item Deleted $row" -NoNewline
                    Write-Host $item.ID
                }
            }
        }
    }
}
catch {
    write-host "Failed to create Item: " $row.Name
    write-host $_.Exception.Message
}
finally {
    $bulk.Dispose();
}

This script will check if the item exist with same name or not. It will not create duplicate items if the script is executed multiple times.

Chirag Goel

I am a developer, likes to work on different future technologies.

Post a Comment (0)
Previous Post Next Post