I want to write the following inside a c# console application:-

  1. I have a site collection http://servername/
  2. This site collection have a subsite http://servername/customers/
  3. And inside the customers sub site I have multiple subsites such as; /customers/A/ , /customers/B/ , etc.

Now I want to do the following loops:-

  1. To loop through all the subsites under the http://servername/customers/ subsite.
  2. And inside each subsite (for example /customers/A/)
  3. To get a list title = “Manager Tracking”
  4. And loop through all the items under the “Manager Tracking” list?

Can anyone advice how I can perfume this using SharePoint server-side object model? The problem I am facing is that I am unable to do the following to start from the http://servername/cusotmers/ subsite:-

SPWeb subsite = new SPWeb(“http://servername/cusotmers/”)

Where I will get this error:-

Microsoft.SharePoint.SPWeb' does not contain a constructor that takes 1 arguments


2 Answers 2


You can use the code as below:

using(SPSite site = new SPSite("http://servername/"))  //site collection url
    using(SPWeb web = site.OpenWeb("customers")) //specific subsite url
        SPWebCollection subWebs = web.Webs; //get all sites under specific subsites

        foreach (SPWeb subSite in subWebs)

            SPList managerList = subSite.Lists.TryGetList("Manager Tracking");

            foreach (SPListItem item in managerList.Items)
                //your logic



  • can you please adivce when i use lists.TryGetList("....") should i specify the list internal name or the list title ? if i need to use the list internal names ,, then can i modify this to use the list title ?
    – John John
    Oct 17, 2016 at 12:48
  • 1
    list title should be specified. Oct 17, 2016 at 13:47
  • ok will check this inside my environment ,, thanks
    – John John
    Oct 17, 2016 at 13:56
  • and the tile is case insensitive is this correct? based on my test the tile is case insensitive ,,but just wanted to confirm ?
    – John John
    Oct 17, 2016 at 15:45

If you are using Site collection Object then It will traverse through all sub sites.

 using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            using (SPWeb web = site.OpenWeb())
                SPList list = web.Lists.TryGetList("ListName");

                    if (list != null)
                        SPQuery query = new SPQuery();
                        query.Query = "<Where>" +
                                    "<Contains>" +
                                    "<FieldRef Name='Title' />" +
                                    "<Value Type='Text'>" + keyWord + "</Value>" +
                                    "</Contains>" +

                        SPListItemCollection items = list.GetItems(query);
                        if (items != null && items.Count > 0)
                        foreach (SPItem item in items)
                            var Title = item["Title"] != null ? item["Title"].ToString() : string.Empty;




Your Answer

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

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