Is there any way to retrieve the webpartxml using csom. I'm able to get the WebPartDefinition of all web parts in a page using LimitedWebPartManager class but WebPartDefinition object or properties.FieldValues doesn't contain the Webpartxml. My intension is to replicate this webpart in a separate site collection using the webpartxml.
1 Answer
Look at this: https://blog.mastykarz.nl/export-web-parts-csom/ (Source)
You would have to export the webpart, then import in the site you choose. Like this (this excerpt coming from above link):
using (ClientContext ctx = new ClientContext("https://contoso.sharepoint.com")) {
SecureString password = new SecureString();
// setting password omitted for brevity
ctx.Credentials = new SharePointOnlineCredentials("[email protected]", password);
File file = ctx.Web.GetFileByServerRelativeUrl("/pages/default.aspx");
LimitedWebPartManager wpMgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
Guid webPartId = new Guid("468a7c31-053b-41b3-9e7e-d658a3ecb7fc");
ClientResult<string> webPartXml = wpMgr.ExportWebPart(webPartId);
ctx.ExecuteQuery();
// Web Part XML: webPartXml.Value
}
-
1I'm getting this error on wpMgr.ExportWebPart: Method "ExportWebPart" does not exist. Is this method only available to SP Online as I'm trying to fetch webpartxml from on-prem SP.– Adi SCommented Aug 21, 2017 at 13:30
-
Also, I am trying to use the GetWebPart2 method from "/_vti_bin/WebPartPages.asmx" service by making a soap call but it's giving me a 401 unauthorized error.– Adi SCommented Aug 21, 2017 at 13:48
-
@AdiS The method is available in SharePoint 2019 too. Older on-prem SP do not have it. Commented Nov 8, 2022 at 13:29