How to add Folder to List or Library?

Mar 1, 2014 at 10:49 PM
I would like to see an example of how a new Folder can be created in a List/Library or both if different. I looked for examples but could not find any.
Coordinator
Apr 28, 2014 at 11:05 AM
Hi lawalker!

You can use "Folder" property inherited from "Item" class.

By setting "Folder", the path ensures automatically and new created items appears in this folder.
Its same behavior for lists and libraries.

You can see example in test project:
[Test]
        public void Add_Adds_Item_To_Folder_Test()
        {
            IQueryList<Item> list = null;
            try
            {
                list = _queryWeb.Create<Item>("Add_Adds_Item_To_Folder_Test");
                var itm = new Item
                {
                    Title = "Add_Adds_Item_To_Folder_Test",
                    Folder = "Folder1/Folder2/Folder3",
                };
                list.Add(itm);

                var item = list.Items(new CamlQuery()
                    .Recursive()  
                    .Query(Q.Where(Q.Eq(Q.FieldRef<Item>(d => d.Title), Q.Value(itm.Title)))))
                    .FirstOrDefault();

                Assert.IsNotNull(item);
                Assert.That(item.Id, Is.EqualTo(itm.Id));
                Assert.That(item.Folder, Is.EqualTo(itm.Folder));
                Assert.That(item.Title, Is.EqualTo(itm.Title));
            }
            finally
            {
                if (list != null)
                {
                    list.DeleteList(false);
                }
            }
        }
Apr 28, 2014 at 3:20 PM
Thank you for the example, it is useful and beneficial to the online documentation. The example code does create the folder and adds the newly created item to it however I was looking for a way to create an empty folder with no Item in it. My use case was/is to create a set of empty folders in preparation for the library's use. My original post was not as clear as that, my apologies. Until I hear back, I will see if I can add a faux item to create the folder then delete the faux item and see if the folder remains. A little more work but may be the workaround for now. Thank you in advance for your time.