uploadPhoto Method

 Purpose

You can use this method to upload photos to Leads or Contacts.

 Request URL

XML Format:

For Leads:
https://<APPDOMAIN>/crm/private/xml/Leads/uploadPhoto?id=Record Id&content=File Input Stream

For Contacts:
https://<APPDOMAIN>/crm/private/xml/Contacts/uploadPhoto?id=Record Id&content=File Input Stream

 Auth Token

Auth Token is an encrypted alphanumeric string that is required to authenticate your CRM account credentials. A valid user's Authentication Token is necessary to access the API. When making an API request, send the authentication token in the Authorization header as shown below:

https://<APPDOMAIN>/crm/private/xml/Leads/uploadPhoto?id=Record Id&content=File Input Stream

Header:

Authorization=1000XXXX65.1000XXXX44.caXXXXXXXXXXXX

(See Using Authentication Token )

 Request Parameters

ParameterData TypeDescription
id*StringSpecify unique ID of the record
content*FileInputStreamPass the FileInputStream of the photo

* - Mandatory parameter

Note:

  • The size of each photo should not exceed 2 MB. If the size exceeds 2 MB, you will receive the following error message: "File size should not exceed 2 MB".

 Java Code to Upload Photo to a Lead or Contact

You can run this program in your Java Environment to upload a photo to a lead or a contact.

In the program, you need to specify values for the following:

  • Your Auth Token
  • The ID of the Record
  • The uploadPhoto Request URL in the format mentioned above
  • The File Path i.e the location of the photo

 

import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.PartSource;
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
public class UploadPhoto 
{
      public static void main(String args[])
      {
            try
            {
                  String auth_token = "USER AUTH TOKEN";
                  String auth_scope = "crmapi";
                  String targetURL = "https://<APPDOMAIN>/crm/private/xml/Leads/uploadPhoto";
                  String recordId = "RECORD ID";
                  String file = "FILE NAME";
                  File f = new File(file);
                  FileInputStream fis = new FileInputStream(f);
                  ByteArrayOutputStream bos = new ByteArrayOutputStream();
                  int c;
                  while ((c = fis.read()) != -1)
                  {
                        bos.write(c);
                  }
                  byte[] fbArray = bos.toByteArray();
                  if(useAuthToken)
                  {
                        targetURL = targetURL + "?authtoken="+ auth_token +"&ampscope="+ auth_scope;
                  }
                  PartSource ps = new ByteArrayPartSource(file,fbArray);
                  PostMethod post =new PostMethod(targetURL);
                  Part[] fields = { new FilePart("content",ps), new StringPart("id", recordId), };
                  post.setRequestEntity(new MultipartRequestEntity(fields,post.getParams()));
                  HttpClient httpclient = new HttpClient();
                  httpclient.executeMethod(post);
                  String postResp = post.getResponseBodyAsString();
                  System.out.println("postResp===========&gt : "+postResp);
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }
}

 

 Sample Response

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/crm/private/xml/Leads/uploadPhoto">
<status>
<code>200</code>
</status>
<success>
<code>4800</code>
<message>Photo uploaded succuessfully</message>
</success>
</response>

 PHP Code to Upload Photo to a Lead or Contact

<?php
$recordId="2000000016885";
$ch=curl_init();
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_VERBOSE,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_URL,"https://<APPDOMAIN>/crm/private/xml/Leads/uploadPhoto?authtoken=<Your Authtoken>&scope=crmapi");
curl_setopt($ch,CURLOPT_POST,true);
$post=array("id"=>$recordId,"content"=>"@/home/path/to/my/photo.png");
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$response=curl_exec($ch);
echo $response;
?>