{"id":3045,"date":"2016-03-19T09:55:08","date_gmt":"2016-03-19T09:55:08","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=3045"},"modified":"2016-03-20T09:33:31","modified_gmt":"2016-03-20T09:33:31","slug":"iterating-through-all-content-controls-in-an-open-xml-wordprocessingml-document","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/iterating-through-all-content-controls-in-an-open-xml-wordprocessingml-document\/","title":{"rendered":"Iterating through all Content Controls in an Open XML WordprocessingML Document"},"content":{"rendered":"<div>Sometimes you want to iterate over all content controls in a WordprocessingML document.  You may want to search for a content control with a specific tag, or you may want to process all content controls of a specific type.  This blog post shows how to iterate over all content controls.<\/p>\n<p>One key point is that if you want to be thorough about this, you need to search for content controls in all parts where content controls can exist.  They can exist in the following parts:<\/p>\n<ul>\n<li>Main document part<\/li>\n<li>Header parts (there can be more than one header part)<\/li>\n<li>Footer parts (there can be more than one footer part)<\/li>\n<li>End note part (there can be zero or one end note part)<\/li>\n<li>Foot note part (there can be zero or one foot note part)<\/li>\n<\/ul>\n<p>The best way to proceed is to write a method that returns a collection of all content controls for a given part, and then write another method that returns a collection of all content controls for all parts.  The most expressive way to write these methods is to write them as extension methods.  This allows us to use those methods by simply \u2018dotting\u2019 into the extension method.  Following is an example that includes the two extension methods, as well as a bit of code to exercise them. <\/p>\n<p>I\u2019ve attached an example document to this post that includes content controls in the main document part, in a header, in a footer, in an end note, and in a foot note.<\/p>\n<p>using&nbsp;System;<br \/>\nusing&nbsp;System.Collections.Generic;<br \/>\nusing&nbsp;System.Linq;<br \/>\nusing&nbsp;System.Text;<br \/>\nusing&nbsp;DocumentFormat.OpenXml;<br \/>\nusing&nbsp;DocumentFormat.OpenXml.Packaging;<br \/>\nusing&nbsp;DocumentFormat.OpenXml.Wordprocessing;<\/p>\n<p>public&nbsp;static&nbsp;class&nbsp;ContentControlExtensions<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;IEnumerable&lt;OpenXmlElement&gt;&nbsp;ContentControls(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this&nbsp;OpenXmlPart&nbsp;part)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;part.RootElement<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Descendants()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(e&nbsp;=&gt;&nbsp;e&nbsp;is&nbsp;SdtBlock&nbsp;||&nbsp;e&nbsp;is&nbsp;SdtRun);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;IEnumerable&lt;OpenXmlElement&gt;&nbsp;ContentControls(<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this&nbsp;WordprocessingDocument&nbsp;doc)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;cc&nbsp;in&nbsp;doc.MainDocumentPart.ContentControls())<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;cc;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;header&nbsp;in&nbsp;doc.MainDocumentPart.HeaderParts)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;cc&nbsp;in&nbsp;header.ContentControls())<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;cc;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;footer&nbsp;in&nbsp;doc.MainDocumentPart.FooterParts)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;cc&nbsp;in&nbsp;footer.ContentControls())<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;cc;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(doc.MainDocumentPart.FootnotesPart&nbsp;!=&nbsp;null)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;cc&nbsp;in&nbsp;doc.MainDocumentPart.FootnotesPart.ContentControls())<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;cc;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(doc.MainDocumentPart.EndnotesPart&nbsp;!=&nbsp;null)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;cc&nbsp;in&nbsp;doc.MainDocumentPart.EndnotesPart.ContentControls())<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;return&nbsp;cc;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n}<\/p>\n<p>class&nbsp;Program<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;void&nbsp;Main(string[]&nbsp;args)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using&nbsp;(WordprocessingDocument&nbsp;doc&nbsp;=<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WordprocessingDocument.Open(&#8220;Test.docx&#8221;,&nbsp;false))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(var&nbsp;cc&nbsp;in&nbsp;doc.ContentControls())<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SdtProperties&nbsp;props&nbsp;=&nbsp;cc.Elements&lt;SdtProperties&gt;().FirstOrDefault();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tag&nbsp;tag&nbsp;=&nbsp;props.Elements&lt;Tag&gt;().FirstOrDefault();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(tag.Val);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<div>Download Document <a href=\"https:\/\/www.ericwhite.com\/blog\/wp-content\/uploads\/2016\/03\/Test.docx\">Test.docx<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to iterate over all content controls in a WordprocessingML document. You may want to search for a content control with a specific tag, or you may want to process all content controls of a specific type. This blog post shows how to iterate over all content controls. One key point is that [&hellip;]<\/p>\n","protected":false},"author":10567,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-3045","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/10567"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=3045"}],"version-history":[{"count":3,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3045\/revisions"}],"predecessor-version":[{"id":3049,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/3045\/revisions\/3049"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=3045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}