Create/Update URI Items/Link using Powershell

  • 1
  • Question
  • Updated 5 months ago
I am trying to create/update links in a shared directory using Powershell.

I create the link type using: 
$linkBody = New-Object Sharefile.api.models.link
Then I assign the Link Name and URI like this:
$linkBody.Name = "Bing Link"
$linkBody.Uri = "https://www.bing.com"
$linkBody looks like this:
$linkBody | select-object *
Uri                        : https://www.bing.com/
Name                       : Bing Link
FileName                   : 
Creator                    : 
Parent                     : 
AccessControls             : 
Zone                       : 
CreationDate               : 
ProgenyEditDate            : 
ClientCreatedDate          : 
ClientModifiedDate         : 
ExpirationDate             : 
Description                : 
DiskSpaceLimit             : 
IsHidden                   : 
BandwidthLimitInMB         : 
Owner                      : 
Account                    : 
FileSizeInKB               : 
Path                       : 
CreatorFirstName           : 
CreatorLastName            : 
ExpirationDays             : 
FileSizeBytes              : 
PreviewStatus              : ShareFile.Api.Models.SafeEnum`1[ShareFile.Api.Models.PreviewStatus]
MaxPreviewSize             : 
HasPendingDeletion         : 
AssociatedFolderTemplateID : 
IsTemplateOwned            : 
HasPermissionInfo          : 
State                      : 
StreamID                   : 
CreatorNameShort           : 
HasMultipleVersions        : 
Metadata                   : 
ESignatureDocument         : 
MetadataUrl                : 
__type                     : 
Id                         : 
url                        : 
The link I am trying to update looks like this:
Uri                        : http://www.google.com/
Name                       : Testing Link
FileName                   : 
Creator                    : 
Parent                     : ShareFile.Api.Models.Item
AccessControls             : 
Zone                       : 
CreationDate               : 2/6/2018 12:58:26 PM
ProgenyEditDate            : 
ClientCreatedDate          : 
ClientModifiedDate         : 
ExpirationDate             : 12/31/9999 4:59:59 PM
Description                : 
DiskSpaceLimit             : 0
IsHidden                   : False
BandwidthLimitInMB         : 0
Owner                      : 
Account                    : 
FileSizeInKB               : 0
Path                       : /root/accb7ab9-e7c9-4994-9e16-21c21cd24775/fo67a6f1-3a3a-496e-bdaf-fbced22d4a1b/fofa6073-8fe0-4121-b8c8-23dd73d39895
CreatorFirstName           : Information
CreatorLastName            : T. Dept.
ExpirationDays             : -1
FileSizeBytes              : 0
PreviewStatus              : ShareFile.Api.Models.SafeEnum`1[ShareFile.Api.Models.PreviewStatus]
MaxPreviewSize             : 
HasPendingDeletion         : True
AssociatedFolderTemplateID : 
IsTemplateOwned            : False
HasPermissionInfo          : True
State                      : 0
StreamID                   : st215299-3619-4762-99f8-61dacaee55d3
CreatorNameShort           : I. T. Dept.
HasMultipleVersions        : 
Metadata                   : 
ESignatureDocument         : 
MetadataUrl                : https://{mysite}.sf-api.com/sf/v3/$metadata#Items/ShareFile.Api.Models.Link@Element
__type                     : ShareFile.Api.Models.Link
Id                         : la3728f2-f685-1e3f-33cf-cbf2e438e6cc
url                        : https://{mysite}.sf-api.com/sf/v3/Items(la3728f2-f685-1e3f-33cf-cbf2e438e6cc)

I am trying to create a new link or update this existing link and having problems with both

When I try to create a new link I use this command:
Send-SfRequest -Client $sfC -Entity items -id "fofa6073-8fe0-4121-b8c8-23dd73d39895" -Body $linkBody -Method PATCH
It doesn't return an error or any output and the link is not created in my sharefile folder.

When I use the same structure ($linkBody) to update the existing link, it changes the link Name, but not the Uri.  So the updated link goes to www.google.com but is named Bing Link.
Send-SfRequest -Client $sfC -Entity items -id "la3728f2-f685-1e3f-33cf-cbf2e438e6cc" -Body $linkBody -Method PATCH
Can someone give me a hint as to what I might be doing wrong?

Thanks,

Zack
Photo of Zack Eleveld

Zack Eleveld

  • 19 Posts
  • 4 Reply Likes

Posted 5 months ago

  • 1
Photo of Ross Bender

Ross Bender

  • 56 Posts
  • 9 Reply Likes
Looking at the documentation to create a link, I think you should be using POST instead of PATCH: https://api.sharefile.com/rest/docs/resource.aspx?name=Items
Photo of Zack Eleveld

Zack Eleveld

  • 19 Posts
  • 4 Reply Likes
Thanks for the suggestion.  I ran it corrected as such and still nothing, no error message or link created:
Send-SfRequest -Client $sfc -Method POST -Id "fofa6073-8fe0-4121-b8c8-23dd73d39895" -Entity items -Body $linkBody
I'm hoping one of the SF admins will look into it soon and reply. 

They really should create tested PowerShell examples of every item in the API located here:
http://api.sharefile.com/rest/

Zack
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 696 Posts
  • 98 Reply Likes
Hey Zack,

I'm looking at your question, will post an answer soon. One thing I noticed quickly was that your code is using -Method PATCH to Create, where it should be using -Method POST. I'll test and confirm this and look at the update failure as well.

Eli
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 696 Posts
  • 98 Reply Likes
Zack,

We do have a known issue where trying to create a link without a "Description" field will cause an error, so I'd add a value there on your object to avoid that. Your command to create a Link is close, but it still needs the extra "/Link" path, which maps to the -Navigation property in Powershell. The complete command would be this:

$link = New-Object ShareFile.Api.Models.Link
$link.Name = "link"
$link.Uri = "https://mylink.com"
$link.Description = "my link"
Send-SfRequest -Method POST -Client $sfc -Entity Items -Id -Navigation Link -Body $link

To update an existing Link, instead of updating an Item Entity, you need to specify a Link is being updated. Once again, this requires the use of -Navigation Link, and using the Link id there
Send-SfRequest -Method PATCH -Client $sfc -Entity Items -Id  -Navigation "Link($link.Id)" -Body $link
I've tested those and the should work, let me know if you get the same result.

Thanks!
Eli
(Edited)
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 696 Posts
  • 98 Reply Likes
Zack,

What was the error when using the Link($link.Id) code?

Eli
Photo of Zack Eleveld

Zack Eleveld

  • 19 Posts
  • 4 Reply Likes
Send-SfRequest -Client $sfC -Method PATCH -id "la3728f2-f685-1e3f-33cf-cbf2e438e6cc" -Entity items -Navigation "Link($updLink.Id)" -Body $updLink
Send-SfRequest : Unable to retrieve HttpResponseMessage.Content
At line:1 char:1
+ Send-SfRequest -Client $sfC -Method PATCH -id "la3728f2-f685-1e3f-33cf-cbf2e438e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Send-SfRequest], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,ShareFile.Api.Powershell.SendSfRequest
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 696 Posts
  • 98 Reply Likes
I see. Do not include the -id parameter in the request. The issue we have here is that using the -id parameter maps that Id to the -Entity value, but we want the Id to be mapped to the -Navigation value, which is not something the current version of the SDK supports. So in this case, your raw request is going out as 

sf/v3/Items(la3728f2-f685-1e3f-33cf-cbf2e438e6ccla37)/Link(la3728f2-f685-1e3f-33cf-cbf2e438e6cc)

Excluding the -id parameter gives us what we need:

sf/v3/Items/Link(la3728f2-f685-1e3f-33cf-cbf2e438e6cc)
Photo of Zack Eleveld

Zack Eleveld

  • 19 Posts
  • 4 Reply Likes
I tried the following 2 options:
Send-SfRequest -Client $sfC -Method PATCH -Entity  items -Id -Navigation "Link($updLink.Id)" -Body $updLink
Send-SfRequest : Missing an argument for parameter 'Id'. Specify a parameter of type 'System.String' and try again.
At line:1 char:58
+ Send-SfRequest -Client $sfC -Method PATCH -Entity  items -Id -Navigation "Link($ ...
+                                                          ~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-SfRequest], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,ShareFile.Api.Powershell.SendSfRequest
and 
Send-SfRequest -Client $sfC -Method PATCH -Entity items -Navigation "Link($updLink.Id)" -Body $updLink
Send-SfRequest : Unable to retrieve HttpResponseMessage.Content
At line:1 char:1
+ Send-SfRequest -Client $sfC -Method PATCH -Entity items -Navigation "Link($updLi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Send-SfRequest], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,ShareFile.Api.Powershell.SendSfRequest
I even tried using the -id "fo######" with the parent folders Id and that didn't work.

By trial and error, this worked:

$link = New-Object ShareFile.Api.Models.Link
$link.Name = "link"
$link.Uri = "https://mylink.com";
$link.Description = "my link"
PS C:\Users\sync> $link | Select-Object *

Uri                        : https://mylink.com/
Name                       : link
FileName                   : 
Creator                    : 
Parent                     : 
AccessControls             : 
Zone                       : 
CreationDate               : 
ProgenyEditDate            : 
ClientCreatedDate          : 
ClientModifiedDate         : 
ExpirationDate             : 
Description                : my link
DiskSpaceLimit             : 
IsHidden                   : 
BandwidthLimitInMB         : 
Owner                      : 
Account                    : 
FileSizeInKB               : 
Path                       : 
CreatorFirstName           : 
CreatorLastName            : 
ExpirationDays             : 
FileSizeBytes              : 
PreviewStatus              : ShareFile.Api.Models.SafeEnum`1[ShareFile.Api.Models.PreviewStatus]
MaxPreviewSize             : 
HasPendingDeletion         : 
AssociatedFolderTemplateID : 
IsTemplateOwned            : 
HasPermissionInfo          : 
State                      : 
StreamID                   : 
CreatorNameShort           : 
HasMultipleVersions        : 
Metadata                   : 
ESignatureDocument         : 
MetadataUrl                : 
__type                     : 
Id                         : 
url                        :
Send-SfRequest -method PATCH -Client $sfC -Entity Items -Navigation "Link(la3728f2-f685-1e3f-33cf-cbf2e438e6cc)" -Body $link
So, I checked powershell syntax and it turns out if you want to expand a property inside of "", you have to $() it to make it work, so this is the correct command (provided you actually set the $link.id first:

$link.Id = "{your link's item id goes here}"
end-SfRequest -Client $sfC -Method PATCH -Entity  items -Navigation "Link($($link.Id))" -Body $link
It's probably easier to just put the id in a variable and not worry about moving forward from there.  Incidentally, the API definition for a link update says:

https://account.sf-api.com/sf/v3/Items/Link(id)?notify=false
{
  "Name":"Name",
&nbsp;&nbsp;"Uri":"<a target="_blank" rel="nofollow" href="https://server/path" title="Link: https://server/path">https://server/path</a>",
&nbsp;&nbsp;"Description":"Description",
&nbsp;&nbsp;"Parent": { "Id": "parentid" }
}

This Powershell method doesn't require the "Parent:" part to be filled in.

How would you specify the "notify=false" if you wanted to use it?

Zack
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 696 Posts
  • 98 Reply Likes
Ah, makes sense. Yes, when I tested I didn't save the Id as a property of an object, I used a simple string variable, so that would work.

The parameters you pass on the body of the request for an update are the ones you need to edit. If you don't want to move the Link (which requires passing a new parent object), there's no need to include that value.

Query string parameters in the sdk are passed with the "-Parameters" flag + an anonymous object, like this:

Send-SfRequest .... -Parameters @{"notify"="false"}