通过CSOM和PowerShell在SharePoint Online中的Wiki页面上添加webPart

问题描述:

这基本上与以下文章相同:

This is essentially the same post as: Adding webParts on a Wiki page in SharePoint Online through CSOM and PowerShell on the SharePoint Exchange.

我也想将其发布在这里,以防有人对解决这个问题有不同的见解或不同的方式.

I also wanted to post it here in case someone would have a different vision or a different way to approach this issue.

这是问题所在,使用以下代码:

Here is the problem, with the following code:

$xmlDocument = Get-Content -Path "C:\Users\<censored>\desktop\table_matieres.webpart" -Raw

$wpManager = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$importWP = $wpManager.ImportWebPart($xmlDocument)

$wp = $wpManager.AddWebPart($importWP.WebPart,"wpz",0)
$context.Load($wp)
$context.ExecuteQuery()

$props = $page.ListItemAllFields
$context.load($props)
$props["WikiField"] = "<div class=""ms-rtestate-read ms-rte-wpbox""><div class=""ms-rtestate-notify ms-rtegenerate-notify ms-rtestate-read " + $wp.Id + """ id=""div_"+ $wp.Id + """></div><div id=""vid_"+ $wp.Id + """ style=""display:block""></div></div>"
$page.Update()
$context.ExecuteQuery()

我在SharePoint Designer(ASPX页)中得到以下信息:

I get the following in SharePoint Designer (ASPX page):

<SharePoint:EmbeddedFormField id="WikiField" FieldName="WikiField" ControlMode="Display" runat="server"><div class="ExternalClassA538D3FA9C374CD6B8B7E2F117F2C08D">
<table id="layoutsTable" style="width&#58;100%;">
<tbody>
<tr style="vertical-align&#58;top;">
<td colspan="2"><div class="ms-rte-layoutszone-outer" style="width&#58;100%;"><div class="ms-rte-layoutszone-inner" style="word-wrap&#58;break-word;margin&#58;0px;border&#58;0px;"><div class="ms-rtestate-read ms-rte-wpbox" ><div class="ms-rtestate-read 1aaac0d0-d055-4652-8be9-8880d2f16583" id="div_1aaac0d0-d055-4652-8be9-8880d2f16583"></div>
<div class="ms-rtestate-read" id="vid_1aaac0d0-d055-4652-8be9-8880d2f16583" style="display&#58;none;"></div>
</div>
</div></div></td>
</tr>
<tr style="vertical-align&#58;top;">
<td style="width&#58;49.95%;"><div class="ms-rte-layoutszone-outer" style="width&#58;100%;"><div class="ms-rte-layoutszone-inner" style="word-wrap&#58;break-word;margin&#58;0px;border&#58;0px;">
<WpNs1:SiteFeedWebPart runat="server" __MarkupType="xmlmarkup" WebPart="true" __WebPartId="{0E290259-6B6D-4524-ACBC-4EBD6C93A6F3}" >
<WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">
<Title>Flux de site</Title>
<FrameType>None</FrameType>
<Description>Le flux de site contient des conversations de microblog sur un site de groupe.</Description>
<IsIncluded>true</IsIncluded>
<ZoneID>wpz</ZoneID>
<PartOrder>1</PartOrder>
<FrameState>Normal</FrameState>
<Height />
<Width />
<AllowRemove>true</AllowRemove>
<AllowZoneChange>true</AllowZoneChange>
<AllowMinimize>true</AllowMinimize>
<AllowConnect>true</AllowConnect>
<AllowEdit>true</AllowEdit>
<AllowHide>true</AllowHide>
<IsVisible>true</IsVisible>
<DetailLink />
<HelpLink />
<HelpMode>Modeless</HelpMode>
<Dir>Default</Dir>
<PartImageSmall />
<MissingAssembly>Impossible d&apos;importer ce composant WebPart.</MissingAssembly>
<PartImageLarge />
<IsIncludedFilter />
<ExportControlledProperties>true</ExportControlledProperties>
<ConnectionID>00000000-0000-0000-0000-000000000000</ConnectionID>
<ID>g_a2fc68fc_4cad_4acf_bca1_0d25a47e533f</ID>
</WebPart>
</WpNs1:SiteFeedWebPart>

</div></div></td>
<td class="ms-wiki-columnSpacing" style="width&#58;49.95%;"><div class="ms-rte-layoutszone-outer" style="width&#58;100%;"><div class="ms-rte-layoutszone-inner" style="word-wrap&#58;break-word;margin&#58;0px;border&#58;0px;"><div class="ms-rtestate-read ms-rte-wpbox" ><div class="ms-rtestate-read bf7102be-ad8c-4a20-83cb-5421f84c6e65" id="div_bf7102be-ad8c-4a20-83cb-5421f84c6e65"></div>
<div class="ms-rtestate-read" id="vid_bf7102be-ad8c-4a20-83cb-5421f84c6e65" style="display&#58;none;"></div>
</div>
</div></div></td>
</tr>
</tbody>
</table>
<span id="layoutsData" style="display&#58;none;">true,false,2</span></div></SharePoint:EmbeddedFormField>

注意:您可以在以下时间注意到问题:

Note: You can notice the issue after:

</WpNs1:SiteFeedWebPart>

由于它试图将Webpart添加到另一个Wiki区域中,因此它在页面上显示为不可见.

Since it tries to add the webpart in another wiki zone, it appears as invisible on the page.

我的目标是编辑一个带标题和2列的Wiki区域,将Site RSS Feed保留在左下角,右下角保持空白,顶部保留一个内容Web部件表.

My goal would be to edit a wiki zone with a header and 2 columns, keeping the Site RSS Feed in the lower left corner, the lower right one remaining empty and the top section would hold a table of content webpart.

请问有一种方法(通过PowerShell和CSOM)修改ASPX页面以实现此目标吗?

Would there be a way to modify the ASPX page (through PowerShell and CSOM) to achieve this goal please?

感谢您的时间和帮助.

您好

检查PageExtensions. OfficeDevPnP中的AddWebPartToWebPartPage方法: https://github.com/SharePoint/PnP- Sites-Core/blob/master/Core/OfficeDevPnP.Core/Extensions/PageExtensions.cs .它进行了其他修复 添加Web部件后访问WikiField内容.

check PageExtensions.AddWebPartToWebPartPage method from OfficeDevPnP: https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/Extensions/PageExtensions.cs. It makes additional fixes to WikiField content after adding web part.