Monday, January 31, 2011

Access Salesforce Content on your VF Page


Recently I came across the Salesforce Content Related requirement, Salesforce only provide limited access to the content (ContentVersion object). It does not allow you to access the Content & show in your custom VF page.
Here Me & my friend Vinod did a lot of Rnd & found a easy solution. This post will save your lot of time if you are doing this kind of task with Content.

To implement its example, take a div
<div class="centerContent" id="myDiv"></div>
There are two simple JS function which will help to get salesforce content –

function OpenDoc(docId){
 
var htmlString = '<div id="chatterFileViewerPanel" class="chatterFileViewerPanel"><br/><br/> <embed height="500px" align="middle" width="500px" type="application/x-shockwave-flash" wmode="transparent" pluginspage="http://www.adobe.com/go/getflashplayer" allowfullscreen="true" allowscriptaccess="sameDomain" ' +
'name="renditionLarge" bgcolor="#f3f3f3" quality="high" id="renditionLarge" '
+'flashvars="shepherd_prefix=/sfc/servlet.shepherd&amp;v='+docId+'&amp;mode=chatterfilepreview&amp;in_tests=false" src="/static/101210/sfc/flex/DocViewer.swf" /> </div> ';
           
        document.getElementById('myDiv').innerHTML =  htmlString;
        PrepareFlexComponent(docId);
       
       
}

function PrepareFlexComponent(docId){
        insertFlexComponent('/static/102010/sfc/flex/DocViewer',
                'shepherd_prefix=/sfc/servlet.shepherd&v='+docId+'&mode=chatterfilepreview&
                amp;in_tests=false','500px', '500px', '#f3f3f3', 'chatterFileViewerPanel', 'renditionLarge', false,
                { adobeFlashVersionLbl : 'You must enable or download Adobe Flash Player version 9.0.115 or 
                   later to use this feature.',
                   downloadAdobeLbl : 'Download Adobe Flash Player', downloadAdobeLinkLbl : 'Click the 
                   link below to download the Adobe Flash Player:' closeDialogLbl : 'Cancel'});
                   
        Ninja.Flex.ContentStaticResourcesBase = '/static/102010/sfc';
        Ninja.Flex.ShepherdController = '/sfc/servlet.shepherd';
}  



Put below script as it is in your page


<script type="text/javascript" src="/static/101210/js/functions.js"></script>
<script  src="/EXT/ext-3.0.0/ext.js" type="text/javascript"></script>
<script  src="/jslibrary/1294273133000/main.js" type="text/javascript"></script>
<script  src="/jslibrary/1289945557000/Chatter.js" type="text/javascript"></script>
<script  src="/jslibrary/1289346548000/ChatterFiles.js" type="text/javascript"></script>
<script  src="/jslibrary/labels/1295420058000/en_US.js" type="text/javascript"></script>
<script type="text/javascript" src="/static/101210/desktop/desktopAjax.js"></script>
             
<script type="text/javascript" src="/static/101210/sfc/javascript/lib/AC_OETags.js"></script>
 
<script>

   function postSubDocument(url, internal) {
      var form = document.getElementById("formSubDocument");
      form.action = url;
      form.internal.value = internal;
      form.submit();
    }
             
    function hideFlex() {
        document.getElementById('IE6Confirm').style.display = "block";
    }
                    
    function showFlex() {
        document.getElementById('IE6Confirm').style.display = "none";
    }
                    
    function cancelDownload(url) {
        showFlex();
    }
                    
    function triggerDownload(url) {
        window.open(url);
    }
                    
    function downloadDocForIE6() {
        showFlex();
        triggerDownload(_url);
    }
                    
    function downloadDoc(url) {
        var isIE6 = (navigator.userAgent.indexOf("MSIE 6") != -1);
        if (isIE6) {
            _url = url;
            hideFlex();
        }
        else {
           triggerDownload(url);
        }
     }


  </script>

It’s done! Now call function OpenDoc by passing a ContentId & you can see your Content on VF page :)
It will show the content same as the salesforce standard way.
Enjoy..!!



18 comments:

  1. Great post, do you know how to enable the download button in the docviewer ?

    ReplyDelete
  2. I guess you have to publish the content for download button. I could not try it because content is not accessible in my account.
    let me know if you still facing the issue....

    ReplyDelete
  3. Hi Kapil, its a great post. I was in need of this.
    Do you have any ideas to hide/disable the sidebar on
    /sfc/#search and /sfc/#version pages.
    Thanks

    ReplyDelete
  4. Hi Praneeth,
    Seems there is no standard way to hide or disable sidebar on these pages.

    ReplyDelete
  5. Hi Kapil great post. I have created a custom related list for Content Version, Now I need to create a column which shows the preview of the document. I tried what ever u said above, I get the pop up - "Document Preview" but I am unable to show the contents within it. Could you please give me some input regarding this.
    Thank you.
    Vikram

    ReplyDelete
  6. Hi Vikram,
    This seems because of the new SF release. Please tell me which version your are using in dev org.
    I also need to look into the code to resolve the issue, please mail me two more things -
    HTML Source file on which you are having "Preview" button.
    Second thing is Source file of SF standard page in which you are able to see the content preview.
    Please mail me both source file at kapil.goutam@gmail.com.

    ReplyDelete
  7. Hi Kapil, Thanks for ur response, I have mailed u the required files.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. HI Kapil,great post. Actually i want to include a VF page in a dashboard, that dashboard contain some filter section and by selection of that filter criteria a list should refresh,that is inside the VF page , is it possible ? Because now when i am including that VF page in dashboard the filter got hide. could you please give me some workaround.
    Thank you
    Avantika

    ReplyDelete
  10. Is there a way to open document in full screen by default!
    Thanks

    ReplyDelete
    Replies
    1. Not sure about this, haven't tried it yet. I will see & let you know if I find anything on this.
      Thanks

      Delete
  11. Hi Kapil,
    Thanks for the sharing the details with us. Could you please let me know if we can use mode=chatterfilepreview with other mode instead of 'chatterfilepreview'.

    Thank You!
    Rohit Sharma

    ReplyDelete
  12. Hi Rohit, actually I did this few years back & I am not sure if salesforce did any other stuff with this, if it allow other mode or not. You need to check this because I am not having any org where Content is enable. Sorry for that.
    Just a suggestion, try to see in view source if you get something on standard page, you may get something there.

    Thank You!

    ReplyDelete
  13. Hi,
    I am getting error as ReferenceError: Ninja is not defined.
    Can you help me with this.

    Thank you :)

    ReplyDelete
  14. Ninja.Flex.ContentStaticResourcesBase = '/static/102010/sfc';
    Ninja.Flex.ShepherdController = '/sfc/servlet.shepherd';

    I have commented these 2 lines.
    Now it is working.

    Thank you very much for this post :)

    ReplyDelete