在我的项目中,我需要在本地存储网站页面。这些页面可能是已发布或未发布的页面。我尝试过以下代码但无法获取页面内容。
ClientContext clientcontext = new ClientContext(url);
var credentials = new SharePointOnlineCredentials(userid, SecurePass);
clientcontext.Credentials = credentials;
Web web = clientcontext.Web;
clientcontext.Load(web, website => website.Lists);
clientcontext.ExecuteQuery();
clientcontext.Load(web, website => website.Lists,
website => website.Lists.Where(
x => x.BaseTemplate == 119));
clientcontext.ExecuteQuery();
var _collection = web.Lists;
foreach (var pages in _collection)
{
clientcontext.Load(pages);
clientcontext.ExecuteQuery();
CamlQuery cq = new CamlQuery();
var pg = pages.GetItems(cq);
clientcontext.Load(pg);
clientcontext.ExecuteQuery();
foreach (var item in pg)
{
clientcontext.Load(item);
clientcontext.ExecuteQuery();
var f_webPage = clientcontext.Web.GetFileByServerRelativeUrl(item.File.ServerRelativeUrl);
clientcontext.Load(f_webPage);
clientcontext.ExecuteQuery();
}
}
如何获取网站页面内容? 提前谢谢。
答案 0 :(得分:2)
在网站页面库(ListTemplateType.WebPageLibrary
的列表类型)中,可以从以下内容中提取列表项的内容,具体取决于内容类型:
WikiField
的情况下,Wiki Page
字段
在CanvasContent1
(又名"modern" page)Site Page
字段
实施例
var listTitle = "Site Pages";
var list = ctx.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include( i => i["WikiField"], i => i["CanvasContent1"], i => i["FileRef"], i => i.ContentType));
ctx.ExecuteQuery();
foreach (var item in items)
{
Console.WriteLine(">>> {0}", item["FileRef"]);
switch (item.ContentType.Name)
{
case "Site Page":
Console.WriteLine(item["CanvasContent1"]);
break;
case "Wiki Page":
Console.WriteLine(item["WikiField"]);
break;
}
}