I have a local folder that is linked with a document collection in SharePoint. When I modify/add/remove files in this folder and its' sub-folders, it all syncs up with SharePoint. I need to determine the URL for an item/file using the local file path so that I can create an anonymous link programatically. I can hack it together but I'd rather use the same mechanism this folder setup uses.

So, go from this: C:\Users\Me\My Corp\Something - Documents\Account Management\Bob\January 2020 Reports\Suzy\awesome.xlsx

to this: https://ourSPsite.com/sites/Something/Shared Documents/Account Management/Bob/January 2020 Reports/Suzy/awesome.xlsx

All my searches always come up with how to manipulate lists and items via SharePoint ClientContext, not how to look at a local file and figure out where it lives in SharePoint.

Here's my hack

 var startIdx= localFilePath.IndexOf("Bob");
 var listItemUrl = localFilePath.Substring(startIdx).Replace('\\', '/');
 var itemUrlFull = $"{siteFullUrl}/Shared Documents/Account Management/{listItemUrl}";

 var link = web.CreateAnonymousLinkForDocument(itemUrlFull, ExternalSharingDocumentOption.View);

There's a handy context menu when you right click a file/folder in Explorer that says View Online. I'd like that chunk of code!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.