0

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

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

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

无法将“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>");            

            }
            }


            }
            }

            }
4

1 回答 1

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();            
    }       
于 2013-02-27T06:11:33.643 回答