Hi, I want to upload and download file from sharepoint using .net web api.
Loading
Hi, I want to upload and download file from sharepoint using .net web api.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 7, 2024, 5:15 AM EST
To upload and download files from SharePoint using AD authentication in a .NET Web API, you can utilize the SharePoint Client Object Model (CSOM). Below is a concise example demonstrating both operations.
Uploading a Fileusing Microsoft.SharePoint.Client; public void UploadFile(string siteUrl, string filePath, string targetFolder) { var securePassword = new SecureString(); foreach (char c in "your_password") securePassword.AppendChar(c); using (var context = new ClientContext(siteUrl)) { context.Credentials = new NetworkCredential("your_username", securePassword, "your_domain"); var targetList = context.Web.Lists.GetByTitle("Documents"); var fileCreationInfo = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(filePath), Url = Path.GetFileName(filePath), Overwrite = true }; var uploadFile = targetList.RootFolder.Files.Add(fileCreationInfo); context.ExecuteQuery(); } }public void DownloadFile(string siteUrl, string fileUrl, string downloadPath) { var securePassword = new SecureString(); foreach (char c in "your_password") securePassword.AppendChar(c); using (var context = new ClientContext(siteUrl)) { context.Credentials = new NetworkCredential("your_username", securePassword, "your_domain"); var fileInfo = File.OpenBinaryDirect(context, fileUrl); using (var fileStream = new FileStream(downloadPath, FileMode.Create)) { fileInfo.Stream.CopyTo(fileStream); } } }Ensure you replace placeholders like
your_username,your_password, andyour_domainwith actual values. This approach allows seamless interaction with SharePoint while leveraging AD authentication for secure access.