How to map wiki content(WikiField) webpart id and webpart defination id?

Yuvraj Patil 361 Reputation points

I am working on SharePoint site, where my requirement is backup the wiki page (its properties and web parts) and later if page is deleted then restore it from the backup copy.

I am able to backup the wiki page content from its WikiField property and web parts using LimitedWebPartManager.

Now as restore, while creating new wiki page I have used backed up WikiField for content and adding web parts into newly created wiki page using LimitedWebPartManager, however the web parts are not shown on the wiki page, after some RND, found that WikiField holds the web part data in some div and there is webpart id as well which is not matching with newly generated ids of web parts after adding ( and not matching with backed up web parts definition id).

I can not add the div with newly created webpart id at the end or beginning of WikiField as wiki page have other content as well and I want webparts to be added on the same place and in the same sequence.

I am using SharePoint CSOM(C#) for this.

How can I achieve this?

While backing up page, If I am able to map the webpart id from wiki content with webpartdefination id then I can keep this data somewhere and use the same while restoring, any idea around this?

This is the wiki page I have :


WikiField property content for the wiki page :

<div class="ExternalClassB6921781E82B446CB3BFC1E687ADA254">
<table id="layoutsTable" style="width:100%;">
<tr style="vertical-align:top;">
<td style="width:100%;">
<div class="ms-rte-layoutszone-outer" style="width:100%;">
<div class="ms-rte-layoutszone-inner">
<div class="ms-rtestate-read ms-rte-wpbox">
<div class="ms-rtestate-notify ms-rtestate-read 74353ad4-33f0-4df8-8cd9-c6f42659f92d" id="div_74353ad4-33f0-4df8-8cd9-c6f42659f92d"/>
<div id="vid_74353ad4-33f0-4df8-8cd9-c6f42659f92d" style="display:none;"/>
<div class="ms-rtestate-read ms-rte-wpbox">
<div class="ms-rtestate-notify ms-rtestate-read 6fac0a22-1e28-4e70-8f63-97ab32cd728d" id="div_6fac0a22-1e28-4e70-8f63-97ab32cd728d"/>
<div id="vid_6fac0a22-1e28-4e70-8f63-97ab32cd728d" style="display:none;"/>
<span id="layoutsData" style="display:none;">false,false,1</span>

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,565 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,655 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 30,906 Reputation points Microsoft Vendor

    Hi @Yuvraj Patil ,
    You can refer to following code to export webpart

    using (ClientContext ctx = new ClientContext("")) {  
      SecureString password = new SecureString();  
      // setting password omitted for brevity  
      ctx.Credentials = new SharePointOnlineCredentials("", 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);  
      // Web Part XML: webPartXml.Value  

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. dousee 0 Reputation points

    Have you resolved it? I have the same question