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

本文关键字:我的 网站 地图 列表 检索 SharePoint 2010 | 更新日期: 2023-09-27 18:01:15

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

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

我可以提取网站集和网站,但无法检索列表。我尝试过使用SPLists,但不知道如何使用SPWeb函数下的foreach循环继续得到这个错误

无法将"Microsoft.SharePoint.SWeb"类型的对象强制转换为"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>");            
            }
            }

            }
            }
            }

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

SPWeb对象具有Lists属性。这是该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();            
    }