Publishing articles through an API

Mike Hashemi
Kilo Sage

I am looking for a way to either create articles in a "published" state, or trigger the "publish" workflow through the REST API.

 

I wrote a little PowerShell snippet that is creating the articles in "draft":

 

$serviceNowCredential = Get-Credential
$uri = "https://<instance>.service-now.com/api/now/table/kb_knowledge"
$method = "POST"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $ServiceNowCredential.UserName, $serviceNowCredential.GetNetworkCredential().Password)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization', ('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept', 'application/json')
$headers.Add('content-type', 'application/json')

$tempObj = [PsCustomObject]@{
    text              = 'Some stuff goes here' 
    sys_created_by    = 'user@emaildomain.com'
    author            = 'e9326af747d9611091271c42846d43bd'
    active            = 'true'
    kb_knowledge_base = 'cf33206447e5a11091271c42846d43ac'
    topic             = 'General'
    short_description = 'How to do a Thing'
    article_type      = 'text'
    kb_category       = 'a196b8a44729a11091271c42846d438c'
}

$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body ($tempObj | ConvertTo-Json) -ErrorAction Stop

 That works just fine, but I don't see any way to publish the resulting article. I found How to create knowledge article in ServiceNow with article state as Publish?, but the answer just says, "go look at the REST API Explorer".

 

I did that, which made me think to try a PUT with "scheduled_publish_date", then "published" with today's date (2023-03-14), but neither helped. Is there another field, at which I should be looking? What am I missing?

2 REPLIES 2

Mike Van Vooren
Kilo Guru

I do not have experience doing this from API.  But I too have come across this issue where the system doesn't like to skip the Draft state.  We ultimately have things happening in 2 parts.  First, the system automatically creates the the new article in Draft.  Second, a job runs (every 60 min) that looks for articles sitting in Draft that were created in this specific way and Publishes them.  We had to make sure the job only Published the Drafts we wanted and not other user's Drafts that they were working on.

I'm not above trying that, but I don't have any experience with jobs (we are currently working on migrating to ServiceNow). Is there any way to show me how your job is configured?