0

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.
  • It seems like the code in your other post doesn't entirely reflect what you are describing here. Can you post current code? – BlueBird Jul 9 '13 at 15:35
  • Exactly how are you getting an SPFolderCollection from the document library? Via SPList.RootFolder.Subfolders or SPDocumentLibrary.RootFolder.Subfolders? – Dylan Cristy Jul 9 '13 at 15:41
  • @BlueBird I updated the question with the code you asked for. Does that help? @Dylan Cristy The collection is fetched using the parent folder's URL to set it as an object, then calling the .SubFolders method on it. – thanby Jul 9 '13 at 19:42
1

Try updating the parent folder instead of newfolder...

SPListItem newFolder = list.AddItem(parentFolder.Url, SPFileSystemObjectType.Folder, newFolderName);
parentFolder.Update();
SPFolderCollection newCollection = parentFolder.SubFolders; 
SPFolder newFolderObj = newCollection[newFolderName];
0

After looking over your code on the other question, I'd say that in addition to the list.Update(), also do either a parentFolder.Update() or parentFolder.Subfolders.UpdateParent() before trying to fetch the new SPFolderCollection and look for your new folder.

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.