Last active
July 23, 2016 12:43
-
-
Save AdamNaj/ad2d059c618df2fc4c03 to your computer and use it in GitHub Desktop.
Create Media item from file on server drive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
# Usage example | |
New-MediaItem "C:\Users\Adam\Desktop\Accelerators.jpg" "$([Sitecore.Constants]::MediaLibraryPath)/Images" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
# Usage example - overwrite the image created with previous cmdlet with new image | |
Update-MediaItem "$SitecoreDataFolder\Tchotchkeys.jpg" "$([Sitecore.Constants]::MediaLibraryPath)/Images/Accelerators" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment