Newer Version Available

This content describes an older version of this product. View Latest

retrieve()

This call retrieves XML file representations of components in an organization.

Syntax

1AsyncResult = metadatabinding.retrieve(RetrieveRequest retrieveRequest)

Usage

Use this call to retrieve file representations of components in an organization.

Metadata API can deploy and retrieve up to 10,000 files or 400 MB at one time. If either of these limits is exceeded, the deployment or retrieval fails.

Note

In API version 31.0 and later, the process of making a retrieve() call has been simplified. You no longer have to call checkStatus() after a retrieve() call to obtain the status of the retrieve operation. Instead, make calls to checkRetrieveStatus() only. If the retrieve operation is in progress, call checkRetrieveStatus() again until the retrieve operation is completed. The checkStatus() call is still supported in versions API version 30.0 or earlier, but is not available in API version 31.0 and later.

For API version 31.0 or later, retrieve packaged or unpackaged components by using the following steps.

  1. Issue a retrieve() call to start the asynchronous retrieval. An AsyncResult object is returned. Note the value in the id field and use it for the next step.
  2. Issue a checkRetrieveStatus() call and pass in the id value from the AsyncResult object from the first step. Check the value of the done field of the returned RetrieveResult. If it is true, this means that the call is completed and proceed to the next step. Otherwise, repeat this step to call checkRetrieveStatus() again until the done field is true.
  3. Retrieve the zip file (zipFile field) and other desired fields from RetrieveResult that was returned by the final call to checkRetrieveStatus() in the previous step.

For API version 30.0 or earlier, retrieve packaged or unpackaged components by using the following steps.

  1. Issue a retrieve() call to start the asynchronous retrieval. An AsyncResult object is returned. If the call is completed, the done field contains true. Most often, the call is not completed quickly enough to be noted in the result. If it is completed, note the value in the id field returned and skip the next step.
  2. If the call is not complete, issue a checkStatus() call in a loop using the value in the id field of the AsyncResult object, returned by the retrieve() call in the previous step. Check the AsyncResult object returned until the done field contains true. The time taken to complete a retrieve() call depends on the size of the zip file being deployed, so use a longer wait time between iterations as the size of the zip file increases.
  3. Issue a checkRetrieveStatus() call to obtain the results of the retrieve() call, using the id value returned in the first step.

For examples of manifest files, see Sample package.xml Manifest Files.

Permissions

Your client application must be logged in with the “Modify All Data” permission.

Arguments

Name Type Description
retrieveRequest RetrieveRequest Encapsulates options for determining which packages or files are retrieved.

Response

AsyncResult

Sample Code—Java

This sample shows how to retrieve components into a zip file. See the deploy() sample code for details on how to deploy a zip file.

This sample requires API version 34.0 or later.

Note

1package com.doc.samples;
2
3import java.io.*;
4import java.util.*;
5import java.nio.ByteBuffer;
6import java.nio.channels.*;
7import java.rmi.RemoteException;
8import javax.xml.parsers.DocumentBuilder;
9import javax.xml.parsers.DocumentBuilderFactory;
10import javax.xml.parsers.ParserConfigurationException;
11import org.w3c.dom.Element;
12import org.w3c.dom.Node;
13import org.w3c.dom.NodeList;
14import org.xml.sax.SAXException;
15
16import com.sforce.soap.metadata.AsyncResult;
17import com.sforce.soap.metadata.MetadataConnection;
18import com.sforce.soap.enterprise.EnterpriseConnection;
19import com.sforce.soap.metadata.RetrieveMessage;
20import com.sforce.soap.metadata.RetrieveRequest;
21import com.sforce.soap.metadata.RetrieveResult;
22import com.sforce.soap.metadata.RetrieveStatus;
23import com.sforce.soap.enterprise.LoginResult;
24import com.sforce.ws.ConnectionException;
25import com.sforce.ws.ConnectorConfig;
26import com.sforce.soap.metadata.PackageTypeMembers;
27
28public class RetrieveSample {
29
30    // Binding for the metadata WSDL used for making metadata API calls
31    private MetadataConnection metadataConnection;
32    
33    static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
34
35    // one second in milliseconds
36    private static final long ONE_SECOND = 1000;
37    // maximum number of attempts to retrieve the results
38    private static final int MAX_NUM_POLL_REQUESTS = 50; 
39
40    // manifest file that controls which components get retrieved
41    private static final String MANIFEST_FILE = "package.xml"; 
42
43    private static final double API_VERSION = 31.0; 
44
45    public static void main(String[] args) throws Exception {
46        final String USERNAME = "user@company.com";
47        // This is only a sample. Hard coding passwords in source files is a bad practice.
48        final String PASSWORD = "password"; 
49        final String URL = "https://login.salesforce.com/services/Soap/c/31.0";
50        
51        RetrieveSample sample = new RetrieveSample(USERNAME, PASSWORD, URL);
52        sample.retrieveZip();
53    }
54    
55    public RetrieveSample(String username, String password, String loginUrl) 
56            throws ConnectionException {
57        createMetadataConnection(username, password, loginUrl);
58    }
59
60    
61    private void retrieveZip() throws RemoteException, Exception
62    {
63        RetrieveRequest retrieveRequest = new RetrieveRequest();
64        // The version in package.xml overrides the version in RetrieveRequest
65        retrieveRequest.setApiVersion(API_VERSION);
66        setUnpackaged(retrieveRequest);
67
68        // Start the retrieve operation
69        AsyncResult asyncResult = metadataConnection.retrieve(retrieveRequest);
70        String asyncResultId = asyncResult.getId();
71        
72        // Wait for the retrieve to complete
73        int poll = 0;
74        long waitTimeMilliSecs = ONE_SECOND;
75        RetrieveResult result = null;
76        do {
77            Thread.sleep(waitTimeMilliSecs);
78            // Double the wait time for the next iteration
79            waitTimeMilliSecs *= 2;
80            if (poll++ > MAX_NUM_POLL_REQUESTS) {
81                throw new Exception("Request timed out.  If this is a large set " +
82                "of metadata components, check that the time allowed " +
83                "by MAX_NUM_POLL_REQUESTS is sufficient.");
84            }
85            result = metadataConnection.checkRetrieveStatus(
86                    asyncResultId, true);
87            System.out.println("Retrieve Status: " + result.getStatus());
88        } while (!result.isDone());
89
90        if (result.getStatus() == RetrieveStatus.Failed) {
91            throw new Exception(result.getErrorStatusCode() + " msg: " +
92                    result.getErrorMessage());
93        } else if (result.getStatus() == RetrieveStatus.Succeeded) {      
94            // Print out any warning messages
95            StringBuilder buf = new StringBuilder();
96            if (result.getMessages() != null) {
97                for (RetrieveMessage rm : result.getMessages()) {
98                    buf.append(rm.getFileName() + " - " + rm.getProblem());
99                }
100            }
101            if (buf.length() > 0) {
102                System.out.println("Retrieve warnings:\n" + buf);
103            }
104    
105            // Write the zip to the file system
106            System.out.println("Writing results to zip file");
107            ByteArrayInputStream bais = new ByteArrayInputStream(result.getZipFile());
108            File resultsFile = new File("retrieveResults.zip");
109            FileOutputStream os = new FileOutputStream(resultsFile);
110            try {
111                ReadableByteChannel src = Channels.newChannel(bais);
112                FileChannel dest = os.getChannel();
113                copy(src, dest);
114                
115                System.out.println("Results written to " + resultsFile.getAbsolutePath());
116            } finally {
117                os.close();
118            }
119        }
120    }
121    
122    /**
123     * Helper method to copy from a readable channel to a writable channel,
124     * using an in-memory buffer.
125     */
126    private void copy(ReadableByteChannel src, WritableByteChannel dest)
127        throws IOException
128    {
129        // Use an in-memory byte buffer
130        ByteBuffer buffer = ByteBuffer.allocate(8092);
131        while (src.read(buffer) != -1) {
132            buffer.flip();
133            while(buffer.hasRemaining()) {
134                dest.write(buffer);
135            }
136            buffer.clear();
137        }
138    }
139    
140    private void setUnpackaged(RetrieveRequest request) throws Exception
141    {
142        // Edit the path, if necessary, if your package.xml file is located elsewhere
143        File unpackedManifest = new File(MANIFEST_FILE);
144        System.out.println("Manifest file: " + unpackedManifest.getAbsolutePath());
145        
146        if (!unpackedManifest.exists() || !unpackedManifest.isFile())
147            throw new Exception("Should provide a valid retrieve manifest " +
148                    "for unpackaged content. " +
149                    "Looking for " + unpackedManifest.getAbsolutePath());
150
151        // Note that we populate the _package object by parsing a manifest file here.
152        // You could populate the _package based on any source for your
153        // particular application.
154        com.sforce.soap.metadata.Package p = parsePackage(unpackedManifest);
155        request.setUnpackaged(p);
156    }
157
158    private com.sforce.soap.metadata.Package parsePackage(File file) throws Exception {
159        try {
160            InputStream is = new FileInputStream(file);
161            List<PackageTypeMembers> pd = new ArrayList<PackageTypeMembers>();
162            DocumentBuilder db =
163                DocumentBuilderFactory.newInstance().newDocumentBuilder();
164            Element d = db.parse(is).getDocumentElement();
165            for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling()) {
166                if (c instanceof Element) {
167                    Element ce = (Element)c;
168                    //
169                    NodeList namee = ce.getElementsByTagName("name");
170                    if (namee.getLength() == 0) {
171                        // not
172                        continue;
173                    }
174                    String name = namee.item(0).getTextContent();
175                    NodeList m = ce.getElementsByTagName("members");
176                    List<String> members = new ArrayList<String>();
177                    for (int i = 0; i < m.getLength(); i++) {
178                        Node mm = m.item(i);
179                        members.add(mm.getTextContent());
180                    }
181                    PackageTypeMembers pdi = new PackageTypeMembers();
182                    pdi.setName(name);
183                    pdi.setMembers(members.toArray(new String[members.size()]));
184                    pd.add(pdi);
185                }
186            }
187            com.sforce.soap.metadata.Package r = new com.sforce.soap.metadata.Package();
188            r.setTypes(pd.toArray(new PackageTypeMembers[pd.size()]));
189            r.setVersion(API_VERSION + "");
190            return r;
191        } catch (ParserConfigurationException pce) {
192            throw new Exception("Cannot create XML parser", pce);
193        } catch (IOException ioe) {
194            throw new Exception(ioe);
195        } catch (SAXException se) {
196            throw new Exception(se);
197        }
198    }
199    
200 
201    private void createMetadataConnection(final String username,
202            final String password, final String loginUrl)
203            throws ConnectionException {
204
205        final ConnectorConfig loginConfig = new ConnectorConfig();
206        loginConfig.setAuthEndpoint(loginUrl);
207        loginConfig.setServiceEndpoint(loginUrl);
208        loginConfig.setManualLogin(true);
209        LoginResult loginResult = (new EnterpriseConnection(loginConfig)).login(
210                username, password);
211
212        final ConnectorConfig metadataConfig = new ConnectorConfig();
213        metadataConfig.setServiceEndpoint(loginResult.getMetadataServerUrl());
214        metadataConfig.setSessionId(loginResult.getSessionId());
215        this.metadataConnection = new MetadataConnection(metadataConfig);
216    }
217    
218    //The sample client application retrieves the user's login credentials.
219    // Helper function for retrieving user input from the console
220    String getUserInput(String prompt) {
221        System.out.print(prompt);
222        try {
223            return rdr.readLine();
224        }
225        catch (IOException ex) {
226            return null;
227        }
228    }
229
230}