Reputation: 508
In my project, i have to show all the name of items of the Library path that user give me. If no folder the library contains => no problem.
However, there are many folders, sub-folders, sub-sub-folders, sub^n-folders. I cannot loop all the folders, which get a endless loop. I don't need the code that i just need the concept of how to loop all folders.
Therefore, i need help from all of you experienced programmer.
The result should be:
item name folder name
---------------------------------------------
item1
item2
item1 subF1
item1 subF2
item1 subF2sub1
.
.
My idea:
// path of library and folder is URL. For example, http://example.com/libraryName/subF2/subF2sub1/item1
//if i get all folders'path, i can then get the file name by those paths.
// i won't paste my code here because there are >100 lines.
void checkFolderExist(libraryPathByUser)
{
if("folderInLibrary" != nil)
{
foreach (var folder in library)
{
string folderPath = getFolderPath(folder);
strList.Add(folderPath);
// so, how about sub-folder in the folder?
}
}
}
Upvotes: 0
Views: 2955
Reputation: 31
Following code will recursively search all the artifacts and store in listItem object. It is using csom though.
ClientContext clientContext = new ClientContext("http://Servername/");
List sharedDocumentsList = clientContext.Web.Lists.GetByTitle("Shared Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View Scope='Recursive' />";
ClientOM.ListItemCollection listItems =
sharedDocumentsList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (var item in listItems)
{
}
Not sure if you are looking for Clientside code or not.
Upvotes: 0
Reputation: 1706
You can create a SPQuery
object and set its Scope
attribute to RecursiveAll
, for example:
SPQuery query = new SPQuery();
SPFolder folder = get the folder object by folder path
query.Folder = folder;
query.ViewXml = "<View Scope=\"RecursiveAll\"><Query>your query goes here</Query></View>";
SPListItemCollection items = yourLibrary.GetItems(query);
Dictionary<string, List<SPListItem>> folderItems = new Dictionary<string, SPListItem[]>();
foreach (SPListItem item in items)
{
// If items are files
SPFile file = item.Web.GetFile(item.Url);
string folderName = file.ParentFolder.Name;
if (!folderItems.ContainsKey(folderName))
{
folderItems[folderName] = new List<SPListItem>();
}
folderItems[folderName].Add(item);
}
Upvotes: 2