2010 Enterprise, VS2012, c# server-side
Here's the situation: I have a method that gets an SPFolderCollection object from a document library. It then loops through the list checking for a specific folder name. If the name is not found, it creates a folder with that name and updates it. Then it fetches a new SPFolderCollection that should contain the new folder, in order to select it and create more folders within it.
The problem is that the new SPFolderCollection only returns the folders that already existed, and does not include the newly created/updated folder. I've tried doing a
list.Update() with no luck either.
So the question is, how do I fetch a new SPFolderCollection that includes the newly created folder?
My original question is here which ended up taking a different direction than expected (leading to this question). Here are some updated code samples with explanations:
SPListItem newFolder = list.AddItem(parentFolder.Url, SPFileSystemObjectType.Folder, newFolderName); // Creating a new folder under an existing one newFolder.Update(); // Committing changes SPFolder newFolderObj = parentFolder.SubFolders[newFolderName]; // Setting the newly created folder as an object so I can create more folders within it. // The parentFolder.Subfolders[index] is the collection that doesn't seem to be updating. // Trace shows the index as being the correct folder that was just created (the name of which was specified earlier in the application) but the new collection does not include it for some reason.
I have also tried creating the collection independently like this:
SPListItem newFolder = list.AddItem(parentFolder.Url, SPFileSystemObjectType.Folder, newFolderName); newFolder.Update(); SPFolderCollection newCollection = parentFolder.SubFolders; // This collection is created completely from scratch so no previous instances could be interfering. SPFolder newFolderObj = newCollection[newFolderName]; // However the new collection still does not contain the newly created folder, so indexing like this fails.