Search code examples
powershellsharefile

How to generate a Sharefile URL using the API and Powershell


Is there a way to generate a url to download files either when uploading or after a file has been uploaded?

I'm working a powershell script that will either upload a single file or zip a directory and upload it to Sharefile.

I have most of it complete, but I cannot figured out how generate a download url or have one created at the time of the upload.

Code for the form below

###### Share Link Automation

### Environment Setting

If (-not(test-path -path "C:\windows\ARP\ShareLink")) {
    New-Item -Force -Path C:\Windows\ARP\ShareLink -ItemType Directory
}

$SFLinkDestination = "C:\windows\ARP\ShareLink"

# Function to upload files to share file
Function Upload-File {

    param (
        $LocalData
    )

Add-PSSnapin ShareFile

#Run the following interactively to create a login token that can be used by Get-SfClient in unattended scripts
$sfClient = Get-SfClient -Name ((Join-Path $env:USERPROFILE "Documents\Sharefile") + "\MySubdomain.sfps")


#upload directory is relative to the root of the account
#get the current user's home folder to use as the starting point
$ShareFileHomeFolder = (Send-SfRequest $sfClient -Entity Items).Url

# Create a PowerShell provider for ShareFile pointing to Personal Folsers\Send
New-PSDrive -Name sfDrive -PSProvider ShareFile -Client $sfClient -Root "\Personal Folders\Send" -RootUri $ShareFilePath

#upload all the files (recursively) in the local folder to the specified folder in ShareFile
Copy-SfItem -Path $LocalData -Destination "sfDrive:"

#Remove the PSProvider when we are done
Remove-PSDrive sfdrive
Exit
}

#### Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


#### The Form

[System.Windows.Forms.Application]::EnableVisualStyles()
$SFLinkForm                         = New-Object system.Windows.Forms.Form
$SFLinkForm.ClientSize              = '480,300'
$SFLinkForm.text                    = "Share File Link"
$SFLinkForm.BackColor               = "#ffffff"
$SFLinkForm.TopMost                 = $false
# $Image                              = [system.drawing.image]::FromFile("G:\APPS\temp\abp_main.bmp")
$SFLinkForm.BackgroundImage         = $Image
$SFLinkForm.BackgroundImageLayout   = "None"
$SFLinkForm.AutoSizeMode            = "GrowAndShrink"

$SFLinkFormTitle                           = New-Object system.Windows.Forms.Label
$SFLinkFormTitle.text                      = "Share File Link"
$SFLinkFormTitle.ForeColor                 = "DarkRed"
$SFLinkFormTitle.AutoSize                  = $true
$SFLinkFormTitle.width                     = 45
$SFLinkFormTitle.height                    = 20
$SFLinkFormTitle.location                  = New-Object System.Drawing.Point(20,100)
$SFLinkFormTitle.Font                      = 'Microsoft Sans Serif,13,style=Bold'

$SFLinkFormTitleZiP                           = New-Object system.Windows.Forms.Label
$SFLinkFormTitleZip.text                      = "Share File Zip Name:"
$SFLinkFormTitleZip.ForeColor                 = "DarkRed"
$SFLinkFormTitleZip.AutoSize                  = $true
$SFLinkFormTitleZip.width                     = 45
$SFLinkFormTitleZip.height                    = 20
$SFLinkFormTitleZip.location                  = New-Object System.Drawing.Point(20,175)
$SFLinkFormTitleZip.Font                      = 'Microsoft Sans Serif,10,style=Bold'

$SFLinkFormDescription                     = New-Object system.Windows.Forms.Label
$SFLinkFormDescription.text                = "When creating a link ..... TBD"
$SFLinkFormDescription.ForeColor           = "DarkRed"
$SFLinkFormDescription.AutoSize            = $false
$SFLinkFormDescription.width               = 450
$SFLinkFormDescription.height              = 20
$SFLinkFormDescription.location            = New-Object System.Drawing.Point(20,125)
$SFLinkFormDescription.Font                = 'Microsoft Sans Serif,10'

### Zip File Namer
$SFLinkFormZip = New-Object System.Windows.Forms.TextBox
$SFLinkFormZip.Location = New-Object System.Drawing.Size(20,200)
$SFLinkFormZip.Size = New-Object System.Drawing.Size(220,20)
$SFLinkForm.Controls.Add($SFLinkFormZip)

#### File Selector
$SFLinkFormFileBtn                   = New-Object system.Windows.Forms.Button
$SFLinkFormFileBtn.BackColor         = "#ffffff"
$SFLinkFormFileBtn.text              = "File"
$SFLinkFormFileBtn.width             = 90
$SFLinkFormFileBtn.height            = 20
$SFLinkFormFileBtn.location          = New-Object System.Drawing.Point(200,230)
$SFLinkFormFileBtn.Font              = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormFileBtn.ForeColor         = "#0"
$SFLinkForm.Controls.Add($SFLinkFormFileBtn)

$SFLinkFormFileBtnLabel                = New-Object system.Windows.Forms.Label
$SFLinkFormFileBtnLabel.text           = "Click to Select a File:"
$SFLinkFormFileBtnLabel.ForeColor      = "DarkRed"
$SFLinkFormFileBtnLabel.AutoSize       = $true
$SFLinkFormFileBtnLabel.width          = 25
$SFLinkFormFileBtnLabel.height         = 20
$SFLinkFormFileBtnLabel.location       = New-Object System.Drawing.Point(20,232)
$SFLinkFormFileBtnLabel.Font           = 'Microsoft Sans Serif,10,style=Bold'

$SFLinkFormFileBtn.Add_Click(
    {
        Add-Type -AssemblyName System.Windows.Forms
        $SFLinkFormFClick = New-Object System.Windows.Forms.OpenFileDialog
        $SFLinkFormFClick.Title = "Please Select File"
        $SFLinkFormFClick.InitialDirectory = $InitialDirectory
        $SFLinkFormFClick.filter = “All files (*.*)| *.*”
        # If ($SFLinkFormFClick.ShowDialog() -eq "Cancel") {
        If ($SFLinkFormFClick.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
            $LocalData = ($SFLinkFormFClick).FileName
            # [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0, 
            # [System.Windows.Forms.MessageBoxIcon]::Exclamation)
            }  
        
        Write-Host "The File Selected is ($SFLinkFormFClick).FileName"
        Write-Host "$LocalData"
        Upload-File -LocalData "$LocalData"
        $SFLinkForm.Close()
        Return 
    Exit
        # $LocalData = ($SFLinkFormFClick).FileName
        }
    
)

### Directory Selector
$SFLinkFormDirBtn                       = New-Object system.Windows.Forms.Button
$SFLinkFormDirBtn.BackColor             = "#ffffff"
$SFLinkFormDirBtn.text                  = "Directory"
$SFLinkFormDirBtn.width                 = 90
$SFLinkFormDirBtn.height                = 20
$SFLinkFormDirBtn.location              = New-Object System.Drawing.Point(200,255)
$SFLinkFormDirBtn.Font                  = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormDirBtn.ForeColor             = "#0"
$SFLinkForm.Controls.Add($SFLinkFormDirBtn)

$SFLinkFormDirBtnLabel                = New-Object system.Windows.Forms.Label
$SFLinkFormDirBtnLabel.text           = "Click to Select a Directory:"
$SFLinkFormDirBtnLabel.ForeColor      = "DarkRed"
$SFLinkFormDirBtnLabel.AutoSize       = $true
$SFLinkFormDirBtnLabel.width          = 25
$SFLinkFormDirBtnLabel.height         = 20
$SFLinkFormDirBtnLabel.location       = New-Object System.Drawing.Point(20,257)
$SFLinkFormDirBtnLabel.Font           = 'Microsoft Sans Serif,10,style=Bold'

$SFLinkFormDirBtn.Add_Click(
    {
    $SFLinkFormDClick = New-Object System.Windows.Forms.FolderBrowserDialog
            if ($SFLinkFormDClick.ShowDialog() -eq [System.Windows.Forms.DialogResult]::"OK") {
            $SFLinkFormDirName = $SFLinkFormDClick.SelectedPath
            $ZipWrapper = $SFLinkFormZip.Text
            $LocalData = (Join-Path C:\windows\ARP\ShareLink\ "$ZipWrapper.zip")
            ### Directgory Compression
            # cd C:\windows\ARP\ShareLink
            Compress-Archive "$SFLinkFormDirName\*" "$LocalData"
            Upload-File -LocalData "$LocalData"
            $SFLinkForm.Close()
            Return            
        Exit
                        }
    }
)


$SFLinkForm.controls.AddRange(@($SFLinkFormTitle,$SFLinkFormDescription,$SFLinkFormFileBtn,$SFLinkFormDirBtn,$SFLinkFormFileBtnLabel,$SFLinkFormDirBtnLabel,$SFLinkFormZip,$SFLinkFormTitleZiP))
$SFLinkForm.ShowDialog()
$SFLinkForm.Close()
Exit

Search all the API codes for Sharefile and could not find the powershell equivalence of "Get A Link" in the API Entities.


Solution

  • Figured it out code below

    Add-PSSnapin ShareFile
    
    $sfClient = Get-SfClient -Name ("path to MySubdomain.sfps")
    $PF = "xxxxxxxx-4c09-4312-9749-88ac56ae0290"
    
    $Folder = Send-SfRequest -Client $sfClient -Entity Items -Id $PF
    
    $fileInfo = '{
    "ShareType":"Send",
    "RequireUserInfo": true,
    "ExpirationDate":"2023-05-16",
    "MaxDownloads":-1,
    "IsViewOnly":false,
    "Settings":{"NotifyOnUse":true},
    "ShareAccessRight":{"odata.type":"ShareFile.Api.Models.ShareAccessRight",
    "DisplayText":"have full control",
    "AccessRightType":"FullControl",
    "odata.metadata":"https://CompanyDomain.sf-api.com/sf/v3/$metadata#ShareAccessRights/ShareFile.Api.Models.ShareAccessRight@Element",
    "Id":"",
    "url":"https://account.sf-api.com/sf/v3/Shares"},
    "Items":[{"Id":"xxxxxxxx-b3a5-6165-489d-b2104787c909"}]
    }'
    
    $Link = Send-SfRequest -Client $sfClient -Entity Shares -Method POST -BodyText $fileInfo
    
    $Link.Uri.AbsoluteUri