public SP.Folder NewFolder(SPList list, string folderName, string currentDir) { if (!IsFolderValid(list, folderName, currentDir)) { return(null); } using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl))) { SP.List splist = clientContext.ToList(list.Id); SP.Folder parentFolder = clientContext.Web.GetFolderByServerRelativeUrl(currentDir); clientContext.Load(parentFolder); // add a new folder SP.Folder newFolder = parentFolder.Folders.Add(folderName); parentFolder.Update(); clientContext.Load(newFolder); try { clientContext.ExecuteQuery(); } catch (Exception ex) { EventLogs.Warn(String.Format("An exception of type {0} occurred while creating a new folder with a name '{1}' for a directory '{2}'. The exception message is: {3}", ex.GetType().Name, folderName, currentDir, ex.Message), "SharePointClient", 778, CSContext.Current.SettingsID); return(null); } cacheService.RemoveByTags(new[] { GetTag(list.Id) }, CacheScope.Context | CacheScope.Process); return(newFolder); } }
internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation) { string UserName = "******"; string Password = "******"; SecureString passWord = new SecureString(); foreach (var c in "iluminadoAgosto27") { passWord.AppendChar(c); } ClientContext ctx = new ClientContext(siteUrl); ctx.Credentials = new SharePointOnlineCredentials(UserName, passWord); Web site = ctx.Web; FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files; //Get the required RootFolder string barRootFolderRelativeUrl = "Shared Documents/foo/bar"; Folder barFolder = site.GetFolderByServerRelativeUrl(folderPath); ctx.ExecuteQuery(); //Create new subFolder to load files into string newFolderName = "baseName"; barFolder.Folders.Add(newFolderName); barFolder.Update(); ctx.ExecuteQuery(); }
public SP.Folder NewFolder(string url, Guid listId, string folderName, string currentDir) { if (!IsFolderValid(url, listId, folderName, currentDir)) { return(null); } using (var clientContext = new SPContext(url, credentials.Get(url))) { SP.Folder parentFolder = clientContext.Web.GetFolderByServerRelativeUrl(currentDir); clientContext.Load(parentFolder); // add a new folder SP.Folder newFolder = parentFolder.Folders.Add(folderName); parentFolder.Update(); clientContext.Load(newFolder); try { clientContext.ExecuteQuery(); } catch (Exception ex) { SPLog.FileNotFound(ex, "Error occurred while creating a new folder with a name '{0}' for a directory '{1}'.", folderName, currentDir); return(null); } CacheService.RemoveByTags(new[] { Documents.Tag(listId), Folders.Tag(listId) }, CacheScope.All); return(newFolder); } }
private static void UploadFileToSharePoint(string SiteUrl, string DocLibrary, string ClientSubFolder, string FileName, string Login, string Password) { try { #region ConnectToSharePoint var securePassword = new SecureString(); foreach (char c in Password) { securePassword.AppendChar(c); } var onlineCredentials = new SharePointOnlineCredentials(Login, securePassword); #endregion #region Insert the data using (ClientContext CContext = new ClientContext(SiteUrl)) { CContext.Credentials = onlineCredentials; Web web = CContext.Web; FileCreationInformation newFile = new FileCreationInformation(); byte[] FileContent = System.IO.File.ReadAllBytes(FileName); newFile.ContentStream = new MemoryStream(FileContent); newFile.Url = Path.GetFileName(FileName); List DocumentLibrary = web.Lists.GetByTitle(DocLibrary); Folder Clientfolder = null; if (ClientSubFolder == "") { Clientfolder = DocumentLibrary.RootFolder; } else { Clientfolder = DocumentLibrary.RootFolder.Folders.Add(ClientSubFolder); Clientfolder.Update(); } Microsoft.SharePoint.Client.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(); } }
/// <summary> /// Gets and sets default content type of the list. /// </summary> /// <param name="requestObject">RequestObject object</param> /// <param name="clientContext">ClientContext object</param> /// <param name="url">SourceURL of the OneDrive document</param> /// <param name="contentTypeId">Content Type Id</param> /// <param name="operationType">Operation type specifies either get or set Content type operation</param> /// <param name="documentLibraryName">Name of the document library</param> /// <returns>Returns Content Type Id</returns> internal static string GetContentTypeList(RequestObject requestObject, ClientContext clientContext, string url, string contentTypeId, int operationType, string documentLibraryName) { try { using (clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(url.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture))[0]), requestObject.RefreshToken)) { List docLib = clientContext.Web.Lists.GetByTitle(documentLibraryName); clientContext.Load(docLib); clientContext.ExecuteQuery(); Microsoft.SharePoint.Client.Folder folder = docLib.RootFolder; IList <ContentTypeId> list = new List <ContentTypeId>(); clientContext.Load(folder, item => item.ContentTypeOrder, item => item.UniqueContentTypeOrder); clientContext.ExecuteQuery(); list = folder.ContentTypeOrder; ServiceConstantStrings.OperationTypes type = (ServiceConstantStrings.OperationTypes)operationType; switch (type) { case ServiceConstantStrings.OperationTypes.Update: contentTypeId = list[0].StringValue; break; case ServiceConstantStrings.OperationTypes.Checkout: int iterator = 0, index = 0; for (iterator = 0; iterator < list.Count; iterator++) { if (list[iterator].StringValue == contentTypeId) { index = iterator; break; } } ContentTypeId currentContentTypeID = list[index]; list.RemoveAt(index); list.Insert(0, currentContentTypeID); folder.UniqueContentTypeOrder = list; folder.Update(); clientContext.ExecuteQuery(); break; } } } catch (Exception exception) { Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName); } return(contentTypeId); }
private static void UploadToSharePoint2(string p, string newUrl) //p is path to file to load { string siteUrl = "https://zyght-my.sharepoint.com/personal/arley_duarte_zyght_com/_layouts/15/"; //Insert Credentials ClientContext context = new ClientContext(siteUrl); SecureString passWord = new SecureString(); foreach (var c in "iluminadoAgosto27") { passWord.AppendChar(c); } context.Credentials = new SharePointOnlineCredentials("*****@*****.**", passWord); Web site = context.Web; //Get the required RootFolder string barRootFolderRelativeUrl = "Shared Documents/foo/bar"; Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl); //Create new subFolder to load files into string newFolderName = "baseName" + DateTime.Now.ToString("yyyyMMddHHmm"); barFolder.Folders.Add(newFolderName); barFolder.Update(); //Add file to new Folder Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName); FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true }; currentRunFolder.Files.Add(newFile); currentRunFolder.Update(); context.ExecuteQuery(); //Return the URL of the new uploaded file newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p); }
/// <summary> /// This function adds a subfolder to the desiginated folder in SharePoint. /// /// Please refer to the checkForReqProps metod for required properties. /// /// </summary> /// <param name="NewFolder"></param> /// <param name="ParentPath"></param> /// <returns></returns> public String AddSPFolder(string NewFolder, string ParentPath) { String tmpRetVal = String.Empty; ClientContext ctx = null; Web tmpWeb = null; List tmpList = null; ListCollection tmpLists; FolderCollection tmpFolders = null; Microsoft.SharePoint.Client.Folder tmpFolder = null; try { ctx = new ClientContext(SPSiteURL); tmpWeb = ctx.Web; tmpLists = tmpWeb.Lists; tmpList = tmpLists.GetByTitle(SPRootPath); if (tmpList != null) { tmpFolders = tmpWeb.GetFolderByServerRelativeUrl(ParentPath).Folders; ctx.Load(tmpFolders); ctx.ExecuteQuery(); tmpFolder = tmpFolders.Add(NewFolder); tmpFolder.Update(); ctx.ExecuteQuery(); tmpRetVal = ParentPath + "/" + NewFolder; } } catch (Exception ex) { RetErrMessage = ex.Message; tmpRetVal = String.Empty; } return(tmpRetVal); }
/// <summary> /// Adds folders inside the document library /// </summary> /// <param name="clientContext">Context of client</param> /// <param name="list">List object</param> /// <param name="folderNames">Names of folder</param> /// <returns>List of folder added</returns> internal static List AddFolders(ClientContext clientContext, List list, IList <string> folderNames) { // Addition of Folders FolderCollection listFolders = list.RootFolder.Folders; Microsoft.SharePoint.Client.Folder listRootFolder = list.RootFolder; clientContext.Load(listFolders); if (0 < folderNames.Count) { foreach (string folderName in folderNames) { // Check for empty folder names if (!string.IsNullOrWhiteSpace(folderName)) { listFolders.Add(folderName); listRootFolder.Update(); } } list.Update(); clientContext.ExecuteQuery(); } return(list); }