0

I'm working on an application that interfaces with SharePoint 2010, and one of the SP objects (SPFolder.Subfolders[index] if you're curious) requires a string to index the items it returns. The problem is that I need to pass it a string variable, but it only accepts literals. Is there a way to convert the variable to a literal for this purpose? If the question isn't clear enough, see the illustration below:

SPFolder folder = rootFolder.SubFolders["Literal folder name"];  // This properly searches the collection (which has already been defined) and returns the folder with the name specified by the literal.

string newFolder = "Literal folder name";  // In this case I'm trying to pass this string as the index.
SPFolder folder = rootFolder.SubFolders[newFolder];  // This is where it throws a "Value does not fall within the expected range" exception when the string is referenced.

If context helps, the application is taking an array of folder names and creating them at a specific level in a library, then looping through the new folders to build a complex folder structure within them. To do this, I have to create a folder, fetch its url, then create the rest of the folders based on that url. Fetching the newly created folder is where I'm running into trouble, because indexing the parent folder with the string I just used to create it is where it gets mad.

2
  • can you do string newFolder = @"New folder name"; ? Commented Jul 3, 2013 at 15:16
  • Just gave it a shot, no luck :/ Commented Jul 3, 2013 at 15:22

3 Answers 3

2

These are the same:

 SPFolder folder = rootFolder.SubFolders["Literal folder name"];

 string folderName = "Literal folder name";
 SPFolder folder = rootFolder.SubFolders[folderName];

In your example, the strings have different values, which will result in different results.

Sign up to request clarification or add additional context in comments.

3 Comments

In the actual application, it makes no difference. The folder object is created with a string, then the same string is passed to the index. The example I gave above is not literally what is being passed (no pun intended). I have corrected the example to clear that up.
@thanby I suspect that the strings are, in fact, different. Try debugging by checking if "Literal folder name" == folderName is true or false.
Already did. Trace shows every instance of the newFolder variable being consistent, even the one where it is failing. The same variable is used in a few different spots during each iteration of the loop, and it's only failing here. I'm pretty sure it's SharePoint's fault for being picky. If I had to take a WILD guess, it's not actually sending the literal to the object class, it's sending the reference, and the class isn't willing to look up the reference. So the trace is showing the correct literal because it looks it up, while the class doesn't. Does that make sense?
1

the example from the docs shows access via integer index where you can check the name. Not the most elegant but would work

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfoldercollection(v=office.14).aspx

2 Comments

That should theoretically work, but requires a lot of extra code to return the int index so if I can avoid it that would be nice. If indeed it is the only solution however, I'll go with it. Thanks for the idea!
yeah you could obviously shortcut it with a LINQ select helper method.. which would be way faster than the for loop style. Not ideal, but functional hopefully. MyCollection.Select(m => m.Name == myNameVariable).FirstOrDefault()
0

Have you tried using verbatim string literals?

var testString = @"This is a verbatim string literal";

MSDN also has a good explanation of the differences between string literals and verbatim string literals.

1 Comment

I did actually try this. Unfortunately it produced the same results. Thanks for the suggestion, though!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.