SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

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

I am trying to add programmatically the desired Folder. I do the following

foreach (SPListItem reparto in reparti.Items)
  var folder = dl.Items.Add(dl.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
  folder["Title"] = "Test";

But I get the following error on update: Invalid item data -- missing FileRef


share|improve this question
up vote 10 down vote accepted

If you want to create a folder, you have to specify its name in the leafName parameter. change your code to :

foreach (SPListItem reparto in reparti.Items)
     var folder = dl.Items.Add(


If it can help, here a small utility method I wrote to create a full folder hierarchy :

/// <summary>
/// Provide utilities methods related to folder management
/// </summary>
public static class SPFolderUtilities
    /// <summary>
    /// Ensures the specified folder exists.
    /// </summary>
    /// <param name="list">The list where to create the folder.</param>
    /// <param name="subFolderPath">The sub folder path.</param>
    /// <returns>
    /// The existing <see cref="SPFolder"/> or a newly created if it did not exists.
    /// </returns>
    public static SPFolder EnsureFolder(SPList list, string subFolderPath)
        Contract.Requires<ArgumentNullException>(list != null);
        if (!IO.SPPathUtilities.IsFileOrFolderNameValid(subFolderPath, true))
            throw new ArgumentException("Invalid characters in the file or folder name", "subFolderPath");

        var folderPaths = subFolderPath.Split('/');

        var currentFolder = list.RootFolder;
        for (int i = 0; i < folderPaths.Length; i++)
            currentFolder = list.ParentWeb.GetFolder(currentFolder.Url); // hacky refresh
            var subFolder = currentFolder.SubFolders.Cast<SPFolder>().FirstOrDefault(f => string.Compare(f.Name, folderPaths[i], true) == 0);
            if (subFolder == null)
                var newFolderItem = list.Items.Add(currentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderPaths[i]);

                subFolder = newFolderItem.Folder;
            currentFolder = subFolder;
        return currentFolder;
share|improve this answer
That little utility is a thing of beauty. Thanks! – Mahendran Govender Apr 17 '13 at 14:31
very nice utility function – Kannan Jun 16 '14 at 3:16

Yet another version how to create a folder including nested folders using SharePoint SSOM.

How to create nested Folder using SharePoint SSOM

internal static class SPFolderExtensions
    /// <summary>
    /// Ensure SPFolder
    /// </summary>
    /// <param name="web"></param>
    /// <param name="listTitle"></param>
    /// <param name="folderUrl"></param>
    /// <returns></returns>
    public static SPFolder CreateFolder(this SPWeb web, string listTitle, string folderUrl)
        if (string.IsNullOrEmpty(folderUrl))
            throw new ArgumentNullException("folderUrl");
        var list = web.Lists.TryGetList(listTitle);
        return CreateFolderInternal(list, list.RootFolder, folderUrl);

    private static SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)
        var folderNames = folderUrl.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
        var folderName = folderNames[0];

        var curFolder =
                            f =>
                            System.String.Compare(f.Name, folderName, System.StringComparison.OrdinalIgnoreCase) ==
        if (curFolder == null)
            var folderItem = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,
            curFolder = folderItem.Folder;

        if (folderNames.Length > 1)
            var subFolderUrl = string.Join("/", folderNames, 1, folderNames.Length - 1);
            return CreateFolderInternal(list, curFolder, subFolderUrl);
        return curFolder;

Key points:

  • Ability to create a nested folder(s)
  • Existing folders will not be affected


The below example demonstrates how to create the following folder structure under Documents library:

   Orders A
      Orders A1


var folder = web.CreateFolder("Documents", "Orders3/Orders A/Orders A1");
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.