需要在SharePoint 2010中检索我的站点地图列表

时间:2013-02-27 05:59:34

标签: c# sharepoint

我正在尝试使用Visual Studio在SharePoint 2010中创建一个简单的站点地图。下面是我需要做的简单模型。

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

我能够提取网站集和网站,但无法检索我尝试过使用SPLists但却不知道如何在SPWeb函数下使用foreach循环的List 不断收到此错误

无法将“Microsoft.SharePoint.SPWeb”类型的对象转换为“Microsoft.SharePoint.SPList”类型。

我是SharePoint开发人员的开始,所以我对此非常陌生。如果有人能让我开始,我可以从那里开始。以下是我的代码

            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 个答案:

答案 0 :(得分:0)

SPWeb对象具有属性 - 列表。这是此Web中的SPList对象的集合。

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();            
    }