You need to sign in to do that
Don't have an account?
martin_wu
How to upload files to Content via API using Java client
Hi all,
I have tried to find some sample code on developer.force.com for how to upload to Content via API, unsuccessfully unfortunately. I would basically like to automate the batch-upload of a set of files, creating a new item in Content for each file.
Does anyone have a link with some sample code in Java that I could use to get started?
Kind regards,
Martin
Hi Martin,
Here's some sample code to upload a file to Content via the SOAP API and display the resulting object id - you can check its operation by browsing to https://na1.salesforce.com/<id> - replace na1 with your Salesforce instance.
The sample uses the Java WSC - you'll need to download wsc-20.jar and generate enterprise.jar as described in the article Introduction to the Force.com Web Services Connector
If you run the code in a dev org, you'll need to enable Content for the org and your user:
Setup > Customize > Salesforce CRM Content > Settings - turn on
Setup > Manage Users > Users > (select your user) - turn on 'Salesforce CRM Content User'
You can upload more than one file in the array, but bear in mind the maximum SOAP message size of 50MB.
Cheers,
Pat
All Answers
Hi Martin,
Here's some sample code to upload a file to Content via the SOAP API and display the resulting object id - you can check its operation by browsing to https://na1.salesforce.com/<id> - replace na1 with your Salesforce instance.
The sample uses the Java WSC - you'll need to download wsc-20.jar and generate enterprise.jar as described in the article Introduction to the Force.com Web Services Connector
If you run the code in a dev org, you'll need to enable Content for the org and your user:
Setup > Customize > Salesforce CRM Content > Settings - turn on
Setup > Manage Users > Users > (select your user) - turn on 'Salesforce CRM Content User'
You can upload more than one file in the array, but bear in mind the maximum SOAP message size of 50MB.
Cheers,
Pat
...and I just noticed that you'd posted the question in the REST forum, but I supplied a SOAP answer... The perils of following the discussion boards via RSS!
Let me see if I can recast my solution in REST.
Here is equivalent functionality in Java using the REST API - the loadFileAsByteArray() method is the same as in the previous example.
I've used Apache HttpClient and the JSON.org Java library - you may need to adapt the code slightly to match your environment, but it shows the general approach.
This is brillient, thanks so much, Pat. The code for the SOAP access is perfectly. I don't mind which protocol I use, as long as it works.
Thanks a million for your help. I'll give it a go now.
Cheers,
Martin
You're welcome, Martin. Just to close the loop - here's the link to the docs - there's a bit more detail there.
Thanks Pat for the REST API version. How do we set custom fields of Content? I tried to set following and then the contentversion insert fails. What wrong am I doing here?
What is the error on the insert? I would double check the field name - maybe it's AccountId__c?
And the response is 400 / Bad request.
What is the body of the response? That usually carries more data.
[{"fields":[],"message":"You cannot set custom fields or tags on a document published into a personal library. Fields set: Account","errorCode":"FIELD_INTEGRITY_EXCEPTION"}]
Thank you once again Pat!
content.put("FirstPublishLocationId", "05890000000Hdl5AAC");
content.put("Account__c", "0019000000Gtw0IAAR");
Can you help me in Rest API file creation code?
I am creating files with size more than 500mb. I got java.lang.OutOfMemoryError: Java heap space error. I can not increase virtual memory size. Can I send file in chunks to salesforce or some other way to avoid this situation?
Please help me in this.
Thanks,
I tried using your piece of code but i cannnot transfer any file which is above 52 MB. I thought the REST api file transfer limit is 2GB but it gives this error:
"message" : "; nested exception is: \n\tcommon.exception.ApiException: Maximum size of request reached. Maximum size of request is 52428800 bytes.",
"errorCode" : "JSON_PARSER_ERROR"
I tried also using use a multipart message using MultipartRequestEntity but that results in this error:
"message" : "multipart form upload failed because field 'json' exceeds max length 10485760",
"errorCode" : "INVALID_MULTIPART_REQUEST"
Please let me know if there is a workaroud for this size issue.
Thanks
Loads of docs about how to create a ContentVersion.
Please update.
Does anyone have a link with some sample code in .NET(c#) that I could use to get started?
Thanks
Vishakha