Sunday, April 11, 2010

Using ContentVersion in Apex

I saw a post today on the Visualforce boards that made me remember that I was going to post about the programmatic use of the ContentVersion object to upload Content to Salesforce.

One of the first considerations should be whether you are intending to use public workspaces to house your Content. This is important because if the User who attempts to upload using the module you build does not have access to the workspace you bind the function to, they will receive inelegant error messages (like our favorite, Insufficient Privileges - assured to engender feelings of inadequacy).

You'll also want to explore Content Types if you want the Content to be tied to one of your Custom Object instances. This will allow you to add Custom Fields for lookup to your Custom Object, respectively bound to a particular object by content type.

So the pattern is this and I'll add some sample code in a later post.

1. Define a workspace and manage the permissions appropriately

2. Define your ContentTypes to allow relationship of content to your context

3. Write an Apex Controller that inserts the content in a page action method. (note you need version 17 or later to use Content Version [I use 18]).

4. Expose the functionality in a VF page using your context object as the standard controller for the page. Use inputFile and bind the content using versionData. Same deal with the API version as above

5. Add a Custom button (detail page button) that uses the VF page

6. Profit


Note that you'll see a performance gain because you are uploading natively rather than through a Flex wrapper thr uses the SOAP API.


- Posted using BlogPress from my iPhone

1 comment:

Glenn said...

Does anyone know how to create or edit a workspace programatically in Apex? From what I can see it's not possible. Would be great if it was!