вторник, 15 апреля 2014 г.

Mass export of CRM Content from Libraries


Exporting documents from Salesforce libraries can be done in different ways:

  • Weekly Export Service - standard service for exporting data. That’s and easy solution, but It may take much time for preparing and downloading all content.
  • FileExporter - script, based on Data Loader for loading content. Good enough, but it doesn work with any version after 23.

Sometimes you may need a more custom solution to satisfy these requirements:
  1. Export all documents from a specific library
  2. Include all versions of the documents adding ‘-vN’ in the end of their titles, where N is a document verision.

This solution can be implemented using Eclipse IDE. So, here are the steps for that:
  1. Download Eclipse IDE
  2. Create new Java project in Eclipse and include generated from WSDL jar and wsc-NN.jar and files in it.
  3. Follow the code below.  

Development for Salesforce is not my primary duty (I'm a Salesforce admin), so the code below can be not so perfect as I'd like it to be :-)

Do not forget to replace values in USERNAME, PASSWORD and LIBRARY_ID constants.


 package wsc;  
 import java.io.BufferedOutputStream;  
 import java.io.File;  
 import java.io.FileOutputStream;  
 import java.util.ArrayList;  
 import java.util.Arrays;  
 import com.sforce.soap.enterprise.Connector;  
 import com.sforce.soap.enterprise.EnterpriseConnection;  
 import com.sforce.soap.enterprise.QueryResult;  
 import com.sforce.soap.enterprise.sobject.ContentVersion;  
 import com.sforce.soap.enterprise.sobject.ContentWorkspaceDoc;  
 import com.sforce.soap.enterprise.sobject.SObject;  
 import com.sforce.ws.ConnectionException;  
 import com.sforce.ws.ConnectorConfig;  
 public class Main {  
       static final String USERNAME = "USERNAME";   
       static final String PASSWORD = "PASSWORD+TOKEN";   
       static final String LIBRARY_ID = "LIBRARY/WORKSPACE ID";   
       static EnterpriseConnection connection;  
       public static void main(String[] args) {  
           ConnectorConfig config = new ConnectorConfig();  
           config.setUsername(USERNAME);  
           config.setPassword(PASSWORD);  
           try {  
                connection = Connector.newConnection(config);  
                // display some current settings  
                System.out.println("Auth EndPoint: " + config.getAuthEndpoint());  
                System.out.println("Service EndPoint: "  
                          + config.getServiceEndpoint());  
                System.out.println("Username: " + config.getUsername());  
                System.out.println("SessionId: " + config.getSessionId());  
                System.out.println(System.getProperty("user.dir"));  
                // run the different examples  
                downloadContent();  
           } catch (ConnectionException e1) {  
                e1.printStackTrace();  
           }  
      }  
      // returns list of Document IDs from a library  
      private static ArrayList<String> getListOfDocuments(String libraryId) {  
           ArrayList<String> documentIDs = new ArrayList<String>();  
           try {  
                // pulling document IDs from ContentWorkspaceDoc  
                QueryResult queryDocumentIDs = connection  
                          .query("select ContentDocumentId "  
                                    + " from ContentWorkspaceDoc where ContentWorkspaceId = '"  
                                    + LIBRARY_ID + "'");  
                ContentWorkspaceDoc[] cwd = Arrays.copyOf(queryDocumentIDs.getRecords(),   
                                                                       queryDocumentIDs.getSize(),   
                                                                       ContentWorkspaceDoc[].class);  
                if (cwd != null) {  
                     for (int i = 0; i < cwd.length; i++) {  
                          if (cwd[i].getContentDocumentId() != null) {  
                               documentIDs.add(cwd[i].getContentDocumentId());  
                          }  
                     }  
                }  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
           return documentIDs;  
      }  
      private static void downloadContent() {  
           try {  
                ArrayList<String> errorIDs = new ArrayList<String>();  
                // query for pulling all versions of a document  
                QueryResult queryAllVersions = new QueryResult();  
                // retrieving list of all documents from a library  
                ArrayList<String> documentIds = getListOfDocuments(LIBRARY_ID);  
                if (documentIds.size() != 0) {  
                     for (int j = 0; j < documentIds.size(); j++) {  
                          // get version of a document  
                          queryAllVersions = connection  
                                    .query("select Id "  
                                              + " from ContentVersion where ContentDocumentId = '"  
                                              + documentIds.get(j) + "'");  
                          SObject[] recordVersions = queryAllVersions.getRecords();  
                          ArrayList<String> versionIDs = new ArrayList<>();  
                          if (recordVersions != null) {  
                               for (int i = 0; i < recordVersions.length; i++) {  
                                    if (recordVersions[i].getId() != null) {  
                                         versionIDs.add(recordVersions[i].getId());  
                                    }  
                               }  
                          }  
                          QueryResult queryVersions;  
                          ContentVersion contentVersion = new ContentVersion();  
                          //exporting each version of document  
                          for (String s : versionIDs) {  
                               queryVersions = connection  
                                         .query("select Id, Title, ContentDocumentId, "  
                                                   + "ContentSize, FileType, VersionData, IsLatest, VersionNumber, "  
                                                   + "PathOnClient from ContentVersion where "  
                                                   + "Id = '" + s + "'");  
                               SObject[] records = queryVersions.getRecords();  
                               if (records != null) {  
                                    for (int i = 0; i < records.length; i++) {  
                                         contentVersion = (ContentVersion) records[i];  
                                         if (contentVersion.getContentSize() != 0) {  
                                              System.out  
                                                        .println("Downloading Version Id:"  
                                                                  + contentVersion.getId()  
                                                                  + " Title:"  
                                                                  + contentVersion.getTitle()  
                                                                  + " ContentSize:"  
                                                                  + contentVersion  
                                                                            .getContentSize());  
                                              int lastDot = contentVersion  
                                                        .getPathOnClient().lastIndexOf('.');  
                                              try {  
                                                   contentVersion  
                                                             .setPathOnClient(contentVersion  
                                                                       .getPathOnClient()  
                                                                       .replaceAll("\\\\", ""));  
                                                   File outputFile = new File(  
                                                             "D://LoadedContent//"  
                                                                       + contentVersion  
                                                                                 .getTitle()  
                                                                       + "-v"  
                                                                       + contentVersion  
                                                                                 .getVersionNumber()  
                                                                       + contentVersion  
                                                                                 .getPathOnClient()  
                                                                                 .substring(  
                                                                                           lastDot));  
                                                   BufferedOutputStream bos = new BufferedOutputStream(  
                                                             new FileOutputStream(outputFile));  
                                                   bos.write(contentVersion  
                                                             .getVersionData());  
                                                   bos.flush();  
                                                   bos.close();  
                                              } catch (Exception e) {  
                                                   errorIDs.add(contentVersion  
                                                             .getContentDocumentId()  
                                                             .toString());  
                                              }  
                                         }  
                                    }  
                               }  
                          }  
                          System.out.println("Export completed");  
                          System.out.println("Document IDs with errors: ");  
                          if (errorIDs.size() != 0) {  
                               for (String s : errorIDs) {  
                                    System.out.println(s);  
                               }  
                          } else {  
                               System.out.println('0');  
                          }  
                     }  
                }  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  
 }