SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's the class of my folder:

class MyFolder
    private List<MyFolder> _folders;
    public List<MyFolder> Folders
            if (_folders == null)
                _folders = new List<MyFolder>();
            return _folders;
            _folders = value;
    public string Name { get; set; }
    public string ContentType { get; set; }

So right now I have a root MyFolder which has five other MyFolders inside its "Folders" list, which in turn have their own MyFolders inside their "Folders" lists and so on and so on. This gives me a nice tree which I would like to upload into sharepoint.

So i have the context and create my document library to hold the folder structure like this:

static MyFolder root = new MyFolder();
private static void CreateLibrary(SPWeb web, string libTitle)
    SPListTemplate listTemplate = web.ListTemplates["Document Library"];
    SPDocTemplate docTemplate = (from SPDocTemplate dt in web.DocTemplates where dt.Type == 100 select dt).FirstOrDefault(); // 100 - no template

    Guid guid = web.Lists.Add("MyLibrary","My folder structure", listTemplate, docTemplate);
    var library = web.Lists[guid] as SPDocumentLibrary;

    library.OnQuickLaunch = true;
    library.ContentTypesEnabled = true;


    CreateFolderStructure(); //this puts the folder structure inside 'root' object

Now I'm looking for an easy implementation for the UploadFolderStructure method. If anybody has done anything like this before, help would be appreciated.

share|improve this question
up vote 0 down vote accepted

OK I seem to have done it. Here's my method:

 foreach (var f in root.Folders)
                UploadFolder(root, l.RootFolder);

 private static void UploadFolder(MyFolder myFolder, SPFolder spFolder)
        SPFolder x = spFolder;
        if (myFolder.Name != null)
            x = spFolder.SubFolders.Add(myFolder.Name.Replace(":"," - "));
        foreach (MyFolder folder in myFolder.Folders)
            UploadFolder(folder, x);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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