Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I trying to add programmatically the detested Folder. I do the following

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

but I get the following error on Update: Invalid item data -- missing FileRef

Why? Tnx

share|improve this question
add comment

1 Answer

up vote 5 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(
          dl.RootFolder.ServerRelativeUrl, 
          SPFileSystemObjectType.Folder, 
          "Test"
          );

     folder.Update();
}

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);
        Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(subFolderPath));
        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]);
                newFolderItem.SystemUpdate();

                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
add comment

Your Answer

 
discard

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.