It worked until we activated Metadata navigation and filtering feature and added Metadata filter to this document library. After that code above started to throw exception:
Unexpected response from server. The status code of response is '403'. The status text of response is 'FORBIDDEN'
In order to avoid the problem we had to disable metadata filter for this document library, i.e. in doclib settings > Metadata navigation settings remove all fields from filter list: