3

Is there a way to add (Download) an image from an external URL to the Sitecore media library directly using SPE?

Example: I want to add an image from URL (http://mySite/wp-Rad.jpg) to Sitecore media library

2
3

The process of uploading an image from External URL can be performed as follows:

  1. Download the image to a directory.
  2. Upload the image.
  3. On completion, delete image from directory

Below is the script I have used to download then upload the image to a predefined Sitecore Path in the Media Library

function New-MediaItem{
    [CmdletBinding()]
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$filePath,

        [Parameter(Position=1, Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$mediaPath)

    $mco = New-Object Sitecore.Resources.Media.MediaCreatorOptions
    $mco.Database = [Sitecore.Configuration.Factory]::GetDatabase("master");
    $mco.Language = [Sitecore.Globalization.Language]::Parse("en");
    $mco.Versioned = [Sitecore.Configuration.Settings+Media]::UploadAsVersionableByDefault;
    $mco.Destination = "$($mediaPath)/$([System.IO.Path]::GetFileNameWithoutExtension($filePath))";

    $mc = New-Object Sitecore.Resources.Media.MediaCreator
    $mc.CreateFromFile($filepath, $mco);
}

$source = "https://scx2.b-cdn.net/gfx/news/hires/2020/abstractart.jpg"
$Filename = [System.IO.Path]::GetFileName($source)
$dest = "$AppPath\custom_images\$Filename"

$wc = New-Object System.Net.WebClient
$response = $wc.DownloadFile($source, $dest);

$images = Get-ChildItem "$AppPath\custom_images\*.jpg" -Recurse

foreach($image in $images){
    
    $imageName = $image.Name
    
    $MediaImagePath = "$AppPath\custom_images\$imageName"
    
    Write-Host $MediaImagePath
    
    #Check if the image already exists in the Media Library location.
    #If exists, skip it but log in the Sitecore log file and in the console
    #If doesn't exists, upload the image and log the message
    if(Test-Path $MediaImagePath.TrimEnd(".png"))
    {
        Write-Host "Image $imageName already exists... skipping"
    }
    else
    {       
        Write-Host "Uploading Image $imageName ..."
        
        New-MediaItem $MediaImagePath "$([Sitecore.Constants]::MediaLibraryPath)/Custom"
        
        Write-Host "Uploading Image $imageName ... done."
        
        Write-Host "Removing file $imageName"
        Remove-Item $image
    }
}

Notes

  • I have created a folder on the Sitecore directory named custom_images. So, all downloaded images will go there. Once the uploaded is completed and successful, it will be deleted from the directory.

  • This script is only targeting the .jpg extension. You may want to change it to a more dynamic extension filtering.

  • You may need to update the name of the image when you are downloading it since special characters may be present. This will prevent the media item from being created.

  • You may need to adjust the script to be more dynamic in terms of the paths.

Result

enter image description here

enter image description here

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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