Get onedrive file URL from a the locally cached file - c#

I am looking for a way to derive the OneDrive file URL for a file cached to my local OneDrive folder? The only thing I can think of is hardcoding some root URLS for each of the OneDrive folders I have, but this seems nasty!
Does anyone know of any OneDrive client API that lets query a URL based on the local file path?
My use case:
I am trying to attach to and open instance of an Excel workbook. I used to be able to do this Marshal.BindToMoniker(_workbookPath);
However it appears that Excel is now registering the OneDrive URL in the ROT rather than the local file path. This this happened with the update that brought the new auto-save feature to Excel 2016 I think, that seems to be about the time my existing code broke.
There is a similar unanswered question here: C# OneDrive for Business / SharePoint: get server path from locally synced file

Might want to use the following key instead:
HKEY_CURRENT_USER\Software\SyncEngines\Providers\OneDrive
This includes the following registry values:
UrlNamespace: (SharePoint site URL)
MountPoint: (local driver location)
It does appear to include old values which are no longer synced - but it shouldn't be too hard to check against
HKEY_CURRENT_USER\Software\Microsoft\OneDrive\Accounts\Business1\ScopeIdToMountPointPathCache
or
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SyncRootManager
for paths that are being actively synced.

I am also looking at a solution for getting the SharePoint url to a file in OneDrive Synced File Explorer.
I have noticed you get under C:\Users%username%\AppData\Local\Microsoft\OneDrive\settings\Business1{GUID}.ini a mapping between your OneDrive synced folder and the SharePoint GUID
Example:
libraryScope = 1 630c2a866d9c458b81060eff107887ed+1 5 "GUIDEs" "Documents" 4 "https://mytenant.sharepoint.com/teams/team_10000035" "8d4b558f-7b2e-40ba-ad1f-e04d79e6265a" e0266a43caf347238f684bab486f4e51 e0d25dcb1a014f5f86d787984f6327c2 4f86b3e3e54e42e0bb0f7a58eadf0335 0 "" 0 4cde5c00-3fe3-4162-b831-d8ef440e1593 libraryFolder = 0 1 8bbfe07dfeff41cea7ab5da4a554592a+1 1558084235 "D:\DSUsers\uid41890\TenantName\GUIDEs - General" 2 "General" bd0c1b7c-2a1f-4492-8b1b-8e152c9e0c26
You also have this mapping in the registry Computer\HKEY_CURRENT_USER\Software\Microsoft\OneDrive\Accounts\Business1\ScopeIdToMountPointPathCache
From the GUID you could get the path using SPWeb.GetFile(Guid)
If you can make a standalone function given a local OneDrive file path that returns the SharePoint url, I would greatly appreciate you share your solution here.

I have implemented a solution in AutoHotkey see documentation here
It is based on #GWD idea (see comments below) to generate a temporary excel file with the formula CELL("filename") at each sync locations to extract this mapping information to a text file that is then later parsed to do the reverse mapping from local file to SharePoint url.

Related

Application can find 'Downloads' folder when debugging but not on IIS

I have an application that allows the user to upload a file (saving it to in a folder located in the wwwroot of the ASPNETCORE application). From here they can make edits to it and then they can choose to export the file as a csv/ xml/ xlsx which downloads the file to the user's 'downloads' folder.
While debugging in Visual Studio this all works fine however when I publish and deploy the application to IIS I am getting the exception
Error saving file C:\windows\system32\config\systemprofile\Downloads(FILE NAME)
Could not find part of the path C:\windows\system32\config\systemprofile\Downloads(FILE NAME)
This is the current way I am getting the downloads folder:
FileInfo file = new FileInfo(Path.Combine(Environment.ExpandEnvironmentVariables(#"%USERPROFILE%\Downloads"), data.Filename + "." + data.FileType));
However I have also tried the solution that Hans Passant has answered to a similar question here. Both solutions worjk fine while debugging locally however as soon as I publish them, this one produces the exception:
Value cannot be null. Parameter name: path1
Which I presume is thrown at this point here when I try and save the file to the user's download folder.
using (var package = new ExcelPackage(file))
{
var workSheet = package.Workbook.Worksheets.Add("ExportSheet");
workSheet.Cells.LoadFromCollection(exports, true);
package.Save();
}
I don't really know how I would be able to reproduce these exceptions seeing as locally using Visual Studio it all works fine.
Has anyone else came across this issue while trying to download a file?
UPDATE: When the application is running on IIS, it seems to be using that as the user profile instead of the actually user, so when it tries to navigate to the Downloads folder, it cannot find it. How can I force it to use the user's profile?
LoadUserProfile is already set to True.
Web applications have no knowledge of the end-user's computer's filesystem!
So using Environment.GetFolderPath or Environment.ExpandEnvironmentVariables in server side code will only reveal the server-side user (i.e. the Windows Service Identity)'s profile directories which is completely separate and distinct from your web-application's actual browser-based users OS user profile.
As a simple thought-experiment: consider a user running a weird alien web-browser on an even more alien operating system (say, iBrowse for the Amiga!) - the concept of a Windows-shell "Downloads" directory just doesn't exist, and yet here they are, browsing your website. What do you expect your code would do in this situation?
To "download" a file to a user, your server-side web-application should serve the raw bytes of the generated file (e.g. using HttpResponse.TransmitFile) with the Content-Disposition: header to provide a hint to the user's browser that they should save the file rather than try to open it in the browser.

To browse folder and Create folder in the server and upload files using asp.net

I am Currently working on asp.net i have worked on file upload by uploading files but i want browse a folder not a file and get all files in it and get foldername and create foldername in server and upload files it to server.Pls give me some refernce and help me to do this.
My aim is when i browse that folder all it files present in it should upload .Only files should upload not folder
Note:To browse folder not file.
Actually you are supposed to include a question if you want an answer, but it seems you are completely new to this so here are a few things you should know when it comes to asp.net and folder/file handling:
Your virtual path always corresponds to a local path on your webserver. Since it would be bad to hardcode that you might want to start off by mapping it. (e.g. /uploads/ to _C:\intepub\application\uploads)
"and get foldername"
string localpath = Server.MapPath("/uploads");
"and create foldername in server"
next you can check if that folder already exists, or if not - create it.
if(!System.IO.Directory.Exists(localpath))
System.IO.Directory.Create(localpath))
Keep in Mind though that your IIS-User needs the rights to create new directories
Now that you have made sure that your upload directory exists, you can upload/download files. Here is a good tutorial to get started:
"and upload files it to server"
http://msdn.microsoft.com/en-us/library/aa479405.aspx
"but i want browse a folder not a file and get all files in"
You can do that on the server, not the client. Your asp.net code never executes on the client.
string[] filenames = System.IO.Directory.GetFiles(localpath);
"My aim is when i browse that folder all it files present in it should upload"
That one isn't so easy, sorry. It would pose a serious security risk to users.
Maybe this will point you in the right direction (apparently it works with Chrome)
How do I use Google Chrome 11's Upload Folder feature in my own code?
When it comes to files, directories, filepaths, etc. in general, I would recommend taking a closer look at these classes:
System.IO.File
System.IO.Directory
System.IO.Path

Server.MapPath Access is denied On Azure

I am exporting an excel using c# code and spreadsheetlight dll.
I am trying to insert a picture into excel, everything works well on local machine but on live web site it gives following error
Access to the path 'F:\sitesroot\0\Images\Logo.png' is denied
My c# code.
string filePath = Server.MapPath("~/Images/Logo.png");
SLPicture pic = new SLPicture(filePath); //SLPicture - An inbuilt class for inserting image
Currently what I am doing is manually providing access rights on IIS server but which is not the correct way to go since each time I upload the service I have to redo that again and again
Any expert advice - How I can provide access right through code?
Or How to get rid of this error.
Thanks.
Assuming you are using Cloud services...
You are supposed to use Local Storage for this (http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx).
Basically;
In your Cloud project, go to the properties for the Role and select Local Storage.
Add a new local storage and give it a name, i.e. "foo" and a maximum size. If this is for temporary processing rather than caching, you probably want to tick "Clean on role recycle".
In your code call RoleEnvironment.GetLocalResource("foo") to get the folder name you can use and then do something like Path.Combine("path","Logo.png") to get a file name which you can then write to.

C# Upload Files on another partition of the server

I'm using FileUpload.SaveAs() function of C# to upload files to the server but I want to save the files on another partition. Let us say, save the files on Drive D of the server instead on the current drive which is Drive C. Please share your thoughts. Thanks is advance.
I have learned that using full path such as
FileUpload.SaveAs("D:\FileUpload");
will save the file outside the web server.
Check this out.
To simplify the question, how can I upload files on the other partition of the server that hosts my web app?
Based on the documentation from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx, the String filename is the full path name of the location to save. Meaning you should be able to do so e.g:
FileUpload.SaveAs("D:\where_you_want_to_save")
By the way what have you tried and what error did you get?
Looking at the example on MSDN, it would appear that .SaveAs() accepts a fully qualified file name as a parameter. You could potentially use a Path object to cleanly build a path for the file, or just specify one directly as a string:
uploader.SaveAs("d:\\someFolder\\someFile.ext");
Resolved this by using Virtual Directory of IIS and providing admin credentials for authentication

Getting the full file path when using a file upload control

I wrote some code to upload files to amazon S3, if I put a full file path manually It successfully uploads the file from my computer. What I'm trying to do is use a file upload control and store the full path in a variable so that I can use it for my amazon method. Ive read everywhere it seems that the browser won't let you get the full file path for security reasons.
How can I get the full file path? Should I just store the files on my webserver and point my amazon method to the server path, and then use the file upload control to tell it what the filename is? I wish I could just do a straight shot to amazon...
First we have to save the file path and then we take it from
string filepath=Path.GetFullPath(UploadFile1.FileName.toString());
I came across this link which has a great tutorial and even gives you a working sample project. (this is different from the code that the .net SDK includes...) http://aws.amazon.com/articles/774?_encoding=UTF8&jiveRedirect=1
We can't take full path in HTML or JS as it violets security so whenever you try to see the path it shows fakepath
so to resolve this issue you can make a seprate folder and you can store the uploaded file there and in the code you can take that folders path as default and use it as a absolute path.
You can get the full path using Python Tkinter but it is limited for desktop app.

Categories