2

We have a problem where the Download script used as a right-click menu option is throwing an error.

enter image description here

Looking into the script, the problem appears to be how the value [Sitecore.Configuration.Settings]::DataFolder

It's coming back with a relative path that seems to be used inconsistently by the saving and the downloading respectively.

If I hard-code this value to be absolute, the functionality works as expected.

In my sitecore config file I have the following, which I assume is what's being read here:

<sitecore database="SqlServer" xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sc.variable name="dataFolder" value="/App_Data" />

I could patch this value to be the absolute path, but I'm not quite sure if there would be other implications for this.

Although it's not a big deal for me to maintain my own version of the Powershell script, I want to check if this is actually something I should address in my config file - or even whether this is a weakness in the Powershell script itself.

Has anyone come across this before - and if so, how did you address it?

Have edited to include the actual script that performs the zip/download:

function ZipItems( $zipArchive, $sourcedir )
{
  Set-Location $sourcedir
  [System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, `
      Culture=neutral, PublicKeyToken=31bf3856ad364e35") | Out-Null
  $ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive, `
      [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite)
  $items = @(Get-Item $sourceDir) + (Get-ChildItem -recurse $sourceDir)
  [byte[]]$buff = new-object byte[] 40960
  $i = 0;
  ForEach ($item In $items) {
    $i++
    if([Sitecore.Resources.Media.MediaManager]::HasMediaContent($item)){
      $mediaItem = New-Object "Sitecore.Data.Items.MediaItem" $item;
      $mediaStream = $mediaItem.GetMediaStream();
      $fileName = Resolve-Path -Path $item.ProviderPath -Relative
      $fileName = "$fileName.$($item.Extension)".Replace("\","/").Replace("./","/");
      "Added: $fileName"
      Write-Progress -Activity "Zipping Files " -CurrentOperation "Adding $fileName" -Status "$i out of $($items.Length)" -PercentComplete ($i *100 / $items.Length)
      $partUri = New-Object System.Uri($fileName, [System.UriKind]::Relative)
      $partUri = [System.IO.Packaging.PackUriHelper]::CreatePartUri($partUri);
      $part=$ZipPackage.CreatePart($partUri, "application/zip", [System.IO.Packaging.CompressionOption]::Maximum)
      $stream=$part.GetStream();
      do {
        $count = $mediaStream.Read($buff, 0, $buff.Length)
        $stream.Write($buff, 0, $count)
      } while ($count -gt 0)
      $stream.Close()
      $mediaStream.Close()
    }
  }
  $ZipPackage.Close()
}

$location = get-location
$time = Get-Date -format "yyyy-MM-d_hhmmss"
$zipName = Split-Path -leaf $location | % { $_ -replace " ", ""}
$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$zipPath = "$dataFolder\$zipName-$time.zip"

ZipItems $zipPath $location
Download-File -FullName $zipPath | Out-Null
Remove-Item $zipPath
Close-Window
1
  • Can you provide an example of your download script? Something that can be easily run in the ISE? Feb 18, 2019 at 4:39

1 Answer 1

2

SPE provides a few variables you can use in your script.

The variable SitecoreDataFolder is perhaps what is needed in your script since it resolves the full path to the folder.

Looking at the source code for that script in the SPE git repo, this issue has been addressed. It may in fact be in the 5.0 release.

You should change the building of the path to this:

$zipPath = "$($SitecoreDataFolder)\$zipName-$time.zip"

And here is the path to the context menu Download Item: /sitecore/system/Modules/PowerShell/Script Library/Media Library Maintenance/Content Editor/Context Menu/Download

1
  • Sorry - I probably wasn't being clear. This is the download script that is included in the library that comes with the Sitecore Extensions module. It's not my script.
    – caleb72
    Feb 18, 2019 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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