SharePoint 2010, c# server object model, VS2012
Long story short, I'm trying to fetch a folder of a document library, create a new folder in it from an array of names, then fetch the new folder via its name and create more folders within it. This is what it looks like:
SPListItem newFolder = list.AddItem(parentFolder.Url, SPFileSystemObjectType.Folder, newFolderName); newFolder.Update(); SPFolder newFolderObj = parentFolder.SubFolders[newFolderName]; // Made an oops here in the example, it's fixed now sorry! SPListItem newSubFolder = list.AddItem(newFolderObj.Url, SPFileSystemObjectType.Folder, newSubFolderName); newSubFolder.Update();
The problem is that when fetching the new folder using the name (it can be an int index or string display name) it will only accept a string literal, not a variable. As in, using
parentFolder.SubFolders["New Folder Literal Name"] works but
parentFolder.SubFolders[newFolderName] (which shows up as being identical during debugging) causes a "Value does not fall within the expected range" exception.
I've posted another take on the question here from a c# perspective (trying to turn the string variable into a string literal), but I'm guessing it's a quirk of SP causing it to begin with. Does anyone know a way around this without using the int index? Is it possible to make it accept a string variable?
Update: I've narrowed the problem down some more, it doesn't have to do with the indexing argument so much as the collection itself. For example, it locates a specific folder "TopFolder" and returns its subfolders as a collection, "Folder1", "Folder2" and "Folder3" with a total count of 3 items. It sees that "Folder4" doesn't exist yet, so it creates it and updates it to commit the changes. Then it fetches a new collection from "TopFolder" against which it indexes the name of the new folder "Folder4". But this new collection is still only returning 3 items, not including the new one. I tried using a
list.Update() before fetching the new collection, but that didn't make a difference. It also does not matter whether you index with a string or an int, the collection still only has 3 items.