CRUD Operation in Sitecore using PowerShell

Today I am going to share a PowerShell script that you can use to perform CRUD (Create, Read, Update and Delete) Operations in Sitecore. The Script in my case is creating, updating, and deleting the records of Employees. Employee record contains name, designation, age, salary, and description.


You can modify the script according to your need to perform the CRUD operations.


Steps to follow


Step 1: Create a Template in Sitecore which contains all the fields that you want for a particular record. In my case template has a field for Name, Designation, Age, Salary, and Description. You can have your own fields. Also, keep a note of the template id because it is needed in the PowerShell Script.



Step 2: Now, create a CSV file that contains the employee details which are name, designation, age, salary, and description. We also need an extra field that signifies what operation is to be performed. Follow the structure shown in the below image for the CSV file.


Note: You must keep all the fields that you specified while creating the template.



Step 3: Now, follow the below PowerShell script and make changes according to the comments mentioned in the script.


## Specify the Path where the Items are to be kept
$Item_Path = "master:\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") {
            # 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
                }
            }
        }
    }
}
finally {
    $bulk.Dispose();
}


Step 4: Once the PowerShell script is ready with your changes, open the PowerShell ISE from Development Tools in Sitecore and copy the script to the PowerShell.


Step 5: Execute the script and you will be able to see the records being inserted, deleted, and updated under the home node in Sitecore Content Tree as you mentioned in the operation field in CSV.




References


PowerShell Training - https://doc.sitecorepowershell.com/training


Power up with Sitecore PowerShell - http://sugblr.in/events/2019/08/mindtree-meetup/powering-up-with-sitecore-powershell.html



Happy Coding, Coders for Life 😊

Chirag Goel

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

Post a Comment (0)
Previous Post Next Post