5

I have a sharepoint list (Not Library), where I am programmatically 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?

  • 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
0

The answer was right here on sharepoint.stackexchange.com: When I create a folder programmatically 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.

0

Please use the following code snippet to create a folder in custom list and add new item in the new folder.

 
 using (SPSite site = new SPSite("http://sp2013/sites/team/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists.TryGetList("CustomList");
        SPListItem folder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
        folder["Title"] = "New Folder";
        folder.Update();
        list.Update();

string folderUrl = folder.Folder.ServerRelativeUrl; SPListItem item = list.AddItem(folderUrl, SPFileSystemObjectType.File); item["Title"] = "New title"; item.Update(); }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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