【问题标题】:Need to retrieve list for my sitemap in SharePoint 2010需要在 SharePoint 2010 中检索我的站点地图的列表
【发布时间】:2013-02-27 05:59:33
【问题描述】:

我正在尝试使用 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>");            

            }
            }


            }
            }

            }

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多