3

I am trying to add a Table-of-contents webpart to an Enterprise Wiki page using the Client Object Model in a custom migration Add-in for Sharepoint Online.

I have exported the xml for the webpart and it starts with this:

<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Microsoft.SharePoint.Publishing.WebControls.TableOfContentsWebPart, Microsoft.SharePoint.Publishing, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

I try to add it with the following code:

 Microsoft.SharePoint.Client.File page = context.Web.GetFileByServerRelativeUrl(pageUrl); 
 LimitedWebPartManager lwp = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

 WebPartDefinition wpd = lwp.ImportWebPart(tocWebPartXml);

 lwp.AddWebPart(wpd.WebPart, "wpz", 0); 
 context.ExecuteQueryRetry();

Where pageUrl = "/sites/DKWikiTst/wiki/User_lpedersen/Pages/home.aspx" and the site Url for context is "https://COMPANY.sharepoint.com/sites/DKWikiTst"

But I get "Exception has been thrown by the target of an invocation".

I also tried to add a Document Library web part using this xml:

<?xml version="1.0" encoding="utf-8" ?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="ListUrl" type="string">MyLibrary</property>
        <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>
      </properties>
    </data>
  </webPart>
</webParts>

That failed with "File not found", but that was because its ListUrl property was set to a placeholder value like "MyLibrary". I changed it to "Documents" and the error went away.

1 Answer 1

0

Since Sharepoint errors are meaningless, I had to use trial and error to find the problem. It turns out that 2 properties in the exported XML caused the error. The offending properties were "ListId" and "ListName". After I deleted those two from the XML, the webpart could be added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.