How to add or remove page restrictions Python
Asked by gurgen2727:

Hi

I couldn't find anywhere how I can add or remove page restrictions for user and group by using Python ?

It can be any API( xmlrpc or REST API), just help me to do that ?

potentially I just need to remove all permission for everybody and then add only for one user


Answers:
author: Stephen Deutsch
body: Hi Gurgen,

You can see the XMLRPC documentation for setting permissions here, specifically setContentPermissions:

<https://developer.atlassian.com/server/confluence/remote-confluence-methods/#permissions>

If you search for some further XMLRPC examples, they shouldn't be too hard to find.

comments: author: Stephen Deutsch
body: And in case someone is curious about the Cloud version, you can already set page permissions via REST:

<https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-restriction-put>


author: gurgen2727
body: Hi Stephen

Thank you very much.

Is there a way to do this by REST API ? seems that feature is in experimental API. I found one way, but that is still in 'experimental' s

```
PUT https://confluence/rest/experimental/content/<id>/restriction/byOperation/read/user?userName=billgates
```

So I am worried whether I can use xmlrpc deprecated method for customers. How long this is going to be supported in XMLRPC ?


author: AnnWorley
body: Hi Gurgen,

I don't know how to remove restrictions using the Python with the REST API. Based on the [Confluence REST API Documentation](https://docs.atlassian.com/atlassian-confluence/REST/6.5.2/#content/{id}/restriction) it looks like the API will let you GET the restrictions but does not provide a way to update them.

Please consider SQL as an alternative, as described in: [How to remove all restrictions from a specific page via the database](https://confluence.atlassian.com/confkb/how-to-remove-all-restrictions-from-a-specific-page-via-the-database-834207785.html).

Thanks,

Ann

comments: author: gurgen2727
body: Thanks I have found that, but I don't need DB.

any suggestion, by using xmlrpc ? anyway ? if you provide me API I can write Python code myself. I need to do that in Python.


author: fwoon
body: most prob you can update the permissions and that should remove whatever you needed.

you can omit the group or user if you don't require to update either one of them

```
url = 'https://your-confluence.com/rest/experimental/content/{id}/restriction'
headers = { "Accept": "application/json", "Content-Type": "application/json" }
payload = json.dumps( [
{
"operation": "update",
"restrictions": {
"user": [
{ "type": "known", 
"username": "<string>",
"userKey": "<string>",
"accountId": "<string>"
}],
"group": [
{
"type": "<string>",
"name": "<string>",
}]
}
}
] )

r = requests.put( url, verify=False, data=payload, headers=headers, auth=(username, passwd) )
```

Take a look at [https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-restriction-put](https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-restriction-put "cloud rest api") for reference...try to do a get permission on the page first to understand what kinda operation there is...do not use administer as the operation...that's for cloud


author: John_Huang
body: This one doesn't work either.


author: Helen Song
body: [@fwoon](/t5/user/viewprofilepage/user-id/247480) [@John_Huang](/t5/user/viewprofilepage/user-id/116565) have you execute it successfully or not?


author: fwoon
body: [@Helen Song](/t5/user/viewprofilepage/user-id/4189763)

Yes i most definitely can. Just verified it on 7.4.1 Confluence Server.

Take note that this is a page restriction update, not space permission restriction.

Again to reiterate, make sure you know the group name if you are adding the group, if it's an individual, make sure you get the userKey, username, displayName. If you don't know, simply do a get on a page that has that user/group on the page restrictions

```
url = BASE_URL + pageId + PERM_URL
headers = { "X-Atlassian-Token": "nocheck" }
payload = {
"expand" : "update.restrictions.user,read.restrictions.group,read.restrictions.user,update.restrictions.group"
}
r = requests.get(url, verify=False, headers=headers, params=payload, auth=(username, passwd))

#Check for successful call 
if r.status_code == 200:
print r.text

```


author: Helen Song
body: Thank you very much.

Do you know how to lock the page by groovy or REST API?


author: Helen Song
body: It looks like that is method of update read permission, is it possible to lock one page edit permission but keep all read permission as before?