public bool AddNewFolder(string listUrl, string listName, string docSetContentTypeName, string newDocSetName) { bool res = false; try { string siteUrl = SLMConstant.Ecm.SiteUrl; //http://ecm/dept/public string domain = SLMConstant.Ecm.Domain; string userName = SLMConstant.Ecm.Username; string password = SLMConstant.Ecm.Password; //log.Info("Connect ECM"); using (ClientContext clientContext = new ToEcm.ClientContext(siteUrl) { Credentials = new NetworkCredential(userName, password, domain) }) { // get the web Web web = clientContext.Web; List list = clientContext.Web.Lists.GetByTitle(listName); clientContext.Load(clientContext.Site); // load the folder Folder f = web.GetFolderByServerRelativeUrl(listUrl + newDocSetName); log.Info(listUrl + newDocSetName); clientContext.Load(f); bool alreadyExists = false; // check if the folder exists try { log.Info("Before clientContext.ExecuteQuery()"); clientContext.ExecuteQuery(); log.Info("After clientContext.ExecuteQuery()"); alreadyExists = true; res = true; } catch (Exception ee) { //ถ้าเข้า catch แสดงว่า ไม่พบ folder ที่โหลดเข้ามา log.Error("Error Check Folder : " + ee.Message); } // create folder if (!alreadyExists) { log.Info("Create Folder: Start"); // folder doesn't exists so create it ContentTypeCollection listContentTypes = list.ContentTypes; clientContext.Load(listContentTypes, types => types.Include (type => type.Id, type => type.Name, type => type.Parent)); var result = clientContext.LoadQuery(listContentTypes.Where (c => c.Name == docSetContentTypeName)); clientContext.ExecuteQuery(); ContentType targetDocumentSetContentType = result.FirstOrDefault(); // create the item ListItemCreationInformation newItemInfo = new ListItemCreationInformation(); newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder; newItemInfo.LeafName = newDocSetName; Microsoft.SharePoint.Client.ListItem newListItem = list.AddItem(newItemInfo); // set title and content type newListItem["ContentTypeId"] = targetDocumentSetContentType.Id.ToString(); newListItem["Title"] = newDocSetName; newListItem.Update(); res = true; // execute it clientContext.Load(list); clientContext.ExecuteQuery(); log.Info("Create Folder: Finished"); } } } catch (Exception ex) { log.Error("Error AddNewFolder : " + ex.Message); throw ex; } return(res); }