使用以下功能我正在尝试创建wiki页面:
static void CreateWikiPage(ClientContext context, string pageName, string pageContent)
{
List wikiPages = context.Web.Lists.GetByTitle("Pages");
context.Load(wikiPages);
context.ExecuteQuery();
context.Load(wikiPages.RootFolder.Files);
context.ExecuteQuery();
wikiPages.RootFolder.Files[0].CopyTo(wikiPages.ParentWebUrl + "/Pages/" + pageName + ".aspx", true);
wikiPages.RootFolder.Update();
context.ExecuteQuery();
context.Load(wikiPages.RootFolder.Files);
context.Load(wikiPages.RootFolder.Folders);
context.ExecuteQuery();
Microsoft.SharePoint.Client.File wikiFile = null;
var query = new CamlQuery
{
ViewXml = "<View><Query><OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy></Query><RowLimit>1</RowLimit></View>"
};
var items = wikiPages.GetItems(query);
context.Load(items, icol => icol.Include(i => i.File));
context.ExecuteQuery();
if (items.Count == 1)
{
var pageFile = items[0].File;
wikiFile = pageFile;
}
ListItem wikiPage = wikiFile.ListItemAllFields;
wikiPage["WikiField"] = pageContent;
wikiPage.Update();
context.ExecuteQuery();
}
我得到例外:Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null.
答案 0 :(得分:0)
由于Pages Library中不存在WikiField
字段,因此可能会发生此错误。我猜您正在尝试在Enterprise Wiki Page
中创建Wiki Page Library
而不是网页。它们不一样,在第一种情况下它是发布页面,页面基于Enterprise Wiki Page
布局,在第二种情况下它是 Wiki WebPart Page
因此,假设需要创建企业Wiki页面,下面将演示如何使用SharePoint 2010中的CSOM完成它。
public static void CreateWikiPage(ClientContext ctx, string pageName, string pageContent)
{
const string templateRedirectionPageMarkup = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>";
var wikiPages = ctx.Web.Lists.GetByTitle("Pages");
var fileInfo = new FileCreationInformation
{
Url = pageName,
Content = Encoding.UTF8.GetBytes(templateRedirectionPageMarkup),
Overwrite = true
};
var wikiFile = wikiPages.RootFolder.Files.Add(fileInfo);
var wikiPage = wikiFile.ListItemAllFields;
wikiPage["PublishingPageContent"] = pageContent;
wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page";
wikiPage.Update();
ctx.ExecuteQuery();
}
<强>用法强>
CreateWikiPage(ctx,"Greetings.aspx","Welcome to SharePoint");