Frage

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.

War es hilfreich?

Lösung 2

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

Andere Tipps

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top