SQL Server
SQL Server
Populi Get Roles
See more Populi Examples
Demonstrates the Populi getRoles task.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 assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- First load the previously obtained API token.
-- See Get Populi Access Token for sample code showing how to get the API token.
DECLARE @xml int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @xml, 'LoadXmlFile', @success OUT, 'qa_data/tokens/populi_token.xml'
DECLARE @accessKey nvarchar(4000)
EXEC sp_OAMethod @xml, 'GetChildContent', @accessKey OUT, 'access_key'
EXEC sp_OAGetProperty @xml, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Did not find the access_key'
EXEC @hr = sp_OADestroy @xml
RETURN
END
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
-- Connect using TLS.
-- A single REST object, once connected, can be used for many Populi REST API calls.
-- The auto-reconnect indicates that if the already-established HTTPS connection is closed,
-- then it will be automatically re-established as needed.
DECLARE @bAutoReconnect int
SELECT @bAutoReconnect = 1
EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'yourcollege.populi.co', 443, 1, @bAutoReconnect
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OASetProperty @rest, 'Authorization', @accessKey
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'task', 'getRoles'
DECLARE @responseBody nvarchar(4000)
EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @responseBody OUT, 'POST', '/api/index.php'
EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- We should expect a 200 response if successful.
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
PRINT 'Request Header: '
EXEC sp_OAGetProperty @rest, 'LastRequestHeader', @sTmp0 OUT
PRINT @sTmp0
PRINT '----'
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'Response StatusCode = ' + @iTmp0
EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
PRINT 'Response StatusLine: ' + @sTmp0
PRINT 'Response Header:'
EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Response Body:'
PRINT @responseBody
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @responseBody
EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
PRINT @sTmp0
-- Sample response:
-- Use this online tool to generate parsing code from sample XML:
-- Generate Parsing Code from XML
-- <?xml version="1.0" encoding="ISO-8859-1"?>
-- <response>
-- <role>
-- <id>7</id>
-- <name>Academic Admin</name>
-- <inactive>0</inactive>
-- </role>
-- <role>
-- <id>19</id>
-- <name>Financial Aid</name>
-- <inactive>0</inactive>
-- </role>
-- <role>
-- <id>15</id>
-- <name>Registrar</name>
-- <inactive>0</inactive>
-- </role>
-- <role>
-- <id>4</id>
-- <name>Staff</name>
-- <inactive>0</inactive>
-- </role>
-- <role>
-- <id>1</id>
-- <name>Everyone</name>
-- <inactive>0</inactive>
-- </role>
-- </response>
DECLARE @i int
DECLARE @count_i int
DECLARE @id int
DECLARE @name nvarchar(4000)
DECLARE @inactive int
SELECT @i = 0
EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @count_i OUT, 'role'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @xml, 'I', @i
EXEC sp_OAMethod @xml, 'GetChildIntValue', @id OUT, 'role[i]|id'
EXEC sp_OAMethod @xml, 'GetChildContent', @name OUT, 'role[i]|name'
EXEC sp_OAMethod @xml, 'GetChildIntValue', @inactive OUT, 'role[i]|inactive'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @rest
END
GO