Hi,
I have multiple documents to upload and some of them have duplicate names. I followed this instructions to enable duplicate title names, however I don’t know how to set the Permalink when creating a content through the rest api.
Anyone could give me an example, please?
Thanks
So for whoever has the same problem, this was how I approached:
This will make sure the permalink is unique and the title is the one you want.
Here's the code in nodejs:
async function createContent(title, ancestorId) {
console.log(`Creating content ${title} in confluence...`);
var pageWithAttachmentMacro = { "type":"page", "ancestors": [{ "type":"page", "id": ancestorId }], "title": title, "space": { "key": space }, "body": { "storage": { "value":'<ac:structured-macro ac:name=\"attachments\"> <\/ac:structured-macro>', "representation": "storage" } } };
try {
const response = await postContent(pageWithAttachmentMacro);
return response.data.id;
} catch (error) {
if (error.response) {
if (error.response.data.statusCode == 400 && error.response.data.message.includes("A page with this title already exists")) {
let newTitle = title + (+new Date).toString(36).slice(-5);
let id = await createContent(newTitle, ancestorId);
await changeScrollPageTitle(id, title);
return id;
}
}
throw error;
}
}
async function postContent(content) {
return await axios({
method: 'post',
url: `${confluenceRootUri}/rest/api/content`,
headers: {
'Content-Type': 'application/json; charset=utf-8',
"Authorization": "Basic " + Buffer.from(username + ":" + password).toString('base64')
},
data: content
});
}
async function changeScrollPageTitle(contentId, pageTitle) {
await axios({
method: 'put',
url: `${confluenceRootUri}/rest/scroll-versions/1.0/metadata/scroll-page-title/${contentId}`,
headers: {
'Content-Type': 'application/json; charset=utf-8',
"Authorization": "Basic " + Buffer.from(username + ":" + password).toString('base64')
},
data: { 'scrollPageTitle': pageTitle }
});
}
So for whoever has the same problem, this was how I approached:
This will make sure the permalink is unique and the title is the one you want.
Here's the code in nodejs:
async function createContent(title, ancestorId) {
console.log(`Creating content ${title} in confluence...`);
var pageWithAttachmentMacro = { "type":"page", "ancestors": [{ "type":"page", "id": ancestorId }], "title": title, "space": { "key": space }, "body": { "storage": { "value":'<ac:structured-macro ac:name=\"attachments\"> <\/ac:structured-macro>', "representation": "storage" } } };
try {
const response = await postContent(pageWithAttachmentMacro);
return response.data.id;
} catch (error) {
if (error.response) {
if (error.response.data.statusCode == 400 && error.response.data.message.includes("A page with this title already exists")) {
let newTitle = title + (+new Date).toString(36).slice(-5);
let id = await createContent(newTitle, ancestorId);
await changeScrollPageTitle(id, title);
return id;
}
}
throw error;
}
}
async function postContent(content) {
return await axios({
method: 'post',
url: `${confluenceRootUri}/rest/api/content`,
headers: {
'Content-Type': 'application/json; charset=utf-8',
"Authorization": "Basic " + Buffer.from(username + ":" + password).toString('base64')
},
data: content
});
}
async function changeScrollPageTitle(contentId, pageTitle) {
await axios({
method: 'put',
url: `${confluenceRootUri}/rest/scroll-versions/1.0/metadata/scroll-page-title/${contentId}`,
headers: {
'Content-Type': 'application/json; charset=utf-8',
"Authorization": "Basic " + Buffer.from(username + ":" + password).toString('base64')
},
data: { 'scrollPageTitle': pageTitle }
});
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.