locked
Can't find ContentControls inside the Word's table (OpenXML SDK 2.0) RRS feed

  • Question

  • I'm using OpenXML SDK 2.0 to search for the Content Controls inside the Word file.
    Everything works fine if controls locate on the plain page, where there are no tables and other custom layouts
    But if I put the control inside the table cells I can't find them any longer. 

    I'm using the following code
    SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>()
                        .Where(r => r.SdtProperties.GetFirstChild<Alias>().Val.Value == controlName).Single();
    
    And nothing returned for the controls that are in tables. Only rest of the controls that exist on the page.

    Starting digging around, and moving back to OpenXML SDK v.1 I found that I can find those tabled-controls using the following code
                    var contentControl = doc.MainDocumentPart
                        .GetXDocument()
                        .Descendants(w + "sdt")
                        .Where(e => ((string) e.Elements(w + "sdtPr")
                                                  .Elements(w + "alias")
                                                  .Attributes(w + "val")
                                                  .FirstOrDefault()).ToLower() == "title");
    Trying to match the artifacts from v2 and v1 samples I realized that problem can be in the magical "stdPr" element of v.1 that I need to add to my original code.
    But I can't find the element that correspond to that abbreviation.

    The question is how to find the content controls inside the tables, for the SDK v.2

    SharePoint 2007 - 2010 Tips & Tricks Portal | Microsoft MVP | My Blog about Information Management | My twitter
    Sunday, February 7, 2010 7:58 AM

Answers

  • Does it work if you search for SdtElement instead of SdtBlock?  SdtElement will match all content controls (run, block, etc.).  It could be that your Content Control inside table is not SdtBlock.  You can use Productivity Tool and reflect your document to see what kind of content control it is.
    Monday, February 8, 2010 6:42 AM
  • vj9999 is correct. Using SdtElement will fix the issue. SdtElement is an abstract class for SdtBlock, SdtRun and SdtCell. If you are looking for content controls in tables then why not find all tables first and then find SdtElements as a descendant of the table element.
    Zeyad Rajabi (MS)
    Tuesday, February 9, 2010 3:42 AM

All replies

  • Does it work if you search for SdtElement instead of SdtBlock?  SdtElement will match all content controls (run, block, etc.).  It could be that your Content Control inside table is not SdtBlock.  You can use Productivity Tool and reflect your document to see what kind of content control it is.
    Monday, February 8, 2010 6:42 AM
  • vj9999 is correct. Using SdtElement will fix the issue. SdtElement is an abstract class for SdtBlock, SdtRun and SdtCell. If you are looking for content controls in tables then why not find all tables first and then find SdtElements as a descendant of the table element.
    Zeyad Rajabi (MS)
    Tuesday, February 9, 2010 3:42 AM