Skip to content

Instantly share code, notes, and snippets.

@mikaelnet
Created November 19, 2015 07:31
Show Gist options
  • Save mikaelnet/00624bd1038bead5eb14 to your computer and use it in GitHub Desktop.
Save mikaelnet/00624bd1038bead5eb14 to your computer and use it in GitHub Desktop.
Sitecore Powershell Image upload
function Update-MediaItem {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[string]$filePath,
[Parameter(Position=1, Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$mediaPath)
[Sitecore.Data.Items.MediaItem]$item = gi $mediaPath
[Sitecore.Resources.Media.Media] $media = [Sitecore.Resources.Media.MediaManager]::GetMedia($item);
$extension = [System.IO.Path]::GetExtension($filePath);
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList $filePath, "Open", "Read"
$media.SetStream($stream, $extension);
$stream.Close();
}
function Find-MediaItem {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[string]$mediaName)
$query = "/sitecore/media library//*[@@name='{0}']" -f $mediaName
Write-Host $query
[Sitecore.Data.Items.MediaItem]$item = Get-Item master: -Query $query | Select -First 1
return $item
}
#write-host "$SitecoreDataFolder"
Get-ChildItem "$SitecoreDataFolder\upload\*.jpg" -Recurse| ForEach-Object {
$file = $_;
Write-Host $file.FullName
$mediaItem = Find-MediaItem $file.BaseName
$mediaPath = "{0}{1}" -f $([Sitecore.Constants]::MediaLibraryPath), $mediaItem.MediaPath
$mediaId = "{0}" -f $mediaItem.ID
Write-Host $mediaId $mediaPath
if ($mediaId -ne "") {
Update-MediaItem $file.FullName $mediaPath
Remove-Item $file
Write-Host "Imported and deleted"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment