Showing posts with label CRM. Show all posts
Showing posts with label CRM. Show all posts

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)
{
count++;
results.Add(article);
if (count >= 5)
break;
}


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();
x.LoadXml(articleXML);

// 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.

Monday, February 13, 2012

MS CRM4: Editing read only or hidden fields set by JS on the fly

we had a CRM form with some fields set as read-only by JavaScript, we wanted to edit one of these fields, and the form was published to production
Screenshot-2012-02-13_14.39.22
The normal way to edit it is to comment the javascript then edit the field then uncomment it back
We thought of another way, using multi edit feature, created another dummy record, then selected both records, and from more actions select Edit
Screenshot-2012-02-13_14.37.26
The form will be opened, nut non of the javascript will be executed, so all fields will be displayed in normal mode, read only and hidden fields can be edited
Screenshot-2012-02-13_14.37.41
Then you will be able to update the record without any change in customizations.
Don’t forget to delete the dummy record Winking smile

Thursday, August 19, 2010

MS CRM Changing business unit for a user gives an error

We were trying to change the business unit for a user and it was giving us CRM error.

We already have increased the timeout long time ago by adding the registry keys as instructed here http://support.microsoft.com/kb/918609

Still having the same error.

Lastly we tried cleaning up the Async table as instructed here http://support.microsoft.com/kb/968520

After doing this we were able to change the business unit for the users

Monday, May 31, 2010

MS CRM4: Creating View and setting filter criteria programmatically

We wanted to create a view with criteria on the “Due Date” field to show all records that have due date on or before today

Checking the view creation wizard, we can select on or before and we must specify specific date, we can’t specify  it as “Today”

Screenshot-2012-02-08_17.03.15

I have came up with a workaround for this, I have created the view and set the criteria to specific date, 01/01/1999 for example, any date. Then I created a plugin to work on execute message, on pre-event, then in the plugin I was parsing the FetchXML of the execution, if the primary entity is same as the view entity, and there is a condition on “Due Date” and the value is “01/01/1999” then to change this value to today’s date in the FetchXML.

main code snippets

//get FetchXML from the context
fetchXml = (string)context.InputParameters.Properties["FetchXml"];

//get the entity name
string entity = xmlDoc.SelectSingleNode("fetch/entity").Attributes["name"].Value.ToString();

if (entity == "incident") // here I am using incident, replace with your entity name
{

if (xmlDoc.SelectSingleNode("fetch/entity/filter") != null)
{

// Check if there are conditions exists
if (xmlDoc.SelectSingleNode("fetch/entity/filter/condition") != null)
{
// Loop through the conditions to look for the followup by condition
nodes = xmlDoc.SelectNodes("fetch/entity/filter/condition");
foreach (XmlNode node in nodes)
{
if (node.Attributes["attribute"] != null && node.Attributes["operator"] != null && node.Attributes["value"] != null)
{
string slaOnOrBefore = "01/01/1999";
if (node.Attributes["attribute"].Value.ToString().ToLower() == "followupby".ToLower() && node.Attributes["operator"].Value.ToString().ToLower() == "on-or-before".ToLower() && node.Attributes["value"].Value.ToString().ToLower() == slaOnOrBefore.ToLower())
{
node.Attributes[
"value"].Value = DateTime.Today.AddDays(-1).ToString("yyyy-MM-ddT00:00:00");
}
}
}
}
}

//Update the fetch xml
fetchXml = xmlDoc.InnerXml.ToString();
context.InputParameters.Properties[
"FetchXml"] = fetchXml;