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