Chilkat Online Tools

SQL Server / Creatio API / $expand parameter

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 @queryParams int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, '$expand', 'Collection2'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'ForceUseSession', 'true'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'BPMCSRF', '{{BPMCSRF}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://myserver.com/0/odata/Collection1(Id)', @queryParams
    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 @queryParams
        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 @queryParams
        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)

    -- {
    --   "@odata.context": "https://myserver.com/0/odata/$metadata#Employee(Account())/$entity",
    --   "Id": "c31c7862-fe33-4a13-9bbc-0943fa08fd02",
    --   "CreatedOn": "2017-03-30T14:50:04Z",
    --   "CreatedById": "76929f8c-7e15-4c64-bdb0-adc62d383727",
    --   "ModifiedOn": "2020-02-14T06:30:46.234Z",
    --   "ModifiedById": "410006e1-ca4e-4502-a9ec-e54d922d2c00",
    --   "ProcessListeners": 0,
    --   "ContactId": "227aab3b-7c0c-4181-abf9-81585563ab23",
    --   "Name": "William Walker",
    --   "OrgStructureUnitId": "d436a9ce-9690-4415-9e03-e8061d7cabb5",
    --   "Notes": "",
    --   "JobId": "11d68189-ced6-df11-9b2a-001d60e938c6",
    --   "FullJobTitle": "Developer",
    --   "OwnerId": "76929f8c-7e15-4c64-bdb0-adc62d383727",
    --   "CareerStartDate": "2019-09-08T00:00:00Z",
    --   "CareerDueDate": "0001-01-01T00:00:00Z",
    --   "ProbationDueDate": "2020-01-09T00:00:00Z",
    --   "ReasonForDismissalId": "00000000-0000-0000-0000-000000000000",
    --   "AccountId": "a0bf3e92-f36b-1410-0499-00155d043204",
    --   "ManagerId": "3e5bd47e-1ebd-41db-a9a6-a3560dcee3cb",
    --   "Account": {
    --     "Id": "a0bf3e92-f36b-1410-0499-00155d043204",
    --     "Name": "XT Group",
    --     "OwnerId": "cad50d15-f28d-49fc-aa79-21615ac35b86",
    --     "CreatedOn": "2020-01-27T08:00:14Z",
    --     "CreatedById": "76929f8c-7e15-4c64-bdb0-adc62d383727",
    --     "ModifiedOn": "2020-01-10T10:12:35Z",
    --     "ModifiedById": "76929f8c-7e15-4c64-bdb0-adc62d383727",
    --     "ProcessListeners": 0,
    --     "OwnershipId": "e0ae3e02-f46b-1410-fb98-00155d043204",
    --     "PrimaryContactId": "017df77c-b01c-4554-b7eb-760ac91cd0d7",
    --     "ParentId": "00000000-0000-0000-0000-000000000000",
    --     "IndustryId": "bd7f1e4a-f36b-1410-c493-00155d043205",
    --     "Code": "60",
    --     "TypeId": "03a75490-53e6-df11-971b-001d60e938c6",
    --     "Phone": "+1 212 753 2819",
    --     "AdditionalPhone": "+1 212 753 2845",
    --     "Fax": "",
    --     "Web": "www.xtg.com",
    --     "AddressTypeId": "780bf68c-4b6e-df11-b988-001d60e938c6",
    --     "Address": "17 Road Street",
    --     "CityId": "c0af42ea-f36b-1410-0299-00155d043204",
    --     "RegionId": "00b03270-f36b-1410-fd98-00155d043204",
    --     "Zip": "10351",
    --     "CountryId": "e0be1264-f36b-1410-fa98-00155d043204",
    --     "AccountCategoryId": "37ea507c-55e6-df11-971b-001d60e938c6",
    --     "EmployeesNumberId": "2c344115-55e6-df11-971b-001d60e938c6",
    --     "AnnualRevenueId": "498aceb9-55e6-df11-971b-001d60e938c6",
    --     "Notes": "",
    --     "AlternativeName": "",
    --     "GPSN": "42.6558932018082",
    --     "GPSE": "-73.7562183364144",
    --     "Completeness": 100,
    --     "PriceListId": "00000000-0000-0000-0000-000000000000",
    --     "AccountLogoId": "69517c67-e5de-47db-ae50-df69447a12cf"
    --   }
    -- }

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

    DECLARE @odata_context nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @odata_context OUT, '"@odata.context"'
    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'Id'
    DECLARE @CreatedOn nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @CreatedOn OUT, 'CreatedOn'
    DECLARE @CreatedById nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @CreatedById OUT, 'CreatedById'
    DECLARE @ModifiedOn nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ModifiedOn OUT, 'ModifiedOn'
    DECLARE @ModifiedById nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ModifiedById OUT, 'ModifiedById'
    DECLARE @ProcessListeners int
    EXEC sp_OAMethod @jResp, 'IntOf', @ProcessListeners OUT, 'ProcessListeners'
    DECLARE @ContactId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ContactId OUT, 'ContactId'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'Name'
    DECLARE @OrgStructureUnitId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @OrgStructureUnitId OUT, 'OrgStructureUnitId'
    DECLARE @Notes nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Notes OUT, 'Notes'
    DECLARE @JobId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @JobId OUT, 'JobId'
    DECLARE @FullJobTitle nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @FullJobTitle OUT, 'FullJobTitle'
    DECLARE @OwnerId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @OwnerId OUT, 'OwnerId'
    DECLARE @CareerStartDate nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @CareerStartDate OUT, 'CareerStartDate'
    DECLARE @CareerDueDate nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @CareerDueDate OUT, 'CareerDueDate'
    DECLARE @ProbationDueDate nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ProbationDueDate OUT, 'ProbationDueDate'
    DECLARE @ReasonForDismissalId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ReasonForDismissalId OUT, 'ReasonForDismissalId'
    DECLARE @AccountId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountId OUT, 'AccountId'
    DECLARE @ManagerId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ManagerId OUT, 'ManagerId'
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountId OUT, 'Account.Id'
    DECLARE @AccountName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountName OUT, 'Account.Name'
    DECLARE @AccountOwnerId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountOwnerId OUT, 'Account.OwnerId'
    DECLARE @AccountCreatedOn nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountCreatedOn OUT, 'Account.CreatedOn'
    DECLARE @AccountCreatedById nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountCreatedById OUT, 'Account.CreatedById'
    DECLARE @AccountModifiedOn nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountModifiedOn OUT, 'Account.ModifiedOn'
    DECLARE @AccountModifiedById nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountModifiedById OUT, 'Account.ModifiedById'
    DECLARE @AccountProcessListeners int
    EXEC sp_OAMethod @jResp, 'IntOf', @AccountProcessListeners OUT, 'Account.ProcessListeners'
    DECLARE @OwnershipId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @OwnershipId OUT, 'Account.OwnershipId'
    DECLARE @PrimaryContactId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PrimaryContactId OUT, 'Account.PrimaryContactId'
    DECLARE @ParentId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ParentId OUT, 'Account.ParentId'
    DECLARE @IndustryId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @IndustryId OUT, 'Account.IndustryId'
    DECLARE @Code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'Account.Code'
    DECLARE @TypeId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @TypeId OUT, 'Account.TypeId'
    DECLARE @Phone nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Phone OUT, 'Account.Phone'
    DECLARE @AdditionalPhone nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AdditionalPhone OUT, 'Account.AdditionalPhone'
    DECLARE @Fax nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Fax OUT, 'Account.Fax'
    DECLARE @Web nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Web OUT, 'Account.Web'
    DECLARE @AddressTypeId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AddressTypeId OUT, 'Account.AddressTypeId'
    DECLARE @Address nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Address OUT, 'Account.Address'
    DECLARE @CityId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @CityId OUT, 'Account.CityId'
    DECLARE @RegionId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @RegionId OUT, 'Account.RegionId'
    DECLARE @v_Zip nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Zip OUT, 'Account.Zip'
    DECLARE @CountryId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @CountryId OUT, 'Account.CountryId'
    DECLARE @AccountCategoryId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountCategoryId OUT, 'Account.AccountCategoryId'
    DECLARE @EmployeesNumberId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @EmployeesNumberId OUT, 'Account.EmployeesNumberId'
    DECLARE @AnnualRevenueId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AnnualRevenueId OUT, 'Account.AnnualRevenueId'
    DECLARE @AccountNotes nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountNotes OUT, 'Account.Notes'
    DECLARE @AlternativeName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AlternativeName OUT, 'Account.AlternativeName'
    DECLARE @GPSN nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @GPSN OUT, 'Account.GPSN'
    DECLARE @GPSE nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @GPSE OUT, 'Account.GPSE'
    DECLARE @Completeness int
    EXEC sp_OAMethod @jResp, 'IntOf', @Completeness OUT, 'Account.Completeness'
    DECLARE @PriceListId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PriceListId OUT, 'Account.PriceListId'
    DECLARE @AccountLogoId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AccountLogoId OUT, 'Account.AccountLogoId'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @queryParams
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -G -d "$expand=Collection2"
	-H "ForceUseSession: true"
	-H "BPMCSRF: {{BPMCSRF}}"
https://myserver.com/0/odata/Collection1(Id)

Postman Collection Item JSON

{
  "name": "$expand parameter",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "ForceUseSession",
        "type": "text",
        "value": "true"
      },
      {
        "key": "BPMCSRF",
        "type": "text",
        "value": "{{BPMCSRF}}"
      }
    ],
    "url": {
      "raw": "{{BaseURI}}/0/odata/{{CollectionName1}}({{ObjectId1}})?$expand={{CollectionName2}}",
      "host": [
        "{{BaseURI}}"
      ],
      "path": [
        "0",
        "odata",
        "{{CollectionName1}}({{ObjectId1}})"
      ],
      "query": [
        {
          "key": "$expand",
          "value": "{{CollectionName2}}"
        }
      ]
    },
    "description": "Request for getting an object collection instance by instance Id of another object collection via the [$expand](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_SystemQueryOptionexpand) parameter."
  },
  "response": [
    {
      "name": "[200] Gets an objects collection instances by another object collection",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "type": "text",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "type": "text",
            "value": "application/json; charset=utf-8"
          },
          {
            "key": "ForceUseSession",
            "type": "text",
            "value": "true"
          },
          {
            "key": "BPMCSRF",
            "type": "text",
            "value": "{{BPMCSRF}}"
          }
        ],
        "url": {
          "raw": "https://myserver.com/0/odata/Employee(c31c7862-fe33-4a13-9bbc-0943fa08fd02)?$expand=Account",
          "protocol": "https",
          "host": [
            "myserver",
            "com"
          ],
          "path": [
            "0",
            "odata",
            "Employee(c31c7862-fe33-4a13-9bbc-0943fa08fd02)"
          ],
          "query": [
            {
              "key": "$expand",
              "value": "Account"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Content-Type",
          "value": "application/json; odata.metadata=minimal"
        },
        {
          "key": "Expires",
          "value": "-1"
        },
        {
          "key": "Server",
          "value": "Microsoft-IIS/10.0"
        },
        {
          "key": "OData-Version",
          "value": "4.0"
        },
        {
          "key": "X-AspNet-Version",
          "value": "4.0.30319"
        },
        {
          "key": "X-Powered-By",
          "value": "ASP.NET"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Date",
          "value": "Wed, 26 Feb 2020 14:52:02 GMT"
        },
        {
          "key": "Content-Length",
          "value": "2206"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"@odata.context\": \"https://myserver.com/0/odata/$metadata#Employee(Account())/$entity\",\n    \"Id\": \"c31c7862-fe33-4a13-9bbc-0943fa08fd02\",\n    \"CreatedOn\": \"2017-03-30T14:50:04Z\",\n    \"CreatedById\": \"76929f8c-7e15-4c64-bdb0-adc62d383727\",\n    \"ModifiedOn\": \"2020-02-14T06:30:46.234Z\",\n    \"ModifiedById\": \"410006e1-ca4e-4502-a9ec-e54d922d2c00\",\n    \"ProcessListeners\": 0,\n    \"ContactId\": \"227aab3b-7c0c-4181-abf9-81585563ab23\",\n    \"Name\": \"William Walker\",\n    \"OrgStructureUnitId\": \"d436a9ce-9690-4415-9e03-e8061d7cabb5\",\n    \"Notes\": \"\",\n    \"JobId\": \"11d68189-ced6-df11-9b2a-001d60e938c6\",\n    \"FullJobTitle\": \"Developer\",\n    \"OwnerId\": \"76929f8c-7e15-4c64-bdb0-adc62d383727\",\n    \"CareerStartDate\": \"2019-09-08T00:00:00Z\",\n    \"CareerDueDate\": \"0001-01-01T00:00:00Z\",\n    \"ProbationDueDate\": \"2020-01-09T00:00:00Z\",\n    \"ReasonForDismissalId\": \"00000000-0000-0000-0000-000000000000\",\n    \"AccountId\": \"a0bf3e92-f36b-1410-0499-00155d043204\",\n    \"ManagerId\": \"3e5bd47e-1ebd-41db-a9a6-a3560dcee3cb\",\n    \"Account\": {\n        \"Id\": \"a0bf3e92-f36b-1410-0499-00155d043204\",\n        \"Name\": \"XT Group\",\n        \"OwnerId\": \"cad50d15-f28d-49fc-aa79-21615ac35b86\",\n        \"CreatedOn\": \"2020-01-27T08:00:14Z\",\n        \"CreatedById\": \"76929f8c-7e15-4c64-bdb0-adc62d383727\",\n        \"ModifiedOn\": \"2020-01-10T10:12:35Z\",\n        \"ModifiedById\": \"76929f8c-7e15-4c64-bdb0-adc62d383727\",\n        \"ProcessListeners\": 0,\n        \"OwnershipId\": \"e0ae3e02-f46b-1410-fb98-00155d043204\",\n        \"PrimaryContactId\": \"017df77c-b01c-4554-b7eb-760ac91cd0d7\",\n        \"ParentId\": \"00000000-0000-0000-0000-000000000000\",\n        \"IndustryId\": \"bd7f1e4a-f36b-1410-c493-00155d043205\",\n        \"Code\": \"60\",\n        \"TypeId\": \"03a75490-53e6-df11-971b-001d60e938c6\",\n        \"Phone\": \"+1 212 753 2819\",\n        \"AdditionalPhone\": \"+1 212 753 2845\",\n        \"Fax\": \"\",\n        \"Web\": \"www.xtg.com\",\n        \"AddressTypeId\": \"780bf68c-4b6e-df11-b988-001d60e938c6\",\n        \"Address\": \"17 Road Street\",\n        \"CityId\": \"c0af42ea-f36b-1410-0299-00155d043204\",\n        \"RegionId\": \"00b03270-f36b-1410-fd98-00155d043204\",\n        \"Zip\": \"10351\",\n        \"CountryId\": \"e0be1264-f36b-1410-fa98-00155d043204\",\n        \"AccountCategoryId\": \"37ea507c-55e6-df11-971b-001d60e938c6\",\n        \"EmployeesNumberId\": \"2c344115-55e6-df11-971b-001d60e938c6\",\n        \"AnnualRevenueId\": \"498aceb9-55e6-df11-971b-001d60e938c6\",\n        \"Notes\": \"\",\n        \"AlternativeName\": \"\",\n        \"GPSN\": \"42.6558932018082\",\n        \"GPSE\": \"-73.7562183364144\",\n        \"Completeness\": 100,\n        \"PriceListId\": \"00000000-0000-0000-0000-000000000000\",\n        \"AccountLogoId\": \"69517c67-e5de-47db-ae50-df69447a12cf\"\n    }\n}"
    }
  ]
}