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


2 Answers 2


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;
  • very nice utility function Commented Jun 16, 2014 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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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