One of the big changes we made in the Open XML SDK v2 April 2009 CTP was improving the Low Level DOM component to include functionality related to Office 2007 SP2. With this improvement came a difference in how some elements were interpreted as a 1st class property of a parent class/element vs. as a child element. For example, SdtProperties is no longer a property off of the SdtXXX classes, but is rather interpreted as just a child element. Scouring through the different customer feedback channels, the Open XML SDK forum, www.openxmldeveloper.org, and the SDK Connect site, I’ve noticed that this change broke some of my previous posts and code samples. In today’s post, I am going to show you a workaround to this issue and I’m going to point you to April 2009 CTP complaint versions of sample code that is currently broken.
SdtBlock does not contain a definition for SdtProperties
Many of my Word related SDK posts leverage content controls as semantic structure. In my posts, I show you how to use these content controls to easily find content or add content to a specific area. In many of these posts I used the following code snippet construct:
If you try to compile the above code snippet in the latest CTP of the SDK you will get the following compiler error:
‘DocumentFormat.OpenXml.Wordprocessing.SdtBlock’ does not contain a definition for ‘SdtProperties’ and no extension method ‘SdtProperties’ accepting a first argument of type ‘DocumentFormat.OpenXml.Wordprocessing.SdtBlock’ could be found (are you missing a using directive or an assembly reference?)
To resolve this error you need to get access to SdtProperties as child element rather than a property. This task can be accomplished with the following code snippet:
SdtBlock sdt = mainPart.Document.Descendants<SdtBlock>()
SdtBlock does not contain a definition for SdtContentBlock
Similarly to the issue with SdtProperties, SdtContentBlock was moved to be a child element rather than a property. You will need to change the following code snippet from:
Paragraph p = sdt.SdtContentBlock.GetFirstChild<Paragraph>();
Paragraph p = sdt.GetFirstChild<SdtContentBlock>().GetFirstChild<Paragraph>();
Links to Fixed Code Samples/Solutions
Here are links to fixed versions of previous solutions that were broken due to April 2009 CTP changes:
Fixed Code Sample Link
We are currently looking into improving our SDK behavior to re-include some of the lost functionality of not seeing some elements as 1st class properties. Stay tuned for an update.
I also wanted to take this time to thank you guys for all your feedback. Keep sending it our way.