PowerShell Webhook Events

  • 1
  • Question
  • Updated 4 months ago
Since i cant edit the ShareFile notifications for uploads. i would like to run a powershell script do check if there is a new file in the folder and send an e-mail.

https://api.sharefile.com/rest/webhooks/events.aspx 

not quite sure where to start with it i made users/groups before. so i got the sfps file.  was hoping someone has an example script for Webhook events/powershell. 
Photo of Rico Droste

Rico Droste

  • 16 Posts
  • 2 Reply Likes

Posted 5 months ago

  • 1
Photo of Michael Berrier

Michael Berrier, Employee

  • 123 Posts
  • 24 Reply Likes
Rico,

You can certainly use PowerShell to subscribe to events by calling the ShareFile API. You will need the latest version of the PowerShell SDK. You will also need a server running that can listen to the webhook callbacks from ShareFile.

See below for a sample. Consider this working but relatively untested. Replace <path to your sfps file> with the location of your ShareFile PowerShell credential file. Replace <your webhook URL> below with the URL of your service listening for callbacks. Replace <your folder ID> with the ID of the folder to which you want to subscribe (e.g., 'fo11111-1111-1111-1111-111111111111').

$sfClient = Get-SfClient -Name 'path to your sfps file'

$subscription = New-Object ShareFile.Api.Models.WebhookSubscription

$subscription.WebhookUrl = 'your webhook url'

$subscriptionContext = New-Object ShareFile.Api.Models.SubscriptionContext
$subscriptionContext.ResourceType = 'Folder'
$subscriptionContext.ResourceId = 'your folder id'
$subscription.SubscriptionContext = $subscriptionContext

$event = New-Object ShareFile.Api.Models.SubscribedResourceEvent
$event.ResourceType = 'File'
$event.OperationName = 'Upload'
$events = New-Object System.Collections.Generic.List[ShareFile.Api.Models.SubscribedResourceEvent]
$events.Add($event)
$subscription.Events = $events

$newSubscription = Send-SfRequest $sfClient -Method POST -Entity WebhookSubscriptions -Body $subscription

Let me know if you have any other questions.

Michael
(Edited)
Photo of Rico Droste

Rico Droste

  • 16 Posts
  • 2 Reply Likes
ok i got the folder id and sfps but not sure where to start with the webhook url i used :

https://obdnoordwest-poc.sf-api.com/sf/v3/WebhookSubscriptions 

but that doesnt seem to work also i dont know how to trigger  the script to run another script (send e-mail for example) after a file was uploaded. (Real time)
Photo of Michael Berrier

Michael Berrier, Employee

  • 123 Posts
  • 24 Reply Likes
Rico,

Sorry, I don't follow your question. The whole thrust of webhooks is asynchronous event processing, so I wouldn't expect the same script that created the subscription would somehow also be handling the callbacks when events occur.

The URL you provided there is the ShareFile V3 API endpoint for webhook subscriptions. That would effectively be hit by the last line of the example script I provided above (if obdnoordwest-poc is the account represented by your PowerShell credential file). It's used to create the webhook subscription.

You also need an endpoint hosted by you to receive callbacks from ShareFile when events occur and that is what you provide in the WebhookUrl field. We can't tell you what this URL should be -- it's the location of a service you implement and provide. This endpoint needs to be capable of handling the event callback, the payload for which is documented here:
http://api.sharefile.com/rest/webhooks/payloads.aspx

In your proposed flow, that endpoint is what would be responsible for sending an email notification. PowerShell would be an unusual choice to implement the server side logic for this, but I suppose you could if you really wanted to. C#, Java, PHP, Python, or Ruby would be more common.

Michael
Photo of Rico Droste

Rico Droste

  • 16 Posts
  • 2 Reply Likes
Hello Michael,

Thank you for you're response, i will check if its indeed needed to use powershell for this. i wish that there was a way to edit the current e-mail notification - https://community.sharefilesupport.com/citrixsharefile/topics/customizing_email_notifications_when_n... ) 
Photo of Rico Droste

Rico Droste

  • 16 Posts
  • 2 Reply Likes
Ok so i tested it some more and i got to the point that i get a Post 405 Method not Allowed back.

using Windows server 2016 with IIS

tried following http://api.sharefile.com/rest/webhooks/payloads.aspx but i am not sure how to do this in IIS. 
Photo of Michael Berrier

Michael Berrier, Employee

  • 123 Posts
  • 24 Reply Likes
Rico,

What is the URL you are hitting that returns an HTTP 405 response?

And where are you having difficulty with the payload parsing? If you're considering using IIS, I assume you are writing a web application in C# that will receive the webhook callback.

Michael
Photo of Rico Droste

Rico Droste

  • 16 Posts
  • 2 Reply Likes
i am not sure where to start with this really all i want is a notifcation once a file is uploaded and then start a powershell script. 



The code i get back :

{
    "WebhookSubscriptionId": "7969cfe7-1cc7-3b61-6bbe-4de9d395d4df",
    "AccountId": "ac9830d3-addc-7e95-ab44-8ff50860166f",
    "AccountInfo": {
        "AccountId": "ac9830d3-addc-7e95-ab44-8ff50860166f",
        "Subdomain": "obdnoordwest-poc",
        "AppControlPlane": "sharefile.eu",
        "ApiControlPlane": "sf-api.eu"
    },
    "Event": {
        "Timestamp": "2018-04-09T08:24:18.6315597Z",
        "EventUserId": "c3518858-3209-415d-ba54-a9c2fe0bd155",
        "OperationName": "Upload",
        "ResourceType": "File",
        "Resource": {
            "Id": "fifa3128-e985-638b-a557-8ddfc7e6ba20",
            "StreamId": "st2292ec-00ab-46fd-9174-9178e8b6553d",
            "Parent": {
                "Id": "fo657f07-6fcd-45a3-8e66-042dfc91ba87"
            }
        }
    }&nbsp;
}
Photo of Michael Berrier

Michael Berrier, Employee

  • 123 Posts
  • 24 Reply Likes
Rico,

That is an example of the notification that a file was uploaded. You will need to implement a service that can accept that payload and then take whatever actions you desire based on its content, which can include kicking off a script if that's what you want to do. ShareFile should reliably send you these webhook callbacks after upload events, but it's up to you from there to decide how to handle them.

Michael