How can I create page in confluence?

I manage to create a draft and publish it, but I want to publish the page right away without creating a draft and I get an error : Attempted to create a page that needs to go to the editor.

	CreateBlueprintPageRestEntity entity = new CreateBlueprintPageRestEntity(
				params.get("typeSelect").toString(),
				this.getAnnounceBlueprint(params).getId().toString(),
				"",
				"",
				params.get("departmentSelect") + ". " + params.get("subject").toString(),
				"",
				0,
				"",
				this.getContext(params)
		);

		BlueprintPage content1 = this.contentBlueprintService.createPage(
				entity,
				AuthenticatedUserThreadLocal.get()
		);


		String baseUrl = this.settingsManager.getGlobalSettings().getBaseUrl();

		PageEntity blpentity = new PageEntity(content1, baseUrl);


		return blpentity.getUrl();```

Hi Alex,

Do you use Script Runner for Confluence? If so, you could you the following:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.confluence.pages.templates.PageTemplateManager
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.confluence.pages.Page
import com.atlassian.sal.api.component.ComponentLocator

def pageManager = ComponentLocator.getComponent(PageManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageTemplateManager =ComponentLocator.getComponent(PageTemplateManager)

def parentPage = pageManager.getPage(ID OF PARENT PAGE)

def space = parentPage.getSpace()

def template1 = pageTemplateManager.getPageTemplate(ID OF TEMPLATE).getContent()

def new_page = new Page(title: “This is the title” , bodyAsString: template1 ,space: space, parentPage: parentPage)
pageManager.saveContentEntity(new_page, DefaultSaveContext.DEFAULT)
parentPage.addChild(new_page)
pageManager.saveContentEntity(new_page, DefaultSaveContext.MINOR_EDIT)

I wrote it based on this code, but the following error occurred when collaborative editing was opened. It is normal to close collaborative editing.

Object of class [com.atlassian.confluence.pages.Page] with identifier [1638420]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.atlassian.confluence.pages.Page#1638420]

I don’t know how to deal with it

I wrote it based on this code, but the following error occurred when collaborative editing was opened. It is normal to close collaborative editing.

Object of class [com.atlassian.confluence.pages.Page] with identifier [1638418]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.atlassian.confluence.pages.Page#1638418]

Hi @hehongqi @Steven2 ,

please try to insert the code into the transaction

transactionTemplate.execute(new TransactionCallback<Object>() {
    @Override
    public Object doInTransaction() {
        //your code
    }
});

Cheers
Adam

1 Like

Your method solved my problem, thank you!