Update Section

Last modified by Vincent Massol on 2021/03/18 11:28

cogModify the content of a specific section programmatically
TypeSnippet
CategoryOther
Developed by

Sunil Khiatani

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

You may need

{{groovy}}
   import com.xpn.xwiki.api.DocumentSection;

    searchSectionTitle = "Section 2";
   def sampleDoc = xwiki.getDocument('Test2.Edit a Section - Sample Page');
 
    println sampleDoc.getTitle();
 
   def section = findSectionWithJIRALink(sampleDoc);
   def sectionNumber = section.getSectionNumber();        
  
   def content = sampleDoc.getDocument().updateDocumentSection(sectionNumber, "=Section 2=\nThis is the new content in the section");

    println content;

    sampleDoc.setContent(content);   
    sampleDoc.save();
   
    DocumentSection findSectionWithJIRALink(doc) {

       def sections = doc.getSections();
       
       for (section in sections) {
           if (section.getSectionTitle() == searchSectionTitle) {
               return section;
            }
        }

       // no match found
       return null;
    }
 
{{/groovy}}
     

Get Connected