Saturday, April 14, 2012

MS CRM4: Retrieving and parsing articles from CRM

We had some articles in CRM that was created on specific template for the purpose of displaying on public web site as bilingual FAQs.

The article template had 4 sections, 2 sections for English question and answer in English, and the other 2 sections for Arabic.

            List<kbarticle> results = new List<kbarticle>();

CrmService crmService = ServiceAssembly.GetCrmService();

QueryExpression query = new QueryExpression();
query.EntityName = "kbarticle";
query.ColumnSet = new ColumnSet(attributes);

query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, KbArticleState.Published.ToString()));
query.Criteria.AddCondition(new ConditionExpression("kbarticletemplateid", ConditionOperator.Equal, _articleTemplate));
query.AddOrder("createdon", OrderType.Descending);

int count = 0;
foreach (kbarticle article in crmService.RetrieveMultiple(query).BusinessEntities)
if (count >= 5)

First I retrieved the templates using the template ID that I created specifically for this purpose

now we have the results, we need to parse article xml for each article

                // We need to return back the question and the answer, whilst preserving
// the formatting of the actual answer.
// In addition, the correct language must be displayed (not both language types).

// Example string in contents:
// <articledata><section id='0'><content><![CDATA[test question english]]></content></section><section id='1'>
// <content><![CDATA[<P align=center>test answer english</P> <UL> <LI> <DIV align=center>Hello</DIV></LI>
// <LI> <DIV align=center>test</DIV></LI> <LI> <DIV align=center>test</DIV></LI></UL> <OL> <LI>Oh</LI>
// <LI>Dear</LI> <LI>My <FONT color=#ff0000><STRONG>ha ha ha</STRONG></FONT></LI></OL>]]></content>
// </section><section id='2'><content><![CDATA[<P align=center>السؤيبليب</P> <P align=center>&nbsp;يبليبلي سليبسليبللا</P>]]></content></section>
// <section id='3'><content><![CDATA[]]></content></section></articledata>

int questionSection = 0;
int answerSection = 1;

if (langugageCode == Lang.ARABIC)
questionSection = 2;
answerSection = 3;

// Load the article
XmlDocument x = new XmlDocument();

// Fetch the question
XmlNode node = x.SelectSingleNode("/articledata/section[@id='" + questionSection + "']/content");

// Question retrieval
string question = node.InnerText;
Tracer.WriteLine("question is: " + question);

// Fetch the answer
node = x.SelectSingleNode("/articledata/section[@id='" + answerSection + "']/content");

// Answer retrieval
string answer = node.InnerText;
Tracer.WriteLine("answer is: " + answer);

// Finally, set the Article class
parsedArticle.Question = question;
parsedArticle.Answer = answer;
parsedArticle.Language = langugageCode;
parsedArticle.ArticleID = articleID;

where parsedArticle is an object that holds the details that we are interested in.

No comments:

Post a Comment