Upload File in SharePoint Document Library using CSOM

Upload File in SharePoint Document Library using CSOM

Upload File in SharePoint Document Library using CSOM. In this post, you will Upload File in SharePoint Document Library using CSOM. SharePoint Document library is to upload any type of document and download it by CSOM. We can also upload and download SharePoint Document Library by C# and Rest API etc.How to download Document in SharePoint Document Library Using CSOM.[AdSense-A]

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using PRCLNT = Microsoft.ProjectServer.Client;

namespace UpdateTaskLevelCustomField_Using_CSOM
{
    class Program
    {

        static string UserName = "UserName";
        static string Passwords = "Password";
        public static string OnlinePWA = "PWAPath";
        static void Main(string[] args)
        {
            using (PRCLNT.ProjectContext ProjectCont = new PRCLNT.ProjectContext(OnlinePWA))//PWA Url
            {
                SecureString passWord3 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord3.AppendChar(c);
                ProjectCont.Credentials = new SharePointOnlineCredentials(UserName, passWord3);
                ProjectCont.Load(ProjectCont.Web);
                ProjectCont.ExecuteQuery();
                using (var fs = new FileStream("C:\\Users\\admin\\Desktop\\DataMigrationLog.txt", FileMode.Open))
                {
                    var fi = new FileInfo("C:\\Users\\admin\\Desktop\\DataMigrationLog.txt");
                    var list = ProjectCont.Web.Lists.GetByTitle("DocumentLib");
                    ProjectCont.Load(list.RootFolder);
                    ProjectCont.ExecuteQuery();
                    var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);

                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ProjectCont, fileUrl, fs, true);
                }
            }
        }
    }
}

 


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!