How to convert Share file path to URL?

  • 1
  • Question
  • Updated 6 months ago
Hello team,

I am hoping you can help me out. I find myself often needing to convert sharefile path like the one in drive mapper to sharefile url. For example S:\Shared Folders\Asia\Japan to https://OurDomain.sharefile.com/app/#/home/shared/fob354af-176c-411a-9566-9958e4a8f3b7
How  would i go about doing this?

We have script we use to convert the other way around:

Add-PSSnapin ShareFile

#Parameters-------------------
If(test-path "C:\Scripts\file.sfps”)
{
$sfclient=Get-sfclient –Name "C:\Scripts\file\.sfps”
}
Else
{
New-sfclient –Name "C:\Scripts\file.sfps”
$sfclient=get-sfclient –Name "C:\Scripts\file.sfps”
} 



$ItemID = Read-Host -Prompt 'Input sharefile url'
$ItemID = $ItemID.Split("/")[-1] 
$folderpath=""
while ($itemID -ne "ae9b2103-b605-40e9-9038-xxxxxxxxxxxx"){
$sfFolder = Send-SfRequest -Client $sfclient  -Entity Items -Id $ItemID
$folderpath=$sfFolder.Name+"\"+$folderpath
$ItemID=$sfFolder.Parent.Id

}
#output the results

write-host "`nS:\shared with me\$folderpath`n"


I am hoping someone can suggest a reverse script or an alternative.
Photo of Nikita Levitan

Nikita Levitan

  • 22 Posts
  • 2 Reply Likes

Posted 6 months ago

  • 1
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 695 Posts
  • 98 Reply Likes
Nikita,

There are a couple API calls you can use to achieve what you need.

1. Get the ShareFile Item by Path:

GET https://domain.sf-api.com/sf/v3/Items/ByPath?path=/Shared Folders/Asia/Japan

This request will give you the full Item object with an Id property, which you can use on the next call

2. Get a Folder link by Id:

For this you could manually construct the url using the Id, but we don't recommend doing that because we may change that url format at any time. The best way to do it is using this API

POST https://domain.sf-api.com/sf/v3/Items(id)/WebAppLink

It will generate a link that when clicked will automatically login the user to the Webapp using the credentials of the authenticated API user when the link was generated.


Let me know if this works for you.

Best,
Eli
Photo of Nikita Levitan

Nikita Levitan

  • 22 Posts
  • 2 Reply Likes
Thanks Eli,

Maybe you misunderstood my question, per your instruction, i tried this code:

dd-PSSnapIn ShareFile
#Set these values with sharefie IDs
$DestID ='fo9d1d56-ad91-42c5-b87b-de4656a90c11' # Authenticate to sharefile $sfpsFile = "c:\users\" + $env:UserName + "\documents\login.sfps" If(test-path $sfpsFile)
{
$sfclient=Get-sfclient –Name $sfpsFile
}
Else
{
New-sfclient –Name $sfpsFile
$sfclient=get-sfclient –Name $sfpsFile
}
GET https://domain.sf-api.com/sf/v3/Items/ByPath?path=/Shared Folders/Asia/nikita/source
I expected to get this response:
https://welocalize.sharefile.com/app/#/home/shared/foc802a0-e814-4d59-b99e-a604b18f0e14

However, i am not getting any output. Can you edit my code and let me know what am i missing? Also I don't like the fact that I need to manually convert all the the slashes in the Drivemapper path ( \ to / ). Can this be solved as well? 
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 695 Posts
  • 98 Reply Likes
Nikita,

Apologies, the requests I gave you where using the Http format that's found in our documentation. I can help you translate those to Powershell. Regarding the slashes, this is simply a result of how Windows uses the backslash as a separator, and the web uses the forward slash, and it's not a ShareFile implementation.
The two requests I mentioned should be executed like this in powershell:

1. $folder = Send-SfRequest -Client $sfclient -Entity Items -Navigation ByPath -Parameters @{"path"=<your_folder_path>}

2. $link = Send-SfRequest -Client $sfclient -Method POST -Entity Items -Id $folder.Id -Navigation WebAppLink


That will give you back a link in the format https://welocalize.sharefile.com/?encparams=<encoded_value>. Once a user clicks on that link, they'll be redirected to the desired folder on the web application.

Let me know if that works.

Thanks!
Eli
Photo of Nikita Levitan

Nikita Levitan

  • 22 Posts
  • 2 Reply Likes
Thanks Eli,
here is the full script in case anyone needs it in the future:

Add-PSSnapIn ShareFile



# Authenticate to sharefile

$sfpsFile = "c:\users\" + $env:UserName + "\documents\login.sfps"

If(test-path $sfpsFile)
{
$sfclient=Get-sfclient –Name $sfpsFile
}
Else
{
New-sfclient –Name $sfpsFile
$sfclient=get-sfclient –Name $sfpsFile
} 


$myPath = Read-Host -Prompt 'Input path starting with S:\Shared Folders or S:\Shared with Me.'
$myPath = $myPath -replace "S:", ""
$myPath = $myPath -replace "Shared with me", "Shared Folders"
$myPath = $myPath -replace "\\", "/"



$sfFolder = Send-SfRequest -Client $sfclient -Entity Items -Navigation ByPath -Parameters @{"path"=$myPath}
$folderID=$sfFolder.id

#output the results

write-host "`nhttps://yourdomain.sharefile.com/app/#/home/shared/$folderID`n" -foregroundcolor "green"
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Official Rep

  • 694 Posts
  • 98 Reply Likes
Nikita,

Thanks for the script. One think to note is that we don't recommend hardcoding that App url format in the script because we may change that url format (and we have) as needed. That's why we provided the API endpoint to construct an authenticated link based on an Item Id.

Thanks!
Eli
Photo of Nikita Levitan

Nikita Levitan

  • 22 Posts
  • 2 Reply Likes
Here is the script, including encoded link per Eli's suggestion above. For our use, we actually need the sorter version, and i guess we will have to edit it if you decide to change it. Last time you made changes to it, it created issues for us. Could you notify users ahead of time about such changes?

Add-PSSnapIn ShareFile



# Authenticate to sharefile

$sfpsFile = "c:\users\" + $env:UserName + "\documents\login.sfps"

If(test-path $sfpsFile)
{
$sfclient=Get-sfclient –Name $sfpsFile
}
Else
{
New-sfclient –Name $sfpsFile
$sfclient=get-sfclient –Name $sfpsFile
} 


$myPath = Read-Host -Prompt 'Input path starting with S:\Shared Folders or S:\Shared with Me.'
$myPath = $myPath -replace "S:", ""
$myPath = $myPath -replace "Shared with me", "Shared Folders"
$myPath = $myPath -replace "\\", "/"



$sfFolder = Send-SfRequest -Client $sfclient -Entity Items -Navigation ByPath -Parameters @{"path"=$myPath}
$folderID=$sfFolder.id

$sflink = Send-SfRequest -Client $sfclient -Method POST -Entity Items -Id $folderId -Navigation WebAppLink
$link = $sflink.uri

#output the results

write-host "`nhttps://yourdomain.sharefile.com/app/#/home/shared/$folderID`n"; -foregroundcolor "green"

write-host "`n
Encoded_value Link
`n$link`n" -foregroundcolor "red"