Hi @MYA ,
Suggest to use SharePoint CSOM library to upload files into SharePoint Library folder if you are using C# code, here is a code snippet for your reference:
using System;
using System.IO;
using SP = Microsoft.SharePoint.Client;
namespace CSOM1
{
class ReadItem
{
static void Main(string[] args)
{
string SiteUrl = "http://sp2016/sites/dev/";
string DocumentLibrary = "UploadLibrary";
string FileName = @"C:\testupload.zip";
string CustomerFolder = "1564_dsfgsst";
string UserName = "TheUserName";
string Password = "ThePassword";
string Domain = "Contoso2016";
UploadFileToSharePoint(SiteUrl, DocumentLibrary, CustomerFolder, FileName, UserName, Password, Domain);
}
private static void UploadFileToSharePoint(string SiteUrl, string DocLibrary, string ClientSubFolder, string FileName, string Login, string Password, string Domain)
{
try
{
using (SP.ClientContext CContext = new SP.ClientContext(SiteUrl))
{
CContext.Credentials = new System.Net.NetworkCredential(Login, Password,Domain);
SP.Web web = CContext.Web;
SP.FileCreationInformation newFile = new SP.FileCreationInformation();
byte[] FileContent = System.IO.File.ReadAllBytes(FileName);
newFile.ContentStream = new MemoryStream(FileContent);
newFile.Url = Path.GetFileName(FileName);
SP.List DocumentLibrary = web.Lists.GetByTitle(DocLibrary);
//SP.Folder folder = DocumentLibrary.RootFolder.Folders.GetByUrl(ClientSubFolder);
SP.Folder Clientfolder = DocumentLibrary.RootFolder.Folders.Add(ClientSubFolder);
Clientfolder.Update();
SP.File uploadFile = Clientfolder.Files.Add(newFile);
CContext.Load(DocumentLibrary);
CContext.Load(uploadFile);
CContext.ExecuteQuery();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("The File has been uploaded" + Environment.NewLine + "FileUrl -->" + SiteUrl + "/" + DocLibrary + "/" + ClientSubFolder + "/" + Path.GetFileName(FileName));
}
#endregion
}
catch (Exception exp)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(exp.Message + Environment.NewLine + exp.StackTrace);
}
finally
{
Console.ReadLine();
}
}
}
}
Thanks
Best Regards
If an Answer is helpful, please click "Accept Answer" and upvote it.
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.