Sample code for 30+ languages & platforms
SQL Server

Microsoft Teams - List Joined Teams

See more Microsoft Teams Examples

Get the teams in Microsoft Teams that the user is a direct member of.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  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.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Implements the following CURL command:

    --  curl -X GET https://graph.microsoft.com/v1.0/me/joinedTeams \
    --    -H 'authorization: Bearer ACCESS_TOKEN'

    --  Use the following online tool to generate HTTP code from a CURL command
    --  Convert a cURL Command to HTTP Source Code

    --  Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    EXEC sp_OASetProperty @http, 'AuthToken', 'ACCESS_TOKEN'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://graph.microsoft.com/v1.0/me/joinedTeams', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    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 @http, 'LastStatus', @respStatusCode OUT

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

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

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

    --  {
    --    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#teams",
    --    "@odata.count": 3,
    --    "value": [
    --      {
    --        "id": "285c8d65-d8b5-447a-91c7-85593d50c826",
    --        "createdDateTime": null,
    --        "displayName": "Chilkat Software, Inc.",
    --        "description": null,
    --        "internalId": null,
    --        "classification": null,
    --        "specialization": null,
    --        "visibility": null,
    --        "webUrl": null,
    --        "isArchived": false,
    --        "isMembershipLimitedToOwners": null,
    --        "memberSettings": null,
    --        "guestSettings": null,
    --        "messagingSettings": null,
    --        "funSettings": null,
    --        "discoverySettings": null
    --      },
    --      {
    --        "id": "e527a8b1-5620-4dcf-9b29-a4713e9afa72",
    --        "createdDateTime": null,
    --        "displayName": "My Sample Team",
    --        "description": "My Sample Team\u2019s Description",
    --        "internalId": null,
    --        "classification": null,
    --        "specialization": null,
    --        "visibility": null,
    --        "webUrl": null,
    --        "isArchived": false,
    --        "isMembershipLimitedToOwners": null,
    --        "memberSettings": null,
    --        "guestSettings": null,
    --        "messagingSettings": null,
    --        "funSettings": null,
    --        "discoverySettings": null
    --      },
    --      {
    --        "id": "bc6ef2f4-d51e-4362-9a17-11ad3382b8ad",
    --        "createdDateTime": null,
    --        "displayName": "Sample Engineering Team",
    --        "description": "This is a sample engineering team, used to showcase the range of properties supported by this API",
    --        "internalId": null,
    --        "classification": null,
    --        "specialization": null,
    --        "visibility": null,
    --        "webUrl": null,
    --        "isArchived": false,
    --        "isMembershipLimitedToOwners": null,
    --        "memberSettings": null,
    --        "guestSettings": null,
    --        "messagingSettings": null,
    --        "funSettings": null,
    --        "discoverySettings": null
    --      }
    --    ]
    --  }

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

    DECLARE @id nvarchar(4000)

    DECLARE @createdDateTime nvarchar(4000)

    DECLARE @displayName nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @internalId nvarchar(4000)

    DECLARE @classification nvarchar(4000)

    DECLARE @specialization nvarchar(4000)

    DECLARE @visibility nvarchar(4000)

    DECLARE @webUrl nvarchar(4000)

    DECLARE @isArchived int

    DECLARE @isMembershipLimitedToOwners nvarchar(4000)

    DECLARE @memberSettings nvarchar(4000)

    DECLARE @guestSettings nvarchar(4000)

    DECLARE @messagingSettings nvarchar(4000)

    DECLARE @funSettings nvarchar(4000)

    DECLARE @discoverySettings nvarchar(4000)

    DECLARE @odata_context nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @odata_context OUT, '"@odata.context"'
    DECLARE @odata_count int
    EXEC sp_OAMethod @jResp, 'IntOf', @odata_count OUT, '"@odata.count"'
    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', @id OUT, 'value[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @createdDateTime OUT, 'value[i].createdDateTime'
        EXEC sp_OAMethod @jResp, 'StringOf', @displayName OUT, 'value[i].displayName'
        EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'value[i].description'
        EXEC sp_OAMethod @jResp, 'StringOf', @internalId OUT, 'value[i].internalId'
        EXEC sp_OAMethod @jResp, 'StringOf', @classification OUT, 'value[i].classification'
        EXEC sp_OAMethod @jResp, 'StringOf', @specialization OUT, 'value[i].specialization'
        EXEC sp_OAMethod @jResp, 'StringOf', @visibility OUT, 'value[i].visibility'
        EXEC sp_OAMethod @jResp, 'StringOf', @webUrl OUT, 'value[i].webUrl'
        EXEC sp_OAMethod @jResp, 'BoolOf', @isArchived OUT, 'value[i].isArchived'
        EXEC sp_OAMethod @jResp, 'StringOf', @isMembershipLimitedToOwners OUT, 'value[i].isMembershipLimitedToOwners'
        EXEC sp_OAMethod @jResp, 'StringOf', @memberSettings OUT, 'value[i].memberSettings'
        EXEC sp_OAMethod @jResp, 'StringOf', @guestSettings OUT, 'value[i].guestSettings'
        EXEC sp_OAMethod @jResp, 'StringOf', @messagingSettings OUT, 'value[i].messagingSettings'
        EXEC sp_OAMethod @jResp, 'StringOf', @funSettings OUT, 'value[i].funSettings'
        EXEC sp_OAMethod @jResp, 'StringOf', @discoverySettings OUT, 'value[i].discoverySettings'
        SELECT @i = @i + 1
      END

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


END
GO