1

I am attempting to upload a PDF file to Sitecore as a single web request and have it added to the media library. I would like to choose a parent folder and have all requests generate a PDF at that location with the provided file name.

It appears that SPE is capable of such a feat, but the similar questions/blogs that I've found thus far do not quite provide the answers I'm looking for.

1 Answer 1

2

First, ensure you have Sitecore Remoting enabled.

Next, ensure the restfulv1 service is enabled (NOTE: I just set up the admin user for a quick and dirty example):

<powershell>
      <services>
        <restfulv1>
          <patch:attribute name="enabled">true</patch:attribute>
          <authorization>
              <add name="isadmin" Permission="Allow" IdentityType="Role" Identity="sitecore\IsAdministrator" />
              <add name="superuser" Permission="Allow" IdentityType="User" Identity="sitecore\superuser" />
            </authorization>
        </restfulv1>
     <remoting>
      <patch:attribute name="enabled">true</patch:attribute>
      <authorization>
        <add name="isadmin" Permission="Allow" IdentityType="Role" Identity="sitecore\IsAdministrator" />
        <add name="superuser" Permission="Allow" IdentityType="User" Identity="sitecore\superuser" />
      </authorization>
    </remoting>
      </services>
</powershell>

The following Powershell script provides the backbone for uploading a PDF via a standard HTTP Request:

$parentMediaFolder = "$([Sitecore.Constants]::MediaLibraryPath)/Files/"
$fileName = $scriptArguments["fileName"]
$fileNameNoExtension = [System.IO.Path]::GetFileNameWithoutExtension($fileName);$mediaCreatorOptions = New-Object Sitecore.Resources.Media.MediaCreatorOptions
$mediaCreatorOptions.Database = [Sitecore.Configuration.Factory]::GetDatabase("master")
$mediaCreatorOptions.Versioned = [Sitecore.Configuration.Settings+Media]::UploadAsVersionableByDefault;
$mediaCreatorOptions.Destination =  $parentMediaFolder + $fileNameNoExtension$mediaCreator = New-Object Sitecore.Resources.Media.MediaCreator$createdMediaItem = $mediaCreator.AttachStreamToMediaItem($($requestStreams.Values)[0], $parentMediaFolder + $fileNameNoExtension, $fileName, $mediaCreatorOptions)
write-host $createdMediaItem.ID
Add this script to Sitecore.

Usage:

Use Postman to construct a request with these settings:

Verb: POST

URL: https://cminstance.local/sitecore%20modules/PowerShell/Services/RemoteScriptCall.ashx

Query String parameters:

  • script: the full path to the SPE script
  • sc_database: master
  • apiVersion: 1 (this is a toggler for a variety of functions available in the RemoteScriptCall service, we need 1 in order to execute a script)
  • fileName: A custom parameter. We can add as many as we need. This includes the file extension. This is sent to the SPE script.

Body: Choose "form-data", add a "KEY" named "File" and modify the "VALUE" value and choose a PDF to attach to the request.

Authorization (tab): Select "Basic auth"

Enter the username as "sitecore\<username>" and the password

Response: The ID of the item that was added. By default, if the file name is a match, it updates the existing item and does not create a new item.

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.