Need a console application in C# to Upload files to SharePoint location

Babu 21 Reputation points
2021-12-15T23:52:22.5+00:00

Hi All,

Can somebody please provide me a console application in C# to upload files from my local drive to sharepoint location

Any links or full code please

Regards,
Babu

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,261 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,671 questions
{count} votes

2 answers

Sort by: Most helpful
  1. satya karki 986 Reputation points MVP
    2022-11-08T12:26:19.623+00:00

    Hi,

    Here is a complete sample for console app.

    namespace Office365.Console  
     {  
         using Microsoft.SharePoint.Client;  
         using Newtonsoft.Json;  
         using System;  
         using System.Collections.Concurrent;  
         using System.IO;  
         using System.Net;  
         using System.Net.Http;  
       
         class Program  
         {  
             private static ConcurrentDictionary<Uri, RESTFormDigest> FormDigests { get; set; }  
       
             static void Main(string[] args)  
             {  
                 string username, password;  
    		 Console.Write("Enter Username")  
                 username = Console.Readline();  
                 Console.Write("Enter Password")  
                 password = Console.Readline();  
                 Uploadfiles(username, password);  
                 System.Console.WriteLine("Completed");  
                 System.Console.ReadLine();  
             }  
       
             static string siteUrl = "https://sppalsmvp.sharepoint.com/sites/DeveloperSite/";           
       
             public async static void Uploadfiles(string userName, string password)  
             {  
                 try  
                 {  
                     OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();  
       
                     using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
                     {  
                         Web web = ctx.Web;  
                         ctx.Load(web);  
                         ctx.Load(web.Lists);  
                         ctx.ExecuteQueryRetry();  
                         List list = web.Lists.GetByTitle("D1");  
                         ctx.Load(list);  
                         ctx.ExecuteQueryRetry();  
                         Folder folder = list.RootFolder.EnsureFolder("Folder1");  
                         ctx.Load(folder);  
                         ctx.ExecuteQueryRetry();  
       
                         Folder folderToUpload = web.GetFolderByServerRelativeUrl(folder.ServerRelativeUrl);  
                         folderToUpload.UploadFile("LargeFile.txt", "D:\LargeFile.txt", true);  
                         folderToUpload.Update();  
                         ctx.Load(folder);  
                         ctx.ExecuteQueryRetry();  
                         folderToUpload.EnsureProperty(f => f.ServerRelativeUrl);  
                         var serverRelativeUrl = folderToUpload.ServerRelativeUrl.TrimEnd('/') + '/' + "LargeFile.txt";  
       
       
       
                     }  
                 }  
                 catch (Exception ex) {  
                     System.Console.WriteLine("Exception occurred : " + ex.Message);  
                     System.Console.ReadLine();  
                 }  
             }  
       
       
               
         }  
     }  
    

    https://www.sharepointpals.com/post/upload-file-to-sharepoint-office-365-programmatically-using-c-csom-pnp/

    2 people found this answer helpful.
    0 comments No comments

  2. RaytheonXie_MSFT 31,226 Reputation points Microsoft Vendor
    2021-12-16T02:22:38.857+00:00

    Hi @Babu ,
    Sample code for your reference:

    string userName = "xxx@xxxx.onmicrosoft.com";  
    string password = "xxx";  
    var securePassword = new SecureString();  
    foreach (char c in password)  
    {  
        securePassword.AppendChar(c);  
    }  
    using (var clientContext = new ClientContext("https://xxx.sharepoint.com/sites/test"))  
    {  
        clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);  
        Web web = clientContext.Web;  
        clientContext.Load(web, a => a.ServerRelativeUrl);  
        clientContext.ExecuteQuery();  
        List documentsList = clientContext.Web.Lists.GetByTitle("Contact");  
      
        var fileCreationInformation = new FileCreationInformation();  
        //Assign to content byte[] i.e. documentStream  
      
        fileCreationInformation.Content = System.IO.File.ReadAllBytes(@"D:\document.pdf");  
        //Allow owerwrite of document  
      
        fileCreationInformation.Overwrite = true;  
        //Upload URL  
      
        fileCreationInformation.Url = "https://testlz.sharepoint.com/sites/jerrydev/" + "Contact/demo" + "/document.pdf";  
      
        Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);  
      
        //Update the metadata for a field having name "DocType"  
        uploadFile.ListItemAllFields["Title"] = "UploadedviaCSOM";  
      
        uploadFile.ListItemAllFields.Update();  
        clientContext.ExecuteQuery();  
      
    }  
    

    Please make sure the file path is valid in System.IO.File.ReadAllBytes method and the fileCreateionInformation.Url should be the complete library file url not the relative url.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.