Chilkat Online Tools

SQL Server / Salesforce Platform APIs / User Photo

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @req int
    -- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'Path', '/services/data/v{{version}}/connect/user-profiles/me/photo'
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'

    DECLARE @jsonFormData1 int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonFormData1 OUT

    EXEC sp_OAMethod @jsonFormData1, 'UpdateString', @success OUT, 'cropY', '0'
    EXEC sp_OAMethod @jsonFormData1, 'UpdateString', @success OUT, 'cropX', '0'
    EXEC sp_OAMethod @jsonFormData1, 'UpdateString', @success OUT, 'cropSize', '200'
    EXEC sp_OAMethod @req, 'AddStringForUpload2', @success OUT, 'json', '', @jsonFormData1, 'utf-8', 'application/json'

    EXEC sp_OAMethod @req, 'AddFileForUpload2', @success OUT, 'fileUpload', ' path to file', 'application/octet-stream'

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Bearer <access_token>'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Content-Disposition', 'form-data; name="photo"'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Expect', '100-continue'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'SynchronousRequest', @resp OUT, 'domain.com', 443, 1, @req
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonFormData1
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonFormData1
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "fullEmailPhotoUrl": "https://<my_domain>/img/userprofile/default_profile_200_v2.png?fromEmail=1",
    --   "largePhotoUrl": "https://<my_domain>/profilephoto/729.../F",
    --   "mediumPhotoUrl": "https://<my_domain>/profilephoto/729.../M",
    --   "photoVersionId": "729...",
    --   "smallPhotoUrl": "https://<my_domain>/profilephoto/729.../T",
    --   "standardEmailPhotoUrl": "https://<my_domain>/img/userprofile/default_profile_45_v2.png?fromEmail=1",
    --   "url": "/services/data/v58.0/connect/user-profiles/005.../photo"
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @fullEmailPhotoUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @fullEmailPhotoUrl OUT, 'fullEmailPhotoUrl'
    DECLARE @largePhotoUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @largePhotoUrl OUT, 'largePhotoUrl'
    DECLARE @mediumPhotoUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @mediumPhotoUrl OUT, 'mediumPhotoUrl'
    DECLARE @photoVersionId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @photoVersionId OUT, 'photoVersionId'
    DECLARE @smallPhotoUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @smallPhotoUrl OUT, 'smallPhotoUrl'
    DECLARE @standardEmailPhotoUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @standardEmailPhotoUrl OUT, 'standardEmailPhotoUrl'
    DECLARE @url nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @url OUT, 'url'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @jsonFormData1
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: multipart/form-data"
	-H "Content-Disposition: form-data; name=\"photo\""
	-H "Content-Type: application/json; charset=UTF-8"
	--form 'json={"cropY":"0","cropX":"0","cropSize":"200"}'
	--form 'fileUpload=@"/path/to/file"'
https://domain.com/services/data/v{{version}}/connect/user-profiles/me/photo

Postman Collection Item JSON

{
  "name": "User Photo",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "multipart/form-data",
        "disabled": true
      },
      {
        "key": "Content-Disposition",
        "value": "form-data; name=\"photo\"",
        "disabled": true
      },
      {
        "key": "Content-Type",
        "value": "application/json; charset=UTF-8",
        "disabled": true
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "json",
          "value": "{\"cropY\":\"0\",\"cropX\":\"0\",\"cropSize\":\"200\"}",
          "type": "text"
        },
        {
          "key": "fileUpload",
          "type": "file",
          "src": [
          ]
        }
      ]
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/connect/user-profiles/me/photo",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "connect",
        "user-profiles",
        "me",
        "photo"
      ]
    },
    "description": "Get, post, and crop a user photo.\nTo use an image from the Files page as a user photo, pass the file ID in the fileId property of the request body or in the fileId request parameter. Images uploaded on the User page don’t have a file ID and can’t be used as the fileId.\n\nTo upload a binary file as the user photo, you must send it in a multipart/form-data message. For information about how to create the multipart/form-data message, see Uploading Binary Files.\n\nTo display user profile photos in a feed, cache the user photos. Then use the photoVersionId property of the Photo response body to determine when you need to update a photo. This technique helps you avoid running over limits and may improve mobile client performance."
  },
  "response": [
    {
      "name": "Succesful User Photo",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "multipart/form-data",
            "disabled": true
          },
          {
            "key": "Content-Disposition",
            "value": "form-data; name=\"photo\"",
            "disabled": true
          },
          {
            "key": "Content-Type",
            "value": "application/json; charset=UTF-8",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "json",
              "value": "{\"cropY\":\"0\",\"cropX\":\"0\",\"cropSize\":\"200\"}",
              "type": "text"
            },
            {
              "key": "fileUpload",
              "type": "file",
              "src": "/Users/scolladon/Downloads/1x1.png"
            }
          ]
        },
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/connect/user-profiles/me/photo",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "connect",
            "user-profiles",
            "me",
            "photo"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 20 Nov 2023 16:15:44 GMT"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000; includeSubDomains"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Robots-Tag",
          "value": "none"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"fullEmailPhotoUrl\": \"https://<my_domain>/img/userprofile/default_profile_200_v2.png?fromEmail=1\",\n    \"largePhotoUrl\": \"https://<my_domain>/profilephoto/729.../F\",\n    \"mediumPhotoUrl\": \"https://<my_domain>/profilephoto/729.../M\",\n    \"photoVersionId\": \"729...\",\n    \"smallPhotoUrl\": \"https://<my_domain>/profilephoto/729.../T\",\n    \"standardEmailPhotoUrl\": \"https://<my_domain>/img/userprofile/default_profile_45_v2.png?fromEmail=1\",\n    \"url\": \"/services/data/v58.0/connect/user-profiles/005.../photo\"\n}"
    }
  ]
}