Programatically rename space home page title

yevhen_rudenko January 5, 2022

Hi there,

I would like to programatically rename space home page title (not sure if I call that stuff correctly, screen attached)

HomePage.jpg

I tried the following REST API:

PUT /rest/space/{spaceKey}

resr.jpg

When I send the following json as a request body then everything works fine, name and description are updated

{

    "name""Space name",

     "description": {

        "plain": {

            "value""New description",

            "representation""plain"

        }

     }

}

However, when I send a json like this

{

    "name""Space name",

     "description": {

        "plain": {

            "value""New description",

            "representation""plain"

        }

     },

     "homepage": {

        "title""New Home Page"

    }

}

I get the following response:

{

    "statusCode"400,

    "data": {

        "authorized"true,

        "valid"false,

        "allowedInReadOnlyMode"true,

        "errors": [

            {

                "message": {

                    "translation""Cannot update space homepage to page in space with key: TEST",

                    "args": []

                }

            }

        ],

        "successful"false

    },

    "message""Cannot update Space",

    "reason""Bad Request"

}

I understand that it is a bad request, but I can't figure out what exactly should be sent as a body to have space home page title updated?

Thanks,

Yevhen

 

1 answer

1 accepted

0 votes
Answer accepted
WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 6, 2022

I think what you're trying to do is change the Homepage selection on the space's Overview -> Edit space details screen, but through the REST API instead of the screen.  If that is correct, then here's what you do.

First, run the Get for the space to see what its details are, something like:

https://<myurl>/rest/api/space/KEY?expand=homepage

You'll see in the documentation that homepage is of type array, so it's not just the title.

Unfortunately, Confluence's documentation is not nearly as good as Jira's, and this particular piece lacks detail, such as what other fields are required to update the homepage field of a space, and there are a lot.  Here's what I got when I ran the Get (I put in a lot of fake values, but the fields are real):

"homepage":{
"id":"987654321"
,"type":"page"
,"status":"current"
,"title":"Test Space Home"
,"extensions":{"position":"none"}
,"_links": {
"webui":"/display/KEY/Test+Space+Home"
,"edit":"/pages/resumedraft.action?draftId=123456789"
,"tinyui":"/x/pYQoTw"
,"self":"https://myurl/rest/api/content/987654321"
}
,"_expandable":{
"container":"/rest/api/space/KEY"
,"metadata":""
,"operations":""
,"children":"/rest/api/content/987654321/child"
,"restrictions":"/rest/api/content/987654321/restriction/byOperation"
,"history":"/rest/api/content/987654321/history"
,"ancestors":""
,"body":""
,"version":""
,"descendants":"/rest/api/content/987654321/descendant"
,"space":"/rest/api/space/KEY"
}
}

 

And fortunately, I tested it, and it only needs the page Id to work.  I didn't test with the title, but feel free to.  So here's the Json you need to send (or something like it):

{
    "name": "Test Space Temp",
    "description": {
        "plain": {
            "value": "New description",
            "representation": "plain"
            }
    },
    "homepage": {
        "id": "11112222"
    }
}

Just put your homepage's id instead of 11112222.

You just needed a couple of little curly brackets, for the most part.

yevhen_rudenko January 7, 2022

@WW 

Thanks for your reply.

I checked that when I send 

{
     "homepage": {
        "id""11112222",
        "title""HomePageNewTitle"
    }
}
the server simply ignores title as it needs id only to identify the page to be set as the homepage.
I actually need to change only homepage title just like if to click Edit button
edit.png
Edit - HomePage.jpg
I assume there might be another REST endpoint  that I didn't manage to find yet.
WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2022

Ah, so that's what I originally thought your question was asking, but then I thought maybe you were asking how to change the homepage like from the Edit space details screen, since that's kind of tricky.

So, the problem you're having is that you're using the wrong REST API call.  You're trying to update the space details rather than the page information.  The homepage is just like any other page when it comes to editing its content, and any page can be the homepage.

You'd just need to use:

PUT /rest/api/content/{contentId}

instead of:

PUT /rest/space/{spaceKey}
Like yevhen_rudenko likes this
yevhen_rudenko January 7, 2022

@WW 

When manually click Edit button and then Update the very end of the url changes accordingly to new title (circles #1 and #2 on the screen) 

hp11.jpg

Then if refresh F5 then I get another update (circle #3) 

hp12.jpg

I tried then

PUT /rest/api/content/{contentId}

The json body looks like this

{
    "title""HomePageREST",
    "type":"page",
    "version": {
        "number":30
    }
}

 As a result I have the following

hp-rest.jpg

Now the url (circle #1) and whatever that section is (circle #3, which I don't really care about, but still...) have been changed, but the title (circle #2) has never changed.

I also checked if this title is a part of page body and it looks like it is not

hp-body.png

Probably missing a property or so, don't really know what I'm doing wrong.

Another thing is that this endpoint requires version, which must be equal to current version + 1, which leads to additional GET request but that is a different story

WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2022

Yes, you do have to put the next version in there, which I think is stupid, bc why would you put or want anything but current version +1, and I think it has to be +1, but yes, that's another story. :)

I ran the below, and it worked for me.  However, when I refreshed the page I renamed, it said it was in the trash.  So, I clicked on the page from the left navigation panel, and it was renamed, and the version had been updated. I ran it again, refreshed, and it gave me this page doesn't exist, maybe you want this other page.  It definitely works.

<baseurl>/rest/api/content/123456789

Json:

{"version":
{
"number":2,"minorEdit": true,"message": "Updated title"
}
,"title":"Test Stuff"
,"type":"page"}

("minorEdit": true is like unchecking the Notify watchers box in the UI)

So, I'm not sure why it's not working for you, but it should work.  Maybe try another browser or clear your cache?  Your pages might not be reindexing properly.  Maybe run a rebuild the index (after hours, and if you're a Confluence admin). 

It looks like the title is just not refreshing for you for some reason, but that the URL is changing and the page really is being renamed, it's just not displaying it.  I wonder if you run it, the wait a while, if it'll display correctly later on.

Well, I think I've answered the how to change the Homepage title question.  Now you may have a different question - why isn't the title change displaying.

yevhen_rudenko January 8, 2022

I've tried clearing cache, a different browser, Confluence Admin > Content Indexing and click "Rebuild". Well, none of them worked.

Then I sent a GET request and yes it returned updated page title. So the value is there but at the same time kind of not.

When I refresh the page the name of the tab in Chrome changes for a while (while loading the page) to updated title but then gets back to old title

hp-l.jpg

I then assumed something was wrong with the space.

And I tried on a different 'blank' space and everything  worked for it.

The thing is that the space I need to change title of the home page for is created by REST api of Scroll Versions for Confluence plugin. The plugin creates a new space based on another template space and that's  why I need to change the home page title as it seems there is no way to configure this while using the plugin. 

But again this seems to be a different question.

@WW thank you for your support

Like WW likes this
WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 10, 2022

That might have something to do with your problem.  Plugins are notorious for interfering with built-in processes.

I would press F12 on your keyboard / open your browser's Developer Console to see what's going on.   Especially look for something being sent when you see that name in the tab.

Good luck!

yevhen_rudenko February 18, 2022
Like WW likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events