2

I have exported items using Export-Item command to a specific path in Data folder say, "websitepath/data/batch/userGivenName", please refer my previous question How to copy selected items to a Data folder using powershell script? now I have to read the items from this folder. The items under folder are in .item extension. I am able to get items using $items = Get-ChildItem -Path "websitepath\Data\batch\sample", but the items are not deserialized.

I tried using, Import-Item but its returning empty list, how do I deserialize/convert them?

$items = Get-ChildItem -Path "websitepath\Data\buckets\sample"
$list=@();
$items|Foreach{
    $itemPath="websitepath\Data\buckets\sample\"+$_.BaseName
    $list+= Import-Item -Path $itemPath -Root "websitepath\Data\buckets\"
   Write-Host($list)
}
if($list.Count -eq 0) {
    
    Show-Alert "There are no items currently added to the batch."
    
} 
else {
 $list | Show-ListView 
   
}
Close-Window

I referred following file for Import-Item, https://doc.sitecorepowershell.com/appendix/packaging/import-item-1

Any guidance would be helpful.

2
  • Does the deserialization work using the Sitecore Ribbon commands? This will help determine if there is an issue with the script or something else. Commented May 3, 2022 at 14:05
  • I guess the issue is with the script, as I noticed the import-items created items under "Sitecore/Content" folder by default as the item path I have provided didnt match with sitecore content tree structure Commented May 4, 2022 at 7:14

2 Answers 2

2

It looking like you're trying to import and deserialize items from files using the Import-Item cmdlet in PowerShell. However, Import-Item isn’t a standard PowerShell cmdlet, so it might be part of a custom module or script you're using.

Since Import-Item isn't a default cmdlet, let's address a more common approach for deserialization based on typical serialization formats such as XML or JSON. Here’s how you might handle it:

If the items are in a custom binary or other format, you will need the specific deserialization method used for that format. For custom formats, you might need to use a .NET class or library that matches the serialization format.

Here’s a hypothetical example if you have a custom deserialization method:

# Example of custom deserialization
Add-Type -AssemblyName "YourAssemblyContainingCustomType"

$path = "websitepath\Data\buckets\sample"
$list = @()

Get-ChildItem -Path $path -Filter *.item | ForEach-Object {
    $itemPath = $_.FullName
    $itemBytes = [System.IO.File]::ReadAllBytes($itemPath)
    $deserializedItem = [YourCustomType]::Deserialize($itemBytes)
    $list += $deserializedItem
}

if ($list.Count -eq 0) {
    Show-Alert "There are no items currently added to the batch."
} else {
    $list | Show-ListView
}
Close-Window

Replace YourCustomType with the actual type used for deserialization.

0

To test the same scenario, I did the following on my local.

  • I created two items in the content tree like this.

enter image description here

  • I exported the items using the Export-Item. And it looks like this

enter image description here

  • I deleted the items in the content tree and then I run this command to import the items using Import-Item in PowerShell like this.

    Import-Item -path "C:\inetpub\wwwroot\Sitecore10.1sc.dev.local\App_Data\serialization\master\sitecore\content\Home" -root "C:\inetpub\wwwroot\Sitecore10.1sc.dev.local\App_Data\serialization\" -recurse

And then it created those items in the content tree.

I think, if you want to import the items, In place of using a loop, you can use recursive and you need to provide the folder path from where you need to import the items. For example, I am exporting items inside the Home folder.

Hope this helps.

1
  • Hi Sumit, I didn't want the items to be created again in the content tree. I tried to convert them back to item as they were in ".item" extension. I guess I have used the wrong command to achieve this. As the Import-Item created items under "Sitecore/Content" folder as the path provide doesn't match with the content structure. Commented May 4, 2022 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.