How to publish a new document from management api?

Hi there,

I’m trying to create a document from the management api and publish it. How can I do it.

I’ll post my code here, just for reference (I tried to replicate the steps that Umbraco does within it’s own CMS, but sadly it does not work for me):

var jsonDocumentContent = JsonContent.Create(documentRequestModel);
var apiDocumentResponse = await client.PostAsync($"{host}/umbraco/management/api/v1/document", jsonDocumentContent);
var apiDocumentResponseText = apiDocumentResponse.Content.ReadAsStringAsync().Result;

if (!apiDocumentResponse.IsSuccessStatusCode)
{
    logger.LogWarning("content item could not be created, due to { Reason}. Title: { Title}", apiDocumentResponseText, migrationObject.Title);
    continue;
}

var validateDocumentResponse = await client.PutAsync($"{host}/umbraco/management/api/v1.1/document/{documentId}/validate", jsonDocumentContent);
var validateDocumentResponseText = apiDocumentResponse.Content.ReadAsStringAsync().Result;

if (!validateDocumentResponse.IsSuccessStatusCode)
{
    logger.LogWarning("content item could not be created, due to { Reason}. Title: { Title}", validateDocumentResponseText, migrationObject.Title);
    continue;
}

var cultureAndScheduleRequestModel = new PublishDocumentRequestModel
{
    PublishSchedules = []
};

var jsonCultureAndScheduleContent = JsonContent.Create(cultureAndScheduleRequestModel);
var documentPublishResponse = await client.PutAsync($"{host}/umbraco/management/api/v1/document/{documentId}/publish", jsonCultureAndScheduleContent);
var documentPublishResponseText = documentPublishResponse.Content.ReadAsStringAsync().Result;

if (!documentPublishResponse.IsSuccessStatusCode)
{
    logger.LogWarning("content item could not be published, due to { Reason}. Title: { Title}", documentPublishResponseText, migrationObject.Title);
    continue;
}

var documentPublishedResponse = await client.GetAsync($"{host}/umbraco/management/api/v1/document/{documentId}/published");
var documentPublishedResponseText = documentPublishResponse.Content.ReadAsStringAsync().Result;

if (!documentPublishResponse.IsSuccessStatusCode)
{
    logger.LogWarning("content item could not be published, due to { Reason}. Title: { Title}", documentPublishedResponseText, migrationObject.Title);
    continue;
}

Just as a heads-up: the document is created successfully, the validate function returns a 200 response and the put request to publish the document returns a 200 response. Only the “published” endpoint I’m running at the end returns a 404 as the “document could not be found” even though it should be published according to the response of the management api endpoint “{host}/umbraco/management/api/v1/document/{documentId}/publish”

Anyone able to help me or willing to help me fix this?

Thanks in advance.

I would suggest you do it manually via backoffice and replicate the json payload as you did, its much easier that way.

You can just create a simple document like this (TS)

 async createDocument({
    parentId,
    documentTypeId,
    name,
    culture = "en-US",
  }: {
    parentId: string;
    documentTypeId: string;
    name: string;
    culture?: string;
  }) {
    const token = await this.getToken();
    const id = uuidv4();
    const payload = {
      id,
      parent: { id: parentId },
      documentType: { id: documentTypeId },
      template: null,
      values: [],
      variants: [
        {
          culture,
          segment: null,
          state: null,
          name,
          publishDate: null,
          createDate: null,
          updateDate: null,
          scheduledPublishDate: null,
          scheduledUnpublishDate: null,
        },
      ],
    };
    const response = await fetch(`${host}/umbraco/management/api/v1/document`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(payload),
    });
    if (!response.ok) throw new Error("Failed to create document");
    return //whatever
  }

  async publishDocument(id: string, culture = "en-US") {
    const token = await this.getToken();
    const payload = {
      publishSchedules: [{ culture }],
    };
    const response = await fetch(`${host}/umbraco/management/api/v1/document/${id}/publish`, {
      method: "PUT",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify(payload),
    });
    if (!response.ok) throw new Error("Failed to publish document");
    const text = await response.text();
    return text ? JSON.parse(text) : {};
  }

it might be some issue with your jsonDocumentContent.

Good suggestion, but I have everything setup correctly. The thing I found out is that when manually saving or publishing the rowspan and columnspan values are set to 1 and 12 respectively. Weird thing is while creating the layouts I set every layout to have rowspan and columnspan values. It seems that the management api somehow ignores those values or is not parsing them accordingly.

I think I’m going to create a bug on the github page of Umbraco.

Link to the Github issue I created: Unable to create publishable blockgrid content via management api · Issue #19911 · umbraco/Umbraco-CMS · GitHub