SharePoint Client Add Folder,file to Library

public void UploadDocument(string siteURL, string documentListName,
string documentListURL, string documentName,

byte[] documentStream)
{

using (ClientContext clientContext = new ClientContext(siteURL))
{        

//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);

var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream

fileCreationInformation.Content = documentStream;
//Allow owerwrite of document

fileCreationInformation.Overwrite = true;
//Upload URL

fileCreationInformation.Url = siteURL + documentListURL + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
    fileCreationInformation);

//Update the metadata for a field having name "DocType"
uploadFile.ListItemAllFields["DocType"] = "Favourites";

uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();

}
}
        /// <summary>
        /// Sharepoint Client Create Folder by Folder name string
        /// </summary>
        public static void CreateFolder(ClientContext oContext, FolderCollection collFolder, string newSiteUrl, string folderStr, string strURL, string strFileName)
        {
            try
            {
                string[] folders = folderStr.Split('/');
                string FolderStr = string.Empty;
                foreach (string folder in folders)
                {
                    if (!string.IsNullOrEmpty(folder))
                    {
                        FolderStr += folder;
                        Folder folderNew = collFolder.Add(newSiteUrl + DocumentlistName + "/" + FolderStr);
                        oContext.Load(folderNew);
                        oContext.ExecuteQuery();
                        FolderStr += "/";
                    }
                }
            }
            catch (Exception ex)
            {
                //ログ出力
                OutputDocumentErrorLog(strFileName, strURL, string.Empty,
                            ex.ToString(), errorLogPath, errorLorName);
            }

        }
                                string newSiteUrl = ConfigurationManager.AppSettings["siteUrlNew"] ;using (ClientContext oContext = new ClientContext(newSiteUrl))
                                {
                                    oContext.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["userNameNew"], 
                                  ConfigurationManager.AppSettings["passwordNew"], ConfigurationManager.AppSettings["domainNew"]); Web web = oContext.Web; FolderCollection collFolder = web.Folders; oContext.Load(collFolder); oContext.ExecuteQuery();
                                string folderStr = folderServerRelativeUrl + listName + serverRelativeUrlOld; CreateFolder(oContext, collFolder, newSiteUrl, folderStr, strURL, strFileName); }
原文地址:https://www.cnblogs.com/wodegui/p/4885196.html