Chilkat Online Tools

SQL Server / Creatio API / $select 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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @queryParams int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, '$select', 'Field1,Field2'

    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', @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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

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

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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(Name,FullJobTitle)",
    --   "value": [
    --     {
    --       "Name": "William Walker",
    --       "FullJobTitle": "Developer"
    --     },
    --     {
    --       "Name": "Timothy Sawyer",
    --       "FullJobTitle": "Developer"
    --     },
    --     {
    --       "Name": "Symon Clarke",
    --       "FullJobTitle": "Marketing manager"
    --     },
    --     {
    --       "Name": "Mary King",
    --       "FullJobTitle": "Customer manager"
    --     },
    --     {
    --       "Name": "Sarah M. Richards",
    --       "FullJobTitle": "Head of accounting department"
    --     },
    --     {
    --       "Name": "Valerie E. Murphy",
    --       "FullJobTitle": "Head of marketing department"
    --     },
    --     {
    --       "Name": "Jason Robinson",
    --       "FullJobTitle": "Specialist"
    --     },
    --     {
    --       "Name": "Megan Lewis",
    --       "FullJobTitle": "Customer manager"
    --     },
    --     {
    --       "Name": "Peter Moore",
    --       "FullJobTitle": "Head of development department"
    --     },
    --     {
    --       "Name": "Caleb Jones",
    --       "FullJobTitle": "CEO"
    --     },
    --     {
    --       "Name": "John Best",
    --       "FullJobTitle": "Head of sales department"
    --     },
    --     {
    --       "Name": "Tiffany J. Martin",
    --       "FullJobTitle": "IT Support Engineer"
    --     },
    --     {
    --       "Name": "Tracy Wilkinson",
    --       "FullJobTitle": "Marketing manager"
    --     }
    --   ]
    -- }

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

    DECLARE @Name nvarchar(4000)

    DECLARE @FullJobTitle nvarchar(4000)

    DECLARE @odata_context nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @odata_context OUT, '"@odata.context"'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'value'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'value[i].Name'
        EXEC sp_OAMethod @jResp, 'StringOf', @FullJobTitle OUT, 'value[i].FullJobTitle'
        SELECT @i = @i + 1
      END

    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 "$select=Field1,Field2"
	-H "ForceUseSession: true"
	-H "BPMCSRF: {{BPMCSRF}}"
https://myserver.com/0/odata/Collection1

Postman Collection Item JSON

{
  "name": "$select parameter",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "ForceUseSession",
        "type": "text",
        "value": "true"
      },
      {
        "key": "BPMCSRF",
        "type": "text",
        "value": "{{BPMCSRF}}"
      }
    ],
    "url": {
      "raw": "{{BaseURI}}/0/odata/{{CollectionName1}}?$select={{FieldName1}},{{FieldName2}}",
      "host": [
        "{{BaseURI}}"
      ],
      "path": [
        "0",
        "odata",
        "{{CollectionName1}}"
      ],
      "query": [
        {
          "key": "$select",
          "value": "{{FieldName1}},{{FieldName2}}"
        }
      ]
    },
    "description": "Request for getting specific fields from an object collection instances via the [$select](http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_SystemQueryOptionselect) parameter."
  },
  "response": [
    {
      "name": "[200] Gets object collection instances with selected fields",
      "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?$select=Name,FullJobTitle",
          "protocol": "https",
          "host": [
            "myserver",
            "com"
          ],
          "path": [
            "0",
            "odata",
            "Employee"
          ],
          "query": [
            {
              "key": "$select",
              "value": "Name,FullJobTitle"
            }
          ]
        }
      },
      "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:51:34 GMT"
        },
        {
          "key": "Content-Length",
          "value": "884"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"@odata.context\": \"https://myserver.com/0/odata/$metadata#Employee(Name,FullJobTitle)\",\n    \"value\": [\n        {\n            \"Name\": \"William Walker\",\n            \"FullJobTitle\": \"Developer\"\n        },\n        {\n            \"Name\": \"Timothy Sawyer\",\n            \"FullJobTitle\": \"Developer\"\n        },\n        {\n            \"Name\": \"Symon Clarke\",\n            \"FullJobTitle\": \"Marketing manager\"\n        },\n        {\n            \"Name\": \"Mary King\",\n            \"FullJobTitle\": \"Customer manager\"\n        },\n        {\n            \"Name\": \"Sarah M. Richards\",\n            \"FullJobTitle\": \"Head of accounting department\"\n        },\n        {\n            \"Name\": \"Valerie E. Murphy\",\n            \"FullJobTitle\": \"Head of marketing department\"\n        },\n        {\n            \"Name\": \"Jason Robinson\",\n            \"FullJobTitle\": \"Specialist\"\n        },\n        {\n            \"Name\": \"Megan Lewis\",\n            \"FullJobTitle\": \"Customer manager\"\n        },\n        {\n            \"Name\": \"Peter Moore\",\n            \"FullJobTitle\": \"Head of development department\"\n        },\n        {\n            \"Name\": \"Caleb Jones\",\n            \"FullJobTitle\": \"CEO\"\n        },\n        {\n            \"Name\": \"John Best\",\n            \"FullJobTitle\": \"Head of sales department\"\n        },\n        {\n            \"Name\": \"Tiffany J. Martin\",\n            \"FullJobTitle\": \"IT Support Engineer\"\n        },\n        {\n            \"Name\": \"Tracy Wilkinson\",\n            \"FullJobTitle\": \"Marketing manager\"\n        }\n    ]\n}"
    }
  ]
}