To get you started, here is a way how to create a folder in a DocumentLibrary:
internal static void CreatePhysicalFolderStructure(ClientContext context)
{
//get list of all folders from the zip file
var allFolders = Globals.FolderStructure.Descendants().Where(n => n.FolderLevel > 1).ToList();
//get reference to the list
List list = context.Web.Lists.GetByTitle(Globals.DocLibName);
CreateFolder(list, "Excel Files");
CreateFolder(list, "Excel Files/2013");
CreateFolder(list, "Excel Files/2013/PDF");
'executes query
context.ExecuteQuery();
}
internal static void CreateFolder(List list, string name)
{
var info = new ListItemCreationInformation
{
UnderlyingObjectType = FileSystemObjectType.Folder,
LeafName = name
};
var newItem = list.AddItem(info);
newItem["Title"] = name;
newItem.Update();
}
Please note that you have to create folder in order. If you would try to create directly the very last folder Excel Files/2013/PDF"
, the method would fail.
Also beware that you cannot create already existing folder. Here is the method, which will return list of all folders that are already existing (you will have to modify it a bit).
private static IEnumerable<string> GetListFolderStructure(ClientContext context, List list)
{
var results = new List<string>();
//query to recursively loop through all folders
var qry = new CamlQuery();
qry.ViewXml = "<View Scope='RecursiveAll'>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='FSObjType' />" +
"<Value Type='Integer'>1</Value>" +
"</Eq>" +
"</Where>" +
"</Query>" +
"</View>";
var allItems = list.GetItems(qry);
context.Load(allItems, n => n.Include(x => x.DisplayName, x => x.Folder));
context.ExecuteQuery();
//retrieve default path that we will trim from our URL result
//e.g. Result we get from query is: /sites/RobertTest/MainFolder/TestFolder/Word Files
//but we want to only return: TestFolder/Word Files
string sDefaultPath = new Uri(Globals.SiteURL).AbsolutePath + "/" + Globals.DocLibName + "/";
//add results to our List
foreach (var item in allItems)
{
results.Add(item.Folder.ServerRelativeUrl.Replace(sDefaultPath, ""));
}
return results;
}
Hope this would help.