Sample code for 30+ languages & platforms
SQL Server

SharePoint Online Authentication

Demonstrates how to authenticate with SharePoint Online. The end result of authentication is to establish an HTTP cookie, named "SPOIDCRL", that contains a binary security token to be sent in subsequent SharePoint HTTPS requests.

The SPOIDCRL cookie can be persisted to a file and re-used in subsequent application runs.

Important: This example uses a method, SharePointOnlineAuth, that is introduced in Chilkat v9.5.0.73.

Also Important: Chilkat realizes there may be cases where your situation is such that it does not work, and something additional is required to make it work. If the example does not work out-of-the-box, please send email to support@chilkatsoft.com. The information logged to the LastErrorText property has been carefully crafted to help solve problems quickly. Also, the JSON argument to the SharePointOnlineAuth method provides a means to include additional information, whatever it may be, to handle future unknown situations.

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 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

    -- Change these things...
    -- Make sure to use "https" and end in a "/".
    DECLARE @siteUrl nvarchar(4000)
    SELECT @siteUrl = 'https://mydomain.sharepoint.com/'
    -- The username is an email address.
    DECLARE @username nvarchar(4000)
    SELECT @username = 'username@mydomain.com'

    -- The jsonExtra is for future use, if extra information is required for particular integrations.
    DECLARE @jsonExtra int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonExtra OUT

    DECLARE @ssPassword int
    EXEC @hr = sp_OACreate 'Chilkat.SecureString', @ssPassword OUT

    EXEC sp_OAMethod @ssPassword, 'Append', @success OUT, 'mypassword'

    -- Set properties to save/send cookies, and specify a cookie directory.
    -- If the CookieDir = "memory", then the SPOIDCRL cookie is persisted in memory for this HTTP object instance only.
    -- To persist the SharePoint authentication cookie for other HTTP objects, and for future application runs,
    -- set the CookieDir equal to a directory path (not a specific file path, but a directory path where cookie files
    -- are to be created).
    EXEC sp_OASetProperty @http, 'SaveCookies', 1
    EXEC sp_OASetProperty @http, 'SendCookies', 1
    EXEC sp_OASetProperty @http, 'CookieDir', 'memory'

    -- The SharePointOnlineAuth method is introduced in Chilkat v9.5.0.73
    EXEC sp_OAMethod @http, 'SharePointOnlineAuth', @success OUT, @siteUrl, @username, @ssPassword, @jsonExtra
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonExtra
        EXEC @hr = sp_OADestroy @ssPassword
        RETURN
      END


    PRINT 'Success! We have the SPOIDCRL cookie...'

    -- -----------------------------------------------------------------------------------------------------------
    -- IMPORTANT:
    -- The purpose of the SharePointOnlineAuth method is to establish the SPOIDCRL cookie that will automatically be
    -- included in subsequent requests using the *** same HTTP object instance. ***
    -- If later you create a new HTTP object instance, then you'll need to re-establish the SPOIDCRL cookie before sending 
    -- a Sharepoint HTTP request.
    -- -----------------------------------------------------------------------------------------------------------

    -- SharePoint authenticated requests may now be sent because the authentication cookie is automatically included.
    -- For example:
    DECLARE @sbResponseXml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseXml OUT

    -- If your Sharepoint site is within a site collection, then use "https://mydomain.sharepoint.com/sites/teamA/_api/web/GetFolderByServerRelativeUrl('/sites/teamA/Documents')/Files" where "teamA" is the name of the site.
    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://mydomain.sharepoint.com/_api/web/GetFolderByServerRelativeUrl(''/Documents'')/Files', @sbResponseXml
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonExtra
        EXEC @hr = sp_OADestroy @ssPassword
        EXEC @hr = sp_OADestroy @sbResponseXml
        RETURN
      END


    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    PRINT 'Response status code = ' + @iTmp0

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OAMethod @xml, 'LoadSb', @success OUT, @sbResponseXml, 1
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jsonExtra
    EXEC @hr = sp_OADestroy @ssPassword
    EXEC @hr = sp_OADestroy @sbResponseXml
    EXEC @hr = sp_OADestroy @xml


END
GO