Sample code for 30+ languages & platforms
SQL Server

Populi Search People

See more Populi Examples

Demonstrates the Populi searchPeople task.

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 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', 'searchPeople'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'search_term', 'Robert'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'limit', '10'

    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="UTF-8"?>
    --  <response>
    -- 	<person>
    -- 		<id>11111</id>
    -- 		<first_name>Robert</first_name>
    -- 		<last_name>McStudent</last_name>
    -- 		<middle_name>Kensington</middle_name>
    -- 		<preferred_name>Bobby</preferred_name>
    -- 		<is_user>1</is_user>
    -- 		<primary_email>r.mcstudent@myschool.edu</primary_email>
    -- 	</person>
    -- 	<person>
    -- 		<id>2222</id>
    -- 		<first_name>Robert</first_name>
    -- 		<last_name>McBoardmember</last_name>
    -- 		<middle_name/>
    -- 		<preferred_name/>
    -- 		<is_user>0</is_user>
    -- 		<primary_email>robert@gmail.com</primary_email>
    -- 	</person>
    -- </response>

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @id int

    DECLARE @first_name nvarchar(4000)

    DECLARE @last_name nvarchar(4000)

    DECLARE @middle_name nvarchar(4000)

    DECLARE @preferred_name nvarchar(4000)

    DECLARE @is_user int

    DECLARE @primary_email nvarchar(4000)

    SELECT @i = 0
    EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @count_i OUT, 'person'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @xml, 'I', @i
        EXEC sp_OAMethod @xml, 'GetChildIntValue', @id OUT, 'person[i]|id'
        EXEC sp_OAMethod @xml, 'GetChildContent', @first_name OUT, 'person[i]|first_name'
        EXEC sp_OAMethod @xml, 'GetChildContent', @last_name OUT, 'person[i]|last_name'
        EXEC sp_OAMethod @xml, 'GetChildContent', @middle_name OUT, 'person[i]|middle_name'
        EXEC sp_OAMethod @xml, 'GetChildContent', @preferred_name OUT, 'person[i]|preferred_name'
        EXEC sp_OAMethod @xml, 'GetChildIntValue', @is_user OUT, 'person[i]|is_user'
        EXEC sp_OAMethod @xml, 'GetChildContent', @primary_email OUT, 'person[i]|primary_email'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @rest


END
GO