如何使用 CSOM 将 Web 部件添加到 SitePages/Home.aspx [英] How to add a Web Part into a SitePages/Home.aspx using CSOM

查看:43
本文介绍了如何使用 CSOM 将 Web 部件添加到 SitePages/Home.aspx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人设法使用 CSOM 将 Web 部件添加到 Wiki 页面中?

has anyone managed to add a Web Part into a Wiki page using CSOM?

背景:Home.aspx 是一个 Wiki 页面,其所有 WP 都位于富文本区域(实际上是一个WikiField"列).从技术上讲,它们位于隐藏的wpz"Web 部件区域中,除此之外,在 WikiField 列中总是有一个带有 WP ID 的占位符.

Background: Home.aspx is a Wiki page and all its WPs are located in the rich text zone (in fact a "WikiField" column). Technically they are located in a hidden "wpz" web part zone and in addition to this there is always a placeholder with WP's ID in the WikiField column.

我修改了 http://blog.mastykarz.nl/programmatically-adding-web-parts-rich-content-sharepoint-2010/http://640kbisenough.com/2014/06/26/sharepoint-2013-moving-webparts-programmatically-to-rich-content-zone/ 到这个:

I've modified the existing server-side code seen on http://blog.mastykarz.nl/programmatically-adding-web-parts-rich-content-sharepoint-2010/ and http://640kbisenough.com/2014/06/26/sharepoint-2013-moving-webparts-programmatically-to-rich-content-zone/ into this:

using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;

public class Class1
{
    void DeployWebPart(ClientContext clientContext, string zone, string pattern, string position)
    {
        List library = clientContext.Web.GetList("/sites/site/SitePages/");
        clientContext.Load(library);
        clientContext.ExecuteQuery();
        CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
        ListItemCollection itemColl = library.GetItems(query);
        clientContext.Load(itemColl, items => items.Include(i => i.Id, i => i.DisplayName, i => i["WikiField"]));
        clientContext.ExecuteQuery();
        ListItem item = itemColl.Where(i => i.DisplayName == "Home").First();
        clientContext.ExecuteQuery();
        File page = item.File;
        LimitedWebPartManager lwm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        string xmlWebPart = @"<webParts>...</webParts>";
        WebPartDefinition wpd = lwm.ImportWebPart(xmlWebPart);
        WebPartDefinition realWpd = lwm.AddWebPart(wpd.WebPart, "wpz", 0);
        List targetList = clientContext.Web.GetList("/sites/site/Announcements/");
        clientContext.Load(targetList, l => l.Views);
        clientContext.Load(realWpd);
        clientContext.ExecuteQuery();
        string wpId = String.Format("g_{0}", realWpd.Id.ToString().Replace('-', '_'));
        if (zone == "wpz")
        {
            string htmlcontent = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contenteditable=\"false\"><div class=\"ms-rtestate-notify  ms-rtestate-read {0}\" id=\"div_{0}\"></div><div id=\"vid_{0}\" style=\"display:none;\"></div></div>", new object[] { realWpd.Id.ToString("D") });
            string content = item["WikiField"] as string;
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(System.Text.RegularExpressions.Regex.Escape(pattern));
            if (position == "before")
            {
                content = regex.Replace(content, (htmlcontent + pattern), 1);
            }
            else
            {
                content = regex.Replace(content, (pattern + htmlcontent), 1);
            }
            item.Update();
            clientContext.ExecuteQuery();
        }
    }
}

一切正常,直到最后一个 item.Update() 和 clientContext.ExecuteQuery() 被调用.在 Update() 之前,新占位符被正确插入到 WikiField 内容中.但是在 Update() 之后,WikiField 内容恢复到其原始状态 (!)

Everything works fine until the last item.Update() and clientContext.ExecuteQuery() are being invoked. Before the Update() the new placeholder gets properly inserted into the WikiField contents. But after the Update() the WikiField contents reverts back to its original state (!)

注意:作为替代方案,可以将 WP 添加到另一个区域(例如底部").在这种情况下,WP 会显示在页面上.但它有两个主要缺点:新创建的区域格式不正确,WP无法移动甚至删除.

Note: As an alternative it is possible to add the WP into another zone (eg. "Bottom"). In this case the WP gets displayed on the page. But it has two major drawbacks: The newly created zone is not well formatted and the WP cannot be moved or even deleted.

感谢您对此的任何意见.

Thanks for any input on this.

推荐答案

以下示例演示了如何将 Web 部件添加到 Enterprise Wiki 页面:

The following example demonstrates how to add web part into Enterprise Wiki page:

public static void AddWebPartIntoWikiPage(ClientContext context, string pageUrl, string webPartXml)
{
        var page = context.Web.GetFileByServerRelativeUrl(pageUrl);
        var webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

        var importedWebPart = webPartManager.ImportWebPart(webPartXml);
        var webPart = webPartManager.AddWebPart(importedWebPart.WebPart, "wpz", 0);
        context.Load(webPart);
        context.ExecuteQuery();

        string marker = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>", webPart.Id);
        ListItem item = page.ListItemAllFields;
        context.Load(item);
        context.ExecuteQuery();
        item["PublishingPageContent"] = marker; 
        item.Update();
        context.ExecuteQuery();
}

用法

var webPartXml = System.IO.File.ReadAllText(filePath);
using (var ctx = new ClientContext(webUri))
{
    AddWebPartIntoWikiPage(ctx, wikiPageUrl,webPartXml);
}

结果

这篇关于如何使用 CSOM 将 Web 部件添加到 SitePages/Home.aspx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆