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:
- Since every file in the SharePoint folder will have its own source doc. Can I get the sourcedoc from the filename which user pass as an input?
- Can we download file without this sourcedoc only with the help of filename?
SharePoint folder structure:
www.abcsite.com/MainFolder/SubFolder/FileName
FileFormat: .pptx
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 (
$valueat the end is important!):If you test this URL in a web browser, the filename will be
$valuebut with your C# code you can rename the downloaded content to something likeFileName.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.