使用 CSOM 进行初始部署后,将 SharePoint 列表 Web 部件添加到特定页面

Posted

技术标签:

【中文标题】使用 CSOM 进行初始部署后,将 SharePoint 列表 Web 部件添加到特定页面【英文标题】:Adding SharePoint list webpart to a Specific Page after Initial deployment with CSOM 【发布时间】:2017-12-30 04:17:02 【问题描述】:

在我的 sharepoint 站点部署期间,我创建了一些列表,我想以编程方式将它们添加到主页,但我找不到任何有用的东西。

现在,我收到以下错误:

The property or field 'SchemaXml' has not been initialized. 
It has not been requested or the request has not been executed.
It may need to be explicitly requested.'

这里是在部署期间创建列表的代码:

ArrayList arrFields = new ArrayList();
        arrFields.Add(fieldBody);
        arrFields.Add(fieldPublishStartDate);
        arrFields.Add(fieldPublishStopDate);
        arrFields.Add(fieldReadMore);

        //create the home page content lists

        List FeaturedStories = CreateList(ctx, currentWeb, "Featured Stories", ListTemplateType.PictureLibrary, arrFields, true);
        CreateList(ctx, currentWeb, "Team Spotlight", ListTemplateType.PictureLibrary, arrFields, true);
        List quickLinks = CreateList(ctx, currentWeb, "Quick Links", ListTemplateType.Links, null, true);
        List resourceLinks = CreateList(ctx, currentWeb, "Resource Links", ListTemplateType.Links, null, true);

        arrFields.Remove(fieldBody);
        List announcementsList = CreateList(ctx, currentWeb, "News and Announcements", ListTemplateType.Announcements, arrFields, true);
        List eventsCalendarList = CreateList(ctx, currentWeb, "Events Calendar", ListTemplateType.Events, arrFields, true);

        ctx.Load(currentWeb, w => w.Lists);
        ctx.ExecuteQuery();

这是我尝试将列表之一添加到主页的代码:

var pageUrl = "/sites/practice3/SitePages/Home.aspx";
var xml = ctx.Web.Lists.GetByTitle("News and Announcements").SchemaXml;
var web = ctx.Web;
var lists = web.Lists;
ctx.Load(lists);
ctx.ExecuteQuery();
AddWebPart(ctx, pageUrl, xml);

这是AddWebPart的方法,但我无法测试这是否可行,因为我无法访问代码:

public static void AddWebPart(ClientContext context, string pageUrl, string xml)
    
        var page = context.Web.GetFileByServerRelativeUrl(pageUrl);
        var webpartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        context.Load(webpartManager);
        context.Load(webpartManager.WebParts);
        context.ExecuteQuery();
        WebPartDefinition webpartDef = webpartManager.ImportWebPart(xml);
        WebPartDefinition webpart = webpartManager.AddWebPart(webpartDef.WebPart, "mainContent", 0);
        context.Load(webpartDef);
        context.ExecuteQuery();
        //webpartManager.ImportWebPart(xml);
        //context.ExecuteQuery();
        Console.WriteLine("Hit any key to continue...");
        Console.ReadKey();
        Console.ReadKey();
    

感谢您的帮助!

【问题讨论】:

【参考方案1】:

您正在加载所有列表信息(带有基本文件),因此在尝试访问 XML 数据时出现NonInitializedProperty 异常。如果你想得到你的 Schema,你需要显式地加载它。

var pageUrl = "/sites/practice3/SitePages/Home.aspx";
// mark object you would like to access
var list = ctx.Web.Lists.GetByTitle("News and Announcements");
// prepare load query with SchemaXml property
ctx.Load(list, l=>l.SchemaXml);
ctx.ExecuteQuery(); // request the data

AddWebPart(ctx, pageUrl, list.SchemaXml);

希望对您有所帮助。

【讨论】:

这解决了这个问题...谢谢!现在我在AddWebPart 函数的行中收到一个错误,上面写着:Cannot recognize the XML namespace of this web part 我不知道为什么会这样,但是如果您有任何想法,那将非常有帮助! 那是因为您将列表 SchemaXML 而不是 Web 部件 xml 内容传递给 webpartManager.ImportWebPart() 方法。如果您想获取您的 Web 部件,请发送正确的 XML 或过滤 webpartManager.WebParts(不要忘记加载该属性)以获取您的 Web 部件。然后将其添加到页面中。 我想这是我的问题,因为我认为 SchemaXML 是我正在寻找的 XML。

以上是关于使用 CSOM 进行初始部署后,将 SharePoint 列表 Web 部件添加到特定页面的主要内容,如果未能解决你的问题,请参考以下文章

从 Sharepoint 列表中获取所有项目,包括使用 CSOM 的查找值

不使用 SharePointOnlineCredentials 的 Project Online CSOM

如何使用 CSOM 从/向 SharePoint 2013 下载/上传文件?

无法通过 App Catalog 将 SPFx webpart 部署到 sharepoint 2019

如何使用 CSOM 禁用 SharePoint 2013 警报

sharepoint 一个farm中部署多个sql