Reputation: 193
Code:
using (ClientContext clientContext = new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientID, clientSecret))
{
Web web = clientContext.Web;
fileUrl = downloadUrl + fileUrl + ".pptx";
Microsoft.SharePoint.Client.File filetoDownload = web.GetFileByUrl(fileUrl);
clientContext.Load(filetoDownload);
clientContext.ExecuteQuery();
Helper.CreateIfMissing(filePath);
var stream = filetoDownload.OpenBinaryStream();
clientContext.ExecuteQuery();
var fileName = Path.Combine(filePath, (string)filetoDownload.Name);
// Add in Config for downloadload
using (var fileStream = new FileStream(fileName, FileMode.Create))
{
stream.Value.CopyTo(fileStream);
}
path = fileName;
}
Config:
"downloadUrl": "https://abc.sharepoint.com/:p:/r/sites/abcFolder/_layouts/15/Doc.aspx?sourcedoc=%7BA6BACCWE-E545-4823-A765-749100023273%7D&file="
I want to download the file and have 2 approaches in mind:
SharePoint folder structure:
www.abcsite.com/MainFolder/SubFolder/FileName
FileFormat: .pptx
Upvotes: 0
Views: 640
Reputation: 193
There is an easy to get this done with the same code. Just make your url like this in app config old download url: "downloadUrl": "https://abc.sharepoint.com/:p:/r/sites/abcFolder/_layouts/15/Doc.aspx?sourcedoc=%7BA6BACCWE-E545-4823-A765-749100023273%7D&file="
new url "downloadUrl": "https://abc.sharepoint.com/MainFolder/Subfolder/FileName
In c# code attach the file name to this url with file extension and pass it to the same getfilebyUrl method fileurl=downloadurl+filename+".pptx"
Upvotes: 0
Reputation: 1078
An easy way to get a document is to build a REST API url. To download a document stored in a document library, you can use this URL with a GET method ($value
at the end is important!):
https://abc.sharepoint.com/sites/abcFolder/_api/web/GetFileByServerRelativeUrl('/sites/abcFolder/abcLibrary/MainFolder/SubFolder/FileName.pptx')/$value
If you test this URL in a web browser, the filename will be $value
but with your C# code you can rename the downloaded content to something like FileName.pptx
.
So, yes, you can download files based on their name if you know in which site/library/folder they are stored. Do not use Doc.aspx?sourcedoc
.
Upvotes: 0