0

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.

1
  • Your edit is now asking a completely different question than the original post. Please post it as a new question. Commented Jul 9, 2013 at 14:40

3 Answers 3

2

Try using the SPListItem.Title property:

SPListItem newFolder = list.AddItem(parentFolder.Url, SPFileSystemObjectType.Folder, newFolderName);
newFolder.Update();
SPFolder newFolderObj = parentFolder.SubFolders[newFolder.Title];
SPListItem newSubFolder = list.AddItem(newFolderObj.Url, SPFileSystemObjectType.Folder, newSubFolderName);
newSubFolder.Update();

The value isn't within the expected range because newFolder is an SPListItem rather than a string. You could also try using the SPListItem.Name property if Title doesn't work.

3
  • Good catch, unfortunately that was my mistake in writing the example. I was actually trying to pass the newFolderName which is a string that was used to name the folder when it was created earlier. The results are unchanged. Just to experiment, I tried passing the .Title and .Name properties of the SPListItem but it is throwing an error saying "no constructors defined". I even tried them both .ToString() for good measure, no love.
    – thanby
    Commented Jul 9, 2013 at 13:36
  • What are you trying to construct here? You aren't trying to construct an object... you could try using this: msdn.microsoft.com/en-us/library/ms461547.aspx but your original question was how to use the folder name as a string variable for your indexer, which should work without a hitch. If you're running into an additional problem, you should post a new question. Commented Jul 9, 2013 at 13:43
  • It turns out that the problem is with the SPFolderCollection, not the indexing argument. I'll also give your suggestion a shot to see if it helps.
    – thanby
    Commented Jul 9, 2013 at 14:33
1

I might be blind, but to me it looks like you are passing in a List Item in the .SubFolders[newFolder] instead of the name of the folder.

7
  • You aren't blind. I was already typing that up as the answer. Commented Jul 3, 2013 at 17:33
  • @rjcup3 We must have hawk eyes! Commented Jul 3, 2013 at 17:36
  • 1
    Staring at umpteen thousands of lines of code for the majority of your waking hours for months on end can do that to a man lol. Commented Jul 3, 2013 at 17:39
  • I apologize for the confusion, I was actually trying to pass the newFolderName item which is a string that was defined earlier to name the folder. The example has been corrected to reflect this.
    – thanby
    Commented Jul 9, 2013 at 13:38
  • 1
    Tried this? msdn.microsoft.com/en-us/library/… Commented Jul 9, 2013 at 13:57
0

Per rjcup3's suggestion, I have posted a new question here.

The answer is not that the argument was invalid, but that the new SPFolderCollection was not returning an accurate list that included the new folder. My new question referenced above deals with this problem.

Thanks to everyone for your suggestions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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