Create download link for folder using Powershell SDK

  • 1
  • Problem
  • Updated 12 months ago
I'm trying to get a download link for a folder using the Powershell SDK.

I do have the folder object at hand in $Folder. And run the following code:

    $rp = New-Object ShareFile.Api.Models.Share
    $rp.ShareType = [ShareFile.Api.Models.ShareType] 'Send'
    $rp.parent = $Folder
    $DownloadLink = Send-SfRequest -Method POST -Entity Shares -Client $SFlogin -Body $rp
    $DownloadLink.uri.AbsoluteUri

This generates an error:

Send-SfRequest : BadRequest: Invalid Argument Shares.items
At line:6 char:21
+ ... nloadLink = Send-SfRequest -Method POST -Entity Shares -Client $SFlog ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Shares:String) [Send-SfRequest], Exception
    + FullyQualifiedErrorId : BadRequest,ShareFile.Api.Powershell.SendSfRequest

I have tried working with the Shares.items property of the object, but cannot seem to get it to accept a folder as input. Actually not even a file.

If I replace the sharetype with 'Request', a link is generated just fine.

Can anyone point me to a solution to this?
Photo of Brian Sprogø

Brian Sprogø

  • 11 Posts
  • 0 Reply Likes

Posted 1 year ago

  • 1
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 732 Posts
  • 102 Reply Likes
Brian,

The Items property is an IEnumerable (list) of Items so passing a single folder or file will not work, you will need to wrap that object in an IEnumerable object. Let me know if that works or if you have any more questions.

Best,
Eli
Photo of Brian Sprogø

Brian Sprogø

  • 11 Posts
  • 0 Reply Likes
Hi Eliezer,

Still struggeling.

My $folder contains a folder object of the type "ShareFile.Api.Models.Folder"

I create an object of the type "ShareFile.Api.Models.Share"
I set the sharetype of this object to "[ShareFile.Api.Models.ShareType] 'Send'"

The problem lies in the Items property.
As far as I can see, it expects a type of "ShareFile.Api.Models.Item"

So how would I add the Folder object to the Items property?
Tried with:  [Collections.Generic.List[String]]@("$($Folder)")
But it errors out:

Exception setting "Items": "Cannot convert the "System.Collections.Generic.List`1[System.String]" value of type "System.Collections.Generic.List`1[[System.String, mscorlib, Ver
sion=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" to type "System.Collections.Generic.IEnumerable`1[ShareFile.Api.Models.Item]"."
At line:1 char:5
+     $rp.Items = [Collections.Generic.List[String]]@("$($Folder)")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

I cannot seem to find any documentation on these things - would love to be able to help myself :)

Side question:

When I create a folder - how do I control whether email notifications are sent for uploads and downloads respectively?

I create the folder this way:

$FolderInfo = '{ "Name":"' + $FolderName + '", "Description":"' + $FolderDescription + '"}'
$Folder = Send-SfRequest -Client $global:sfLogin -Entity Items -Method POST -Navigation Folder -BodyText $FolderInfo -Id $SharefileDownloadParentFolder

I guess i should/could manipulate the folder after creation, but unsure how.
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 732 Posts
  • 102 Reply Likes
Hey Brian,

When struggling with the quirkiness of Powershell's type system, I personally resort to working with simple Json, so that's what I'd recomend here.
$itemId = "fo3258fb-e73d-4b18-a6f1-5c077699df5e"
$share = @{
    ShareType="Send";
    Title="Sample title";
    Items=@(@{Id=$itemId});
    RequireLogin="false"
} | ConvertTo-Json
$createdShare = Send-SfRequest -Client $sfc -Entity Shares -Method POST -BodyText $share 

The key here is just building the Share as an anonymous object, converr it to Json, and then use the -BodyText parameter which I see you're familiar with. This worked for me, let me know if it works for you.

Regarding your side question, upload/download notifications are managed by the access controls endpoint. You can update these values like this, assuming you're trying to update permissions for the folder creator indicated by the user Id

$folderId = "fo3258fb-e73d-4b18-a6f1-5c077699df5e"
$userId = "607fd775-b3f2-4aaa-a84a-33c7ee144a21"
$accessControls = @{
    Principal = @{"Id"=$userId};
    NotifyOnUpload="true";
    NotifyOnDownload="true";
    CanManagePermissions="true"
} | ConvertTo-Json
$acl= Send-SfRequest -Client $sfc -Method PATCH -Entity Items -Id $folderId -Navigation AccessControls -BodyText $accessControls 

Let me know if you run into any issues with these requests.

Eli
Photo of Brian Sprogø

Brian Sprogø

  • 11 Posts
  • 0 Reply Likes
Hi Eliezer,

Thanks again for your time with this.

I have tried both code snippets, and they are working as expected.

This makes it possible for me to almost close a big implementation of sharefile automation :)

Only point that worries me a bit, is the problem around 0KB files when using sync-sfitem.
Right now I cannot reproduce the problem, but I have been fighting with this for 3-4 months now - and I am not the only one I can see.

https://community.sharefilesupport.com/citrixsharefile/topics/downloads-with-sync-sfitem-is-0kb
https://community.sharefilesupport.com/citrixsharefile/topics/psdk-sync-not-working-properly-0kb-fil...

If you have any ideas around this - or can point me to anyone who can help, I would be very happy...

Merry christmas to you :)

/Brian
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 732 Posts
  • 102 Reply Likes
Brian,

We had a Powershell release a couple days ago that we're hoping will solve all instances of the 0KB error, you can download the latest SDK here: https://github.com/citrix/ShareFile-PowerShell/releases

I'll post that on that thread if it hasn't been updated already.

Thanks!
Eli