.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to traverse a SharePoint list folder and folder Item

Posted By:abhays       Posted Date: April 30, 2014    Points: 40    Category: SharePoint    URL: http://www.dotnetspark.com  

Recursive function for traversing List folders and all folder Items
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
namespace Location
{
    class TraverseListItems
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("http://ServerName/"))
            {
                using (SPWeb spweb = siteCollection.OpenWeb())
                {
                    SPList splist = spweb.Lists["ListName"];
                    Trace.WriteLine("Traversing list: " + splist.Title);
                    Trace.WriteLine("Base type: " + splist.BaseType.ToString());
                    TraverseListFolder(splist.RootFolder);
                    Console.ReadLine();
                }
                //SPWebCollection sites = siteCollection.AllWebs;
                //SPList spList=sites
            }
        }
        private static void TraverseListFolder(SPFolder folder)
        {
            SPQuery qry = new SPQuery();
            qry.Folder = folder;
            Console.WriteLine("Folder-- Foldername: " + folder.Name);
            SPWeb web = null;
            try
            {
                web = folder.ParentWeb;
                SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);
                foreach (SPListItem subitem in ic)
                {
                    Console.WriteLine("ItemName: " + subitem.Name);
                    if (subitem.Folder != null)
                    {
                        TraverseListFolder(subitem.Folder);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                if (web != null)
                {
                    web.Dispose();
                }
            }
        }
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend