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 have a sharepoint list (Not Library), where I am programatically adding in a folder:

SPList list = web.Lists[ListID];

SPFolder parent = list.RootFolder;
SPFolder child = parent.SubFolders.Add(FolderName);
parent.Update();
child.Update();

This adds in a folder to the library, to which I then add in a listitem:

SPListItem item = list.AddItem(child.Url, SPFileSystemObjectType.File);
item[DefaultSettings.S_COL_Title] = "New title";

item.Update();

The net result of this is that the item is added with no errors. But when I go to my library I do not see the item.

If I go into the view and change the option under Foldering from "Show items inside folders" to "Show all items without folders", my newly added item is then visible. Also if I manually type in the url to the folder, I can see the item.

So I know the item is there, and that it is in its folder.

I want the folder to display in the view, and if I click on the folder to then see the item.

How can I achieve this?

share|improve this question
    
What are you naming your folder? Folders starting or ending with underscore "_" will be hidden from the UI –  Robert Lindgren Apr 17 '13 at 9:48
    
Thats an interesting point Robert, I'm calling it "HELLO_WORLD", so there is an underscore in the name. Will this have an effect? –  Mahendran Govender Apr 17 '13 at 9:55
    
I just tried it without the underscore and it still has the same issue. :'( –  Mahendran Govender Apr 17 '13 at 9:58
    
No it should not have any effect if not in beginning or end. –  Robert Lindgren Apr 17 '13 at 11:16
add comment

1 Answer

The answer was right here on sharepoint.stackexchange.com: When I create a folder porgrammatically get the error "Invalid item data -- missing FileRef"

For those that don't want to click a link :D here's the relevant code:

/// <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;
}

@Steve B: You sir are a legend.

share|improve this answer
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.