Skip to main content

Software Development

Bypassing the PowerShell File Import Issue

Learning from collaboration

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.

PowerShell Import Error

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

File Import Popup

File Import Popup Selected

Conclusion

The PowerShell file import script was working correctly on the local project instance, but it failed to work when deployed to other higher environments and with this approach, it 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!!!

Thoughts on “Bypassing the PowerShell File Import Issue”

  1. This issue is caused by a change Sitecore introduced in the product, most often seen after applying hotfixes. A newer hotfixe provided by support should address this issue.

  2. Saket Singh Post author

    Thank you Michael for the suggestion, surely I will try to apply the hotfixes and check if the issue is resolved by that or not for higher environments.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Saket Singh

Saket Singh is currently working with Perficient GDC Nagpur as a Senior Technical Consultant. He is a Sitecore developer who works on SXA and Sitecore Headless websites. He also has knowledge of ASP.NET, ASP.NET MVC, C#, Web API, MSSQL, and JavaScript. He enjoys discovering new technologies and understanding the architecture that supports them.

More from this Author

Follow Us