SQL Server
SQL Server
Create Task
See more Microsoft Tasks and Plans Examples
Demonstrates how to create a new plannerTask.See https://docs.microsoft.com/en-us/graph/api/planner-post-tasks?view=graph-rest-1.0 for more information.
Chilkat SQL Server Downloads
-- 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
-- The Microsoft Planner REST API requires an OAuth2 token with the Group.ReadWrite.All scope.
-- 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
-- Create a JSON body for the HTTP POST
-- Use this online tool to generate the code from sample JSON:
-- Generate Code to Create JSON
-- {
-- "planId": "xqQg5FS2LkCp935s-FIFm2QAFkHM",
-- "bucketId": "hsOf2dhOJkqyYYZEtdzDe2QAIUCR",
-- "title": "Update client list",
-- "assignments": {
-- "fbab97d0-4932-4511-b675-204639209557": {
-- "@odata.type": "#microsoft.graph.plannerAssignment",
-- "orderHint": " !"
-- }
-- },
-- }
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'planId', 'xqQg5FS2LkCp935s-FIFm2QAFkHM'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bucketId', 'hsOf2dhOJkqyYYZEtdzDe2QAIUCR'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'title', 'Update client list'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'assignments.fbab97d0-4932-4511-b675-204639209557."@odata.type"', '#microsoft.graph.plannerAssignment'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'assignments.fbab97d0-4932-4511-b675-204639209557.orderHint', ' !'
-- POST the JSON to https://graph.microsoft.com/v1.0/planner/tasks
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://graph.microsoft.com/v1.0/planner/tasks', @json, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
EXEC sp_OASetProperty @json, 'EmitCompact', 0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Failed, response status code = ' + @iTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- A sample response:
-- (See code for parsing this response below..)
-- {
-- "createdBy": {
-- "user": {
-- "id": "6463a5ce-2119-4198-9f2a-628761df4a62"
-- }
-- },
-- "planId": "xqQg5FS2LkCp935s-FIFm2QAFkHM",
-- "bucketId": "hsOf2dhOJkqyYYZEtdzDe2QAIUCR",
-- "title": "Update client list",
-- "orderHint": "85752723360752+",
-- "createdDateTime": "2015-03-25T18:36:49.2407981Z",
-- "assignments": {
-- "fbab97d0-4932-4511-b675-204639209557": {
-- "@odata.type": "#microsoft.graph.plannerAssignment",
-- "assignedBy": {
-- "user": {
-- "id": "6463a5ce-2119-4198-9f2a-628761df4a62"
-- }
-- },
-- "assignedDateTime": "2015-03-25T18:36:49.2407981Z",
-- "orderHint": "RWk1"
-- }
-- },
-- "id":"01gzSlKkIUSUl6DF_EilrmQAKDhh"
-- }
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
DECLARE @createdByUserId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @createdByUserId OUT, 'createdBy.user.id'
DECLARE @planId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @planId OUT, 'planId'
DECLARE @bucketId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @bucketId OUT, 'bucketId'
DECLARE @title nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @title OUT, 'title'
DECLARE @orderHint nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @orderHint OUT, 'orderHint'
DECLARE @createdDateTime nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @createdDateTime OUT, 'createdDateTime'
DECLARE @assignments_odataType nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @assignments_odataType OUT, 'assignments.fbab97d0-4932-4511-b675-204639209557."@odata.type"'
DECLARE @assignmentsAssignedByUserId nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @assignmentsAssignedByUserId OUT, 'assignments.fbab97d0-4932-4511-b675-204639209557.assignedBy.user.id'
DECLARE @assignmentsAssignedDateTime nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @assignmentsAssignedDateTime OUT, 'assignments.fbab97d0-4932-4511-b675-204639209557.assignedDateTime'
DECLARE @assignmentsOrderHint nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @assignmentsOrderHint OUT, 'assignments.fbab97d0-4932-4511-b675-204639209557.orderHint'
DECLARE @id nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'id'
PRINT 'Success.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @resp
END
GO