C# 需要在SharePoint 2010中检索我的网站地图列表
我正在尝试使用Visual Studio在SharePoint 2010中创建一个简单的站点地图。下面是我需要做的一个简单的模型C# 需要在SharePoint 2010中检索我的网站地图列表,c#,sharepoint,C#,Sharepoint,我正在尝试使用Visual Studio在SharePoint 2010中创建一个简单的站点地图。下面是我需要做的一个简单的模型 • Site collection o Site List List o Site o Site • Site collection 我可以拉取网站集和网站,但无法检索列表。我尝试使用splist,但不知道如何使用SPWeb函数I下的foreach循环 不断得到这个错误 无法将类型为“Mi
• Site collection
o Site
List
List
o Site
o Site
• Site collection
我可以拉取网站集和网站,但无法检索列表。我尝试使用splist,但不知道如何使用SPWeb函数I下的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>");
}
}
}
}
}
使用系统;
使用系统组件模型;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用Microsoft.SharePoint;
使用Microsoft.SharePoint.WebControl;
使用System.Collections.Generic;
使用Microsoft.SharePoint.Administration;
使用SP=Microsoft.SharePoint.Client;
命名空间SiteMapWebPart.SPSiteMap
{
[ToolboxItemAttribute(false)]
公共类SPSiteMap:WebPart
{
DropDownList webAppList=新的DropDownList();
按钮提交=新建按钮();
List siteList=新列表();
受保护的覆盖无效OnInit(事件参数e)
{
SPFarm=SPFarm.Local;
SPWebService服务=farm.Services.GetValue(“”);
foreach(SPWebApplication-webApp-in-service.WebApplications)
{
webAppList.Items.Add(webApp.alternateUrs[0].Uri.ToString());
}
}
受保护的覆盖无效CreateChildControls()
{
submit.Text=“提交”;
submit.Click+=新建事件处理程序(submit\u Click);
this.Controls.Add(webAppList);
本.控件.添加(提交);
}
无效提交\单击(对象发送者,事件参数e)
{
SPWebApplication webApp=SPWebApplication.Lookup(新Uri(webAppList.SelectedItem.ToString());
foreach(webApp.Sites中的SPSite站点)
{
添加(site.Url.ToString());
site.Dispose();
}
}
受保护的覆盖无效渲染(HtmlTextWriter编写器)
{
伦德尔德伦(作家);
writer.Write(“
”);
foreach(站点列表中的字符串url)
{
使用(SPSite站点=新SPSite(url))
{
foreach(site.AllWebs中的SPWeb站点)
{
作者:写(“”);
writer.Write(“+web.Title+”);
作者:写(“”);
Dispose();
}
作者:写(“”);
}
}
}
}
}
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();
}