Sample code for 30+ languages & platforms
SQL Server

List Groups

See more Microsoft Group Examples

List all the groups available in an organization, including but not limited to Office 365 Groups.

See https://docs.microsoft.com/en-us/graph/api/group-list?view=graph-rest-1.0 for more information.

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 @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires 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

    -- Use your previously obtained access token as shown here:
    --    Get Microsoft Graph OAuth2 Access Token with Group.ReadWrite.All scope.

    DECLARE @jsonToken int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT

    EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/msGraphGroup.json'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jsonToken, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        RETURN
      END
    EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0

    -- Send a GET request to https://graph.microsoft.com/v1.0/groups?$orderby=displayName
    DECLARE @strResponse nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @strResponse OUT, 'https://graph.microsoft.com/v1.0/groups?$orderby=displayName'
    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 @jsonToken
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'Load', @success OUT, @strResponse
    EXEC sp_OASetProperty @json, 'EmitCompact', 0

    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN
        EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
        PRINT @sTmp0

        EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
        PRINT 'Failed, response status code = ' + @iTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample output:
    -- (See parsing code below..)

    -- {
    --     "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups",
    --     "value": [
    --         {
    --             "id": "45b7d2e7-b882-4a80-ba97-10b7a63b8fa4",
    --             "deletedDateTime": null,
    --             "classification": null,
    --             "createdDateTime": "2018-12-22T02:21:05Z",
    --             "creationOptions": [],
    --             "description": "Self help community for golf",
    --             "displayName": "Golf Assist",
    --             "groupTypes": [
    --                 "Unified"
    --             ],
    --             "mail": "golfassist@contoso.com",
    --             "mailEnabled": true,
    --             "mailNickname": "golfassist",
    --             "onPremisesLastSyncDateTime": null,
    --             "onPremisesSecurityIdentifier": null,
    --             "onPremisesSyncEnabled": null,
    --             "preferredDataLocation": "CAN",
    --             "proxyAddresses": [
    --                 "smtp:golfassist@contoso.onmicrosoft.com",
    --                 "SMTP:golfassist@contoso.com"
    --             ],
    --             "renewedDateTime": "2018-12-22T02:21:05Z",
    --             "resourceBehaviorOptions": [],
    --             "resourceProvisioningOptions": [],
    --             "securityEnabled": false,
    --             "visibility": "Public",
    --             "onPremisesProvisioningErrors": []
    --         },
    --         {
    --             "id": "d7797254-3084-44d0-99c9-a3b5ab149538",
    --             "deletedDateTime": null,
    --             "classification": null,
    --             "createdDateTime": "2018-11-19T20:29:40Z",
    --             "creationOptions": [],
    --             "description": "Talk about golf",
    --             "displayName": "Golf Discussion",
    --             "groupTypes": [],
    --             "mail": "golftalk@contoso.com",
    --             "mailEnabled": true,
    --             "mailNickname": "golftalk",
    --             "onPremisesLastSyncDateTime": null,
    --             "onPremisesSecurityIdentifier": null,
    --             "onPremisesSyncEnabled": null,
    --             "preferredDataLocation": "CAN",
    --             "proxyAddresses": [
    --                 "smtp:golftalk@contoso.onmicrosoft.com",
    --                 "SMTP:golftalk@contoso.com"
    --             ],
    --             "renewedDateTime": "2018-11-19T20:29:40Z",
    --             "resourceBehaviorOptions": [],
    --             "resourceProvisioningOptions": [],
    --             "securityEnabled": false,
    --             "visibility": null,
    --             "onPremisesProvisioningErrors": []
    --         }
    --     ]
    -- }
    -- 
    -- Use this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

    DECLARE @odataContext nvarchar(4000)

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @id nvarchar(4000)

    DECLARE @deletedDateTime nvarchar(4000)

    DECLARE @classification nvarchar(4000)

    DECLARE @createdDateTime nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @displayName nvarchar(4000)

    DECLARE @mail nvarchar(4000)

    DECLARE @mailEnabled int

    DECLARE @mailNickname nvarchar(4000)

    DECLARE @onPremisesLastSyncDateTime nvarchar(4000)

    DECLARE @onPremisesSecurityIdentifier nvarchar(4000)

    DECLARE @onPremisesSyncEnabled nvarchar(4000)

    DECLARE @preferredDataLocation nvarchar(4000)

    DECLARE @renewedDateTime nvarchar(4000)

    DECLARE @securityEnabled int

    DECLARE @visibility nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @strVal nvarchar(4000)

    EXEC sp_OAMethod @json, 'StringOf', @odataContext OUT, '"@odata.context"'
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'value'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'value[i].id'
        EXEC sp_OAMethod @json, 'StringOf', @deletedDateTime OUT, 'value[i].deletedDateTime'
        EXEC sp_OAMethod @json, 'StringOf', @classification OUT, 'value[i].classification'
        EXEC sp_OAMethod @json, 'StringOf', @createdDateTime OUT, 'value[i].createdDateTime'
        EXEC sp_OAMethod @json, 'StringOf', @description OUT, 'value[i].description'
        EXEC sp_OAMethod @json, 'StringOf', @displayName OUT, 'value[i].displayName'
        EXEC sp_OAMethod @json, 'StringOf', @mail OUT, 'value[i].mail'
        EXEC sp_OAMethod @json, 'BoolOf', @mailEnabled OUT, 'value[i].mailEnabled'
        EXEC sp_OAMethod @json, 'StringOf', @mailNickname OUT, 'value[i].mailNickname'
        EXEC sp_OAMethod @json, 'StringOf', @onPremisesLastSyncDateTime OUT, 'value[i].onPremisesLastSyncDateTime'
        EXEC sp_OAMethod @json, 'StringOf', @onPremisesSecurityIdentifier OUT, 'value[i].onPremisesSecurityIdentifier'
        EXEC sp_OAMethod @json, 'StringOf', @onPremisesSyncEnabled OUT, 'value[i].onPremisesSyncEnabled'
        EXEC sp_OAMethod @json, 'StringOf', @preferredDataLocation OUT, 'value[i].preferredDataLocation'
        EXEC sp_OAMethod @json, 'StringOf', @renewedDateTime OUT, 'value[i].renewedDateTime'
        EXEC sp_OAMethod @json, 'BoolOf', @securityEnabled OUT, 'value[i].securityEnabled'
        EXEC sp_OAMethod @json, 'StringOf', @visibility OUT, 'value[i].visibility'
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].creationOptions'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            -- ...
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].groupTypes'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'value[i].groupTypes[j]'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].proxyAddresses'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'value[i].proxyAddresses[j]'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].resourceBehaviorOptions'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            -- ...
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].resourceProvisioningOptions'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            -- ...
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'value[i].onPremisesProvisioningErrors'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            -- ...
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jsonToken
    EXEC @hr = sp_OADestroy @json


END
GO