PowerShell proves highly useful in automating processes and managing various aspects of Sitecore. However, we faced difficulties when we encountered issues while importing files by using PowerShell Script. In this article we will discuss the file import issue which we encountered during a task in one of our projects.

We encountered a scenario where we required to import data from an ‘Excel’ or ‘CSV’ file into ‘Sitecore’. We developed a PowerShell script to read the ‘Excel’ or ‘CSV’ file and import its contents into the Sitecore content tree. The script functioned correctly on the local project instance. However, when we deployed the same changes to a higher environment, it failed to function properly and produced the error mentioned below.

Import File Error : Using PowerShell Script

Calling Spe.Client.Applications.UploadFile.PowerShellUploadFileForm.OKClick method through reflection is not allowed.

I reviewed one of the Sitecore PowerShell Issues blog posts and implemented the solutions mentioned in it. Unfortunately, it didn’t work for me.

I followed the listed approach below, and upon implementation, I managed to import the data from the ‘Excel’ or ‘CSV’ file into Sitecore.

Upload file in media library
Import data using PowerShell Script

Upload file in Media Library

Create a specific folder inside the ‘Sitecore Media Library’ for uploading ‘Excel’ or ‘CSV’ files. This folder will also allow users to upload and select files from it. All files intended for data import into Sitecore will be uploaded and saved here first.

Import data using PowerShell Script

Once the file is uploaded to the ‘Media Library Folder,’ it is ready for import into Sitecore. Select the ‘data’ folder for data import and execute the ‘PowerShell’ script.

Note : We have created a ‘PowerShell’ script that reads ‘Excel’ or ‘CSV’ files from the designated ‘Media Library’ folder and includes the necessary code logic for data import. Furthermore, we have added a script that enables users to select desired files from the list of uploaded files in the ‘Media Library’ folder. When the required file is selected, the code logic will work in the background to import the data.

PowerShell Code – Allowing Users to Select a Specific File

New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext){
$importCSVFileFolderPath = “Your Media Library folder path”
if (-Not (Test-Path $importCSVFileFolderPath))
{
Show-Alert -Title “‘$importCSVFileFolderPath’ doesn’t exist !!”
Write-Host “‘$importCSVFileFolderPath’ doesn’t exist!” -ForegroundColor Red;
Exit
}

#Code to Select the CSV file which needs to be imported
$finalFileImportPath = “”;
$importCSVFilePathSelector = @{
Title = “Data Import”
Description = “Please select the necessary CSV file”
OkButtonName = “Select”
CancelButtonName = “Cancel”
Width = 500
Height = 300
Parameters = @(@{ Name = “importFileFolder”; Title = “Select File”; Source=”Datasource=$importCSVFileFolderPath”; Mandatory = $true; Editor=”droptree”})
}

$importFilePathSelector = Read-Variable @importCSVFilePathSelector
if($importFilePathSelector -eq “cancel”)
{
Write-Host “Please select a CSV file to import”
Exit
}
else
{
$finalFileImportPath = $importFileFolder.Paths.FullPath
}

#Remaining logic to import file data should start from here
}

Output of above code

Conclusion

This approach allows us to get around the PowerShell file import issue and gives the user the ability to choose which exact file needs to be imported.

Hope this helps. Happy learning!!!