N.Hesketh N.Hesketh - 16 days ago 5x
Java Question

Podio API - Java - Attaching files to existing items

I've been searching high and low for any information on this but I haven't found anything useful so far. I'm relatively new to the podio API and I'm trying to upload/attach a file I create in my program to a specific item in a specific app. I've successfully uploaded the file to the API and I've added it to the specific item's list of files however it doesn't 'stick'/update to have any permanence. At the end of the method I can call getFiles() and the file will be there however the file is not visible on podio and if i call getFiles() any other time it won't be there. Here's my code:

public static void uploadTimeSheet(File timeSheet){
FileAPI fileApi = apiFactory.getAPI(FileAPI.class);
int fileId = fileApi.uploadFile(timeSheet.getName(), timeSheet);
com.podio.file.File timeSheetUploaded = fileApi.getFile(fileId);
List<com.podio.file.File> files = new ArrayList<com.podio.file.File>();
files = userItem.getFiles();

userItem is the specific Item I want to upload the file to. I'm by no means an experienced coder but it seems to me that by calling setFiles(files) i only update a local version of the item userItem? If that's the case then how can I update podio's version of userItem with my own? And if i completely barking up the wrong tree where have I gone wrong? I've been searching for methods which update items but they only seems to be for updating fields/field values.

Thanks in advance!


Attach file method is not available in Java SDK unfortunately. You can use other SDKs such as ruby

 # Attach a file to an existing reference
    # @see https://developers.podio.com/doc/files/attach-file-22518
    def attach(id, ref_type, ref_id)
      Podio.connection.post do |req|
        req.url "/file/#{id}/attach"
        req.body = {:ref_type => ref_type, :ref_id => ref_id}

If you really wanna work in JAVA SDK, I will update JAVA SDK corresponding method or there is a work around you can use updateItem to update a file to an existing item.

public void updateItemFieldValues(int itemId, int fieldId,
            List<Map<String, Object>> values, boolean silent, boolean hook) {
                .getApiResource("/item/" + itemId + "/value/" + fieldId)
                .queryParam("silent", silent ? "1" : "0")
                .queryParam("hook", hook ? "1" : "0")
                .entity(values, MediaType.APPLICATION_JSON_TYPE).put();

the above the file ID have to be derived from the below method enter image description here

Let me know it helps.