Welcome to OpenXML Developer Sign in | Join | Help

Removing content control when custom xML is updated

Last post 08-19-2008, 7:43 AM by rezash. 7 replies.
Sort Posts: Previous Next
  •  08-04-2007, 7:37 AM 1869

    Removing content control when custom xML is updated


    Hi,

    I am working on server side generation of Word documents where I update the custom XML part of the document and the mapped content controls get the new data from added custom XML. This works great and I can easily change the contents of content controls. What I want is to convert the content controls into normal text when I update the custom XML part. Below is my method to update custom XML part.

    public void UpdateCustomXMLPart(Stream templateStream, System.Xml.XmlDocument customXML)
    {

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templateStream, true))
            {
                MainDocumentPart mainPart = wordDoc.MainDocumentPart;

                mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
                CustomXmlPart customXmlPart = mainPart.AddNewPart<CustomXmlPart>();           

                customXML.Save(customXmlPart.GetStream());

            }
    }

    I don’t have Word object model available in this application. When I add a content control from Word, I can see a property called ‘Remove content control when contents are edited’. This property does exactly what it says, when contents are edited, the content control is deleted and the placeholder test is converted to the normal text. I want exactly the same behavior when I update the custom XML part of the document. It makes sense to remove the content control when document is filled with data but I could not get this through any open xml method/property. Can anyone guide me what I can do to achieve this behavior? Thanks in advance.

  •  08-06-2007, 11:48 PM 1879 in reply to 1869

    Re: Removing content control when custom xML is updated

    Hi,

     

    The cod snippet you have mentioned is only adding customXML part to the main document. If you want to set content control property, you need to construct your document.xml accordingly.

    This is the code snippet of document.xml when content control is not marked as remove after edit

    <w:sdt>

    - <w:sdtPr>
      <w:id w:val="865823534" />
    - <w:placeholder>
      <w:docPart w:val="DefaultPlaceholder_22675703" />
      </w:placeholder>
      <w:showingPlcHdr />
      <w:dataBinding w:prefixMappings="xmlns:ns0='http://myschemas/thisone'" w:xpath="/ns0:Customer[1]/ns0:Name" w:storeItemID="myCustomXmlDataIdentifier" />
      <w:text />
      </w:sdtPr>
    - <w:sdtContent>
    - <w:r w:rsidRPr="00B90F6A">
    - <w:rPr>
      <w:rStyle w:val="PlaceholderText" />
      </w:rPr>
      <w:t>Click here to enter text.</w:t>
      </w:r>
      </w:sdtContent>
      </w:sdt>
     
    The presense of placeholder element allows not to remove content control after edit
     
    This is the code snippet when content control property is set to remove the contrl after edit
     
    w:r w:rsidR="00976A6E">
      <w:t>Name Changed</w:t> 
      </w:r>
    I I feel, CustomXML part and content control are two seperate things.
     
    Sheela
  •  08-07-2007, 4:14 PM 1891 in reply to 1879

    Re: Removing content control when custom xML is updated

    Hi Sheela,

    Thanks for reply. You are right. Custom XML part and content control are two separate things and updating the Custom XML part does not make any difference on the behavior of content control. I believe the reason is this that updating the Custom XML part does not raise a ‘content change’ event in Word object model so no matter what we do with Custom XML part, it’s not going to convert content control to normal text even when content control is marked as 'temporary'. The code snippet you provided does not make any difference either. I added a content control by adding the provided snippet in document.xml and the updating the Custom XML doesn’t remove content control.

    I think a possible workaround to this could be to remove all content control’s markup and insert markup for normal text in document.xml, but that sounds over killed.

    -----------------document.xml-------------------

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">

    <w:body>

    <w:sdt>

    <w:sdtPr>

    <w:dataBinding w:xpath="/Person[1]/Name[1]" w:storeItemID="{52DECFC9-C58F-43AC-ADB8-12A869012D4A}" />

    <w:alias w:val="SampleTile" />

    <w:tag w:val="SampleTag" />

    <w:id w:val="6383658" />

    <w:temporary />

    <w:text />

    </w:sdtPr>

    <w:sdtContent>

    <w:p w:rsidR="00CF0D2F" w:rsidRDefault="00E8253E">

    <w:r w:rsidR="00976A6E">

    <w:t>Name Changed</w:t>

    </w:r>

    </w:p>

    </w:sdtContent>

    </w:sdt>

    <w:sectPr w:rsidR="00CF0D2F" w:rsidSect="00CF0D2F">

    <w:pgSz w:w="12240" w:h="15840" />

    <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />

    <w:cols w:space="720" />

    <w:docGrid w:linePitch="360" />

    </w:sectPr>

    </w:body>

    </w:document>

  •  08-07-2007, 11:53 PM 1896 in reply to 1891

    Re: Removing content control when custom xML is updated

    Hi Wajid,

    I have used this document.xml containing <w:temporary /> to mark content control

    to remove content control when edited. And it exact does what is suppose to do.

    I am loading document.xml file into main document
     
    using (WordprocessingDocument wordDoc =

    WordprocessingDocument.Open(@"C:\test\aa.docx", true))
                {
                    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

                  
                    XmlDocument mainDoc = new XmlDocument();
                    mainDoc.Load(@"..\..\MainpartXML.xml"); // this document.xml file

    containing <w:temporary />

                    mainDoc.Save(mainPart.GetStream());

    // your code goes here

    When the document is ready with the custom xml, content control with a place

    holder appear.
    Select the content control, select Developer ribbbon and click on properties in

    the controls box."remove content controls when controls are edited" is checked.

    So, <w:temporary /> element is doing its job.

    This is my document.xml file
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <w:document xmlns:ve="
    http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:o="urn:schemas-microsoft-com:office:office"

    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"

    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"

    xmlns:v="urn:schemas-microsoft-com:vml"

    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"

    xmlns:w10="urn:schemas-microsoft-com:office:word"

    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"

    xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
     <w:body>
      <w:p w:rsidR="000F3ACF" w:rsidRDefault="000F3ACF"

    w:rsidP="000F3ACF">
       <w:r>
        <w:t>The customer name is:</w:t>
       </w:r>
       <w:r>
        <w:tab/>
       </w:r>
       <w:sdt>
        <w:sdtPr>
         <w:id w:val="865823534"/>
         <w:placeholder>
          <w:docPart

    w:val="DefaultPlaceholder_22675703"/>
         </w:placeholder>
         <w:showingPlcHdr/>
         <w:dataBinding

    w:prefixMappings="xmlns:ns0='http://myschemas/thisone'"

    w:xpath="/ns0:Customer[1]/ns0:Name" w:storeItemID="myCustomXmlDataIdentifier"/>
         <w:alias w:val="SampleTile" />

         <w:tag w:val="SampleTag" />


         <w:text/>
         <w:temporary />
        </w:sdtPr>
        <w:sdtContent>
         <w:r w:rsidRPr="00B90F6A">
          <w:rPr>
           <w:rStyle

    w:val="PlaceholderText"/>
          </w:rPr>
          <w:t>Click here to enter

    text.</w:t>
         </w:r>
        </w:sdtContent>
       </w:sdt>
       <w:r w:rsidRPr="000F3ACF">
        <w:t xml:space="preserve"> </w:t>
       </w:r>
      </w:p>
      <w:p w:rsidR="000F3ACF" w:rsidRPr="000F3ACF"

    w:rsidRDefault="000F3ACF" w:rsidP="000F3ACF"/>
      <w:sectPr w:rsidR="000F3ACF" w:rsidRPr="000F3ACF"

    w:rsidSect="000F371E">
       <w:pgSz w:w="12240" w:h="15840"/>
       <w:pgMar w:top="1440" w:right="1800" w:bottom="1440"

    w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
       <w:cols w:space="720"/>
       <w:docGrid w:linePitch="360"/>
      </w:sectPr>
     </w:body>
    </w:document>

    Sheela

     

  •  08-08-2007, 2:46 PM 1906 in reply to 1896

    Re: Removing content control when custom xML is updated

    Hi sheela,

    I totally agree with you that ‘Temporary’ tag does its job. This is not I am looking at. What I want is to find a mechanism to remove content control when I insert or update Custom XML part through Open XML API. I can surely add a content control marked as temporary but this temporary content control is removed only when I edit the contents in the content control within Word. I am interested in removing the content control when I update the mapped custom XML through code.

    Take the same example,

    <w:dataBinding w:xpath="/Person[1]/Name[1]" w:storeItemID="{52DECFC9-C58F-43AC-ADB8-12A869012D4A}" />

    As you can see above, the content control is bound to the ‘Name’ child element of ‘Person’ element.  We all know that the content control gets its contents from the mapped XML and we can simply feed new custom XML part to change the contents of the content control. However, updating the custom XML part does not remove content control even if they are marked as temporary. Suppose I insert the following Custom XML part to the document,

    <Person>

      <Name>My Name</Name>

    </Person>

     I am wondering if I can remove the content control as soon as I feed Custom XML part and the mapping is finished.

  •  08-09-2007, 12:07 AM 1910 in reply to 1906

    Re: Removing content control when custom xML is updated

    Hi,

    Since application does not know that mapped xml data had changed, so even having 'Temporary’  tag will not solve the problem.

    Since there is no object to expect an event of data change, you need to take care in your code such that whenever there is a change in the custom xml data, remove content control and place text.

    Sheela

     

  •  08-09-2007, 3:51 AM 1913 in reply to 1910

    Re: Removing content control when custom xML is updated

    Hi Sheela,

    I had the same feeling, thanks for confirming it. :)

  •  08-19-2008, 7:43 AM 3578 in reply to 1869

    Re: Removing content control when custom xML is updated

    Dear Sir ,

    May you please tell me how can I add a content control to a table's row in a word document by using open XML programatically ?

View as RSS news feed in XML