none
Fatest way to check and create folder in document library within SharePoint Online (CSOM)? RRS feed

  • Question

  • I have a SharePoint Online where i can connect through my console application successfully:

        private static ClientContext GetUserContext()
        {
            var o365SecurePassword = new SecureString();
    
            foreach (char c in o365Password)
            {
                o365SecurePassword.AppendChar(c);
            }
    
            var o365Credentials = new SharePointOnlineCredentials(o365Username, o365SecurePassword);
    
            var o365Context = new ClientContext(o365SiteUrl);
            o365Context.Credentials = o365Credentials;
    
            return o365Context;
        }

    But what i need now to do is to go into my SharePoint Document Library named "doc_archive" and check if there exists a folder with name "K20170409-01". If not create a new one.

    I have tried this, but its not working:

            ClientContext context = GetUserContext();
    
            Web web = context.Web;
            Web webroot = context.Site.RootWeb;
            context.Load(web);
            context.Load(webroot);
    
            List list = webroot.GetList("doc_archive");
            context.Load(list);
    
            FolderCollection folders = list.RootFolder.Folders;
            context.Load(folders);
    
            IEnumerable<Folder> existingFolders = context.LoadQuery(
                folders.Include(
                folder => folder.Name)
            );
            context.ExecuteQuery();


    What is the fastest ways to check and create a folder within a document library in SharePoint Online via CSOM (commandline application)?


    Best regards BOG

    Sunday, April 9, 2017 4:06 PM

Answers

  • Hi,

    Here is sample code for your reference:

    using (var context = new ClientContext("https://xxx.sharepoint.com/sites/lee/"))
                {
                    string password = "pw";
                    SecureString sec_pass = new SecureString();
                    Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                    sec_pass.MakeReadOnly();
                    context.Credentials = new SharePointOnlineCredentials("user@xxx.onmicrosoft.com", sec_pass);
    
                    List list = context.Web.Lists.GetByTitle("MyDoc2");
                    FolderCollection folders = list.RootFolder.Folders;
                    context.Load(folders);                
                    context.ExecuteQuery();
    
                    var folderExists = folders.Any(x => x.Name == "K20170409-01");
                    if (!folderExists) { 
                        //create folder
                        Folder newFolder= folders.Add("K20170409-01");
                        context.Load(newFolder);
                        context.ExecuteQuery();
                    } Console.WriteLine("Done"); Console.ReadKey(); }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, April 10, 2017 2:51 AM

All replies

  • Hi,

    Here is sample code for your reference:

    using (var context = new ClientContext("https://xxx.sharepoint.com/sites/lee/"))
                {
                    string password = "pw";
                    SecureString sec_pass = new SecureString();
                    Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                    sec_pass.MakeReadOnly();
                    context.Credentials = new SharePointOnlineCredentials("user@xxx.onmicrosoft.com", sec_pass);
    
                    List list = context.Web.Lists.GetByTitle("MyDoc2");
                    FolderCollection folders = list.RootFolder.Folders;
                    context.Load(folders);                
                    context.ExecuteQuery();
    
                    var folderExists = folders.Any(x => x.Name == "K20170409-01");
                    if (!folderExists) { 
                        //create folder
                        Folder newFolder= folders.Add("K20170409-01");
                        context.Load(newFolder);
                        context.ExecuteQuery();
                    } Console.WriteLine("Done"); Console.ReadKey(); }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, April 10, 2017 2:51 AM
  • You can check folder exist or not in following ways:

     public static int IsFolderExist(ClientContext clientContext, String listTitle, String folderName)
            {
                int existingFolder = 0;
     
                Web web = clientContext.Web;
                ListCollection lists = web.Lists;
     
                List list = web.Lists.GetByTitle(listTitle);
     
                if (list != null)
                {
                    FolderCollection folders = list.RootFolder.Folders;
                    clientContext.Load(folders, fl => fl.Include(ct => ct.Name).Where(ct => ct.Name == folderName));
                    clientContext.ExecuteQuery();
                    existingFolder = folders.Count();
                }
     
                return existingFolder;
            }

    After checking folder exist or not, you can create new one


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog

    Monday, April 10, 2017 9:10 AM