1

I am trying to create a list of all the folders that exist within the root folder. From there I want to implement the creation of a folder if the folder does not already exist.

Is there a way to create a folder on the root path if the folder does not exist?

Current Code

public void ListFolders_CreateIfNone(string listTitle, string folderName)
    {
        using (var clientContext = OpenConnectionToSharePoint())
        {
            var list = clientContext.Web.Lists.GetByTitle(listTitle);
            clientContext.Load(list);
            clientContext.Load(list.RootFolder);
            clientContext.Load(list.RootFolder.Folders);


            clientContext.ExecuteQuery();

            var folderCollection = list.RootFolder.Folders;

            foreach (var folder in folderCollection)
            {
                if (folder.Name == folderName)
                {
                    clientContext.Load(folder.Files);
                    clientContext.ExecuteQuery();
                }
                else
                {
                    var i = folderCollection.Add(folder.Name);
                    i.Update();
                    break;
                }

            }

        }
    }
1

Solved #

 public void ListFolders_CreateIfNone(string listTitle, string folderName)
    {
        using (var clientContext = OpenConnectionToSharePoint())
        {
            var list = clientContext.Web.Lists.GetByTitle(listTitle);
            list.EnableFolderCreation = true;

            clientContext.Load(list);
            clientContext.Load(list.RootFolder);
            clientContext.Load(list.RootFolder.Folders);

            clientContext.ExecuteQuery();

            var folderCollection = list.RootFolder.Folders;

            foreach (var folder in folderCollection)
            {

                if (folder.Name == folderName)
                {
                    clientContext.Load(folder.Files);
                    clientContext.ExecuteQuery();
                }
                else
                {
                    var itemCreateInfo = new ListItemCreationInformation
                    {
                        UnderlyingObjectType = FileSystemObjectType.Folder,
                        LeafName = folderName
                    };

                    var newItem = list.AddItem(itemCreateInfo);
                    newItem["Title"] = folderName;
                    newItem.Update();
                    clientContext.Credentials = NetworkCredential();
                    clientContext.ExecuteQuery();

                    break;
                }

            }

        }
    }

This is the method that I created to check to see if a folder already existed ... if there was no folder I would then create the new one.

0

If you mean the .RootFolder, then I will go with NO. That folder should get created when the library was created. If you mean any other folder, then your code should work:

var i = folderCollection.Add(folder.Name);  // is this the path to folder? the server relative url?
i.Update();

//try rather
clientContext.Load(i)
clientContext.ExecuteQuery()

If you are getting any errors, post them so we can troubleshoot.

0
var list = ctx.GetListByTitle("MyList);
list.EnableFolderCreation = true;
list.RootFolder.EnsureFolder("ExisingFolder1").EnsureFolder("NewSubFolderToFolder1");
ctx.ExecuteQuery();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.