0
votes

I'm trying to create a simple sitemap in SharePoint 2010 using Visual Studio. Below is a simple mock-up of what I need to do.

   • Site collection
     o Site
        List
        List
     o Site
     o Site
   • Site collection

I'm able to pull the Site Collection and the Site but not able to retrieve the List I've tried using SPLists but don't know how to use the foreach loop under the SPWeb function I keep getting this error

Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.SPList'.

I'm a beginning SharePoint developer so I'm really new to this. If someone can just get me started I can take it from there. Below is my code

            using System;
            using System.ComponentModel;
            using System.Web;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;
            using Microsoft.SharePoint;
            using Microsoft.SharePoint.WebControls;
            using System.Collections.Generic;
            using Microsoft.SharePoint.Administration;
            using SP = Microsoft.SharePoint.Client;
            namespace SiteMapWebPart.SPSiteMap
            {
            [ToolboxItemAttribute(false)]
            public class SPSiteMap : WebPart
            {
            DropDownList webAppList = new DropDownList();
            Button submit = new Button();
            List<string> siteList = new List<string>();



            protected override void OnInit(EventArgs e)
            {

            SPFarm farm = SPFarm.Local;
            SPWebService service = farm.Services.GetValue<SPWebService>("");

            foreach (SPWebApplication webApp in service.WebApplications)
            {
            webAppList.Items.Add(webApp.AlternateUrls[0].Uri.ToString());
            }

            }


            protected override void CreateChildControls()
            {
            submit.Text = "Submit";
            submit.Click += new EventHandler(submit_Click);
            this.Controls.Add(webAppList);
            this.Controls.Add(submit);
            }

            void submit_Click(object sender, EventArgs e)
            {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(webAppList.SelectedItem.ToString()));


            foreach (SPSite site in webApp.Sites)
            {
            siteList.Add(site.Url.ToString());
            site.Dispose();
            }
            }

            protected override void Render(HtmlTextWriter writer)
            {


            RenderChildren(writer);
            writer.Write("<br/><br/><Table border='1'>");
            foreach (string url in siteList)
            {

            using (SPSite site = new SPSite(url))
            {
            foreach (SPWeb web in site.AllWebs)
            {


            writer.Write("<tr><td><a href='" + url + "'>" + url + "</a></td>");
            writer.Write("<td>" + web.Title + "</td>");
            writer.Write("</tr>");

            web.Dispose();
            }
            writer.Write("</Table>");            

            }
            }


            }
            }

            }
1

1 Answers

0
votes

SPWeb object has property - Lists. This the a collection of SPList objects in this web.

static void Main(string[] args)
    {
        try
        {
            SPSite site = new SPSite("http://xxx");
            foreach (SPWeb web in site.AllWebs)
            {
                Console.WriteLine(web.Title);
                foreach (SPList list in web.Lists)
                    Console.WriteLine("List: " + list.Title);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.WriteLine("End");
        Console.Read();            
    }