Get Knowledge Article Content Programmatically Using the SDK

I’ve had a few people ask how to do this recently so, here goes!

Getting knowledge article objects using the SDK is the same as getting any other kind of object except with a slight twist.  The System.Knowledge.Article class is one of the few classes in the data model out of the box that has binary data type properties.  Here is a look at the class properties on the System.Knowledge.Article class:


See those two properties there that are binary data type – EndUserContent and AnalystContent?  That’s the tricky bit (pun intended!).

When we are working with a binary data type property we have to first cast the property value to a Microsoft.EnterpriseManagement.Common.BinaryStream object.  Microsoft.EnterpriseManagement.Common.BinaryStream derives from the System.IO.Stream class in the .Net Framework.

Let’s walk through some code.  First get a knowledge article object.  This is standard stuff.



Next we are going to get the EndUserContent property value and cast it to a BinaryStream object:


Now that we have a Stream to work with we can use a StreamReader to read the binary content into a string:



Viola!  Now, another twist is that knowledge in SCSM is stored in RTF format.  You don’t want to show the user the RTF format content so you need to use something like a RichTextBox class control to display the content to the user with the RTF formatting tags hidden and showing the text in the correct format.

In this example I simply assign the string (which contains RTF content) to the .Rtf property of the RichTextBox control:



Here is what the simple demo app looks like after I have clicked the button that has a knowledge article containing RTF formatted text in the EndUserContent property:



I’ve uploaded this code sample project to the TechNet Gallery if you want to grab it.