Jonathan Gaskell
Jonathan Gaskell

Reputation: 21

Programmatically creating an article in Dynamics CRM 2011

I have a plugin that is able to update a case in CRM, but I now want it to create a new knowledge base article, as I don't believe it is possible to automate this using a workflow. The plugin is triggered by a workflow that is executed when a case is resolved.

Here is what I have so far but it does not work:

Entity article = new Entity("kbarticle");
article["title"] = articleTitle;
article["subject"] = articleSubject;

service.Create(article);

Guid articleGUID = service.Create(article);

ColumnSet attributes = new ColumnSet(new string[] { "description" });

article = service.Retrieve(article.LogicalName, articleGUID, attributes);

article["description"] = articleDescription;

service.Update(article);

Upvotes: 1

Views: 2562

Answers (4)

Jonathan Gaskell
Jonathan Gaskell

Reputation: 21

thanks for the answers, they all helped to send me in the right direction towards a solution. The main issue was that I needed to use entity references for the subjectid and template:

Entity kbarticle = new Entity("kbarticle");

kbarticle["title"] = title;
kbarticle["subjectid"] = new EntityReference(subject_LogicalName, subject_Guid);
kbarticle["kbarticletemplateid"] = new EntityReference(template_LogicalName, template_Guid);

service.Create(kbarticle);

Upvotes: 1

Greg Owens
Greg Owens

Reputation: 3878

A few things...

Invalid attribute

article["subject"] = articleSubject;

subject is not a valid attribute on the kbarticle entity. subjectid is but will need to be a Lookup to a valid subject record. I can't tell from your snippet if it is or not.

Missing attribute

According to the SDK, you also need to specify a KB template:

When you create a knowledge base article, you have to associate it with a knowledge base template and a subject...

[snip]

To associate an article with a template, use the KbArticle. KbArticleTemplateId attribute. To place an article in a specific category by specifying a subject, use the KbArticle.SubjectId attribute.

Redundant code

Also, probably not the source of your error, but your code tries to create the article twice. Your first line of code here is redundant:

service.Create(article);

Guid articleGUID = service.Create(article);

Beyond that, we really do need to know the error that is raised by your code (though I suspect it will be my first point).

Upvotes: 2

James Wood
James Wood

Reputation: 17562

Have you seen this MSDN article, its not a code example but it describes the steps to create an article.

Edit:

You need to provide us with more debugging information. Either;

  • Enable Tracing as described here or use this handy tool. Reproduce the problem and post the log.
  • Post the error from the workflow itself, as shown here.
  • Attach the debugger and find out whats going on that way.

You would probably find it easier to develop the article creation code against unit tests in Visual Studio, then you can just hook it up to a workflow activity later.

Upvotes: 0

paramosh
paramosh

Reputation: 2258

It should look like

        KbArticle a = new KbArticle();
        a.Title = articleTitle;
        a.SubjectId = new Xrm.Sdk.EntityReference(Subject.EntityLogicalName, subjectGuid);
        service.Create(a);

Upvotes: 0

Related Questions