Manage Knowledge Articles Using Apex

Create new article:

Knowledge__kav ka = new Knowledge__kav();
ka.Title = 'Salesforce CRM';
ka.UrlName = 'salesforce-crm';
ka.Summary = 'Salesforce Cloud CRM';
ka.Language = 'en_US';
insert ka;

Publish a draft article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);

Unpublish a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);

Schedule archive of a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
Datetime scheduledDate = System.now().addMonths(2); //Add date to schedule the archive
KbManagement.PublishingService.archiveOnlineArticle(knowledgeArticleId, scheduledDate);

Note: If the specified scheduledDate is null, the article is archived immediately.

Cancel a scheduled archive published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.cancelScheduledArchivingOfArticle(knowledgeArticleId);

Delete an archived article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.deleteArchivedArticle(knowledgeArticleId);

Delete a draft article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.deleteDraftArticle(knowledgeArticleId);

Create a draft article from the archived article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.editArchivedArticle(knowledgeArticleId);

Unpublish a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.editOnlineArticle(knowledgeArticleId, true);