PnP Core Component - Upload a File to a Specific Folder in SharePoint 2016

Please refer to the Introduction to PnP Core Component and OfficeDevPnP.Core for more details. I have created a console application and added SharePointPnPCore2016 NuGet package for the SharePoint 2016 version.
 
Code Snippet
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using OfficeDevPnP.Core;  
  8.   
  9. namespace SP2016PnPCoreComponentDemo  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             // Input Parameters  
  16.             string siteUrl = "http://c7395723754/";  
  17.             string userName = "administrator";  
  18.             string password = "xxxxxxxxxxx";  
  19.             string domain = "AD2012";  
  20.             string serverRelativeURL = "/Shared Documents/Folder";  
  21.             string localPath = @"C:\Users\Administrator.AD2012\Documents\Folder\Document.rtf";  
  22.             string fileName = "UploadedDocument";  
  23.   
  24.             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();  
  25.   
  26.             try  
  27.             {  
  28.                 // Get the client context  
  29.                 using (var ctx = authMgr.GetNetworkCredentialAuthenticatedContext(siteUrl, userName, password, domain))  
  30.                 {  
  31.                     // Upload a file to a specific folder  
  32.                     Folder folder = ctx.Web.GetFolderByServerRelativeUrl(serverRelativeURL);  
  33.                     folder.UploadFile(fileName, localPath, true);  
  34.                 }  
  35.             }  
  36.   
  37.             catch (Exception ex)  
  38.             {  
  39.                 Console.WriteLine("Error Message: " + ex.Message);  
  40.             }  
  41.         }  
  42.     }  
  43. }
Ebook Download
View all
Learn
View all