Welcome to the new Box Support website. Check out all the details here on what’s changed.

Accessing Collaboration object from Python SDK?

Answered
New post

Comments

2 comments

  • nadaoneal

    I just attended the biweekly Developer Office Hours, and learned many exciting things. 

     

    ONE, the Collaboration object is already supported by the Python SDK, it's just not documented. You just have to search the github repo to see that Collaboration object support is defined here, and the add_collaborator function is defined in folder.py

     

    A usage example is in example.py

     

        root_folder = client.folder(folder_id='0')
        collab_folder = root_folder.create_subfolder('collab folder')
        try:
            print('Folder {0} created'.format(collab_folder.get()['name']))
            collaboration = collab_folder.add_collaborator('***email address removed for privacy***', CollaborationRole.VIEWER)
            print('Created a collaboration')

    TWO, even  is not sure how to use the make_request() function with all the parameters that the Collaboration object requires, but I will post that here once he gets back to me.

    0
    Comment actions Permalink
  • nadaoneal

    I had a relevant similar problem and wanted to share my code in case someone is struggling with something similar.

     

    A few weeks after we created all these collaborations, we wanted to cut off any that were still pending. (We wanted to cut these off because a deadline had passed, and we didn't want people to get the automated "final reminder" from Box to accept the collaboration invite.)

     

    This is hard in the python SDK because there isn't a method to list all existing collaborations on a folder, and there isn't a method to create a collaboration object from a collaboration ID, at least that I could find. 

     

    folder_id = 'nnnnnnnn'
    root_folder = client.folder(folder_id=folder_id)
    
    items = root_folder.get_items(limit=200, offset=0)
    
    for i,item in enumerate(items):
    	json_response = client.make_request(
    		'GET',
        	client.get_url('folders', item.id, 'collaborations'),
    	).json()
    
    	for collaboration in json_response['entries']:
    		if collaboration['status'] == "pending":
    			print "Folder: %s, %s, created by %s at %s" % (item.name, collaboration['status'], collaboration['created_by']['name'], collaboration['created_at'])
    			delete_me = Collaboration(client._session, object_id=collaboration['id'],response_object=collaboration)
    			delete_me.delete()

    The code above identifies all subfolders of folder nnnnnnnn, identifies all collaborations, and then prints and deletes any that are still pending. 

     

    In order to make delete_me = Collaboration(client._session, object_id=collaboration['id'],response_object=collaboration) work, you need from boxsdk.object.folder import Collaboration

     

    0
    Comment actions Permalink

Please sign in to leave a comment.