SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to use C# to iterate over the publishing Pages library items on SharePoint 2010 and get their properties. For example, I need to get the item's filename (if it's a file) and detect if it's a folder and get its name. There could be folders within folders, so this might require recursion.

Your help is appreciated. Thanks :)

share|improve this question
up vote 3 down vote accepted

Simplest possible code to just get page and folder names is:

using(SPSite site = new SPSite("http://myCoolSite"))
    using(SPWeb web = site.OpenWeb())
        SPList pageList = web.Lists["MyCoolPageLibrary"];
        // Get items
        foreach(SPListItem item in pageList.Items)
            // item.File.Name 
        // Get folders
        foreach(SPListItem folder in pageList.Folders)
            // folder.Title

However this is not best practice: Read: Best Practices: Common Coding Issues When Using the SharePoint Object Model for more info on this subject. It really depends on your specific needs but enumerating all items and/or folders can easily lead to poor performance and even exceptions and/or timeouts (if working with large lists)


I have found one nice article about folder hierarchies with code examples:

share|improve this answer
Thanks, Vedran. We need to go over a relatively small list of pages (a dozen or so) and one or two folders with a few pages in them. Your help is much appreciated :) – Alex C Oct 6 '11 at 10:19
How do you get the various attributes of a publishing Page, such as its Title? – Alex C Oct 6 '11 at 14:54
item.Title or item["Title"]. You can get even file name by using item["FileLeafRef"]. Just use item["Internal or display field name"]. You can do foreach on item.Fields and see what you can get. – Vedran Rasol Oct 6 '11 at 16:11
Thanks, Vedran. Wow, that's very cool! – Alex C Oct 6 '11 at 17:13
I have updated my answer with link to one interesting article on this subject. I think you can find all answers to your followups there. But if you need any additional help feel free to ask. – Vedran Rasol Oct 6 '11 at 21:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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