Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

SharePoint Get Files in Root Folder

See more SharePoint Examples

Gets the list of files that exist in the root folder.

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

    --  --------------------------------------------------------------------------------------------------------
    --  Provide the information needed for Chilkat to automatically fetch the OAuth2.0 access token as needed.

    --  To create your App Registration for SharePoint in Azure Entra ID,
    --  see How to Create SharePoint App Registration for OAuth 2.0 Client Credentials

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

    EXEC sp_OAMethod @jsonOAuthCC, 'UpdateString', @success OUT, 'client_id', 'CLIENT_ID'
    EXEC sp_OAMethod @jsonOAuthCC, 'UpdateString', @success OUT, 'client_secret', 'SECRET_VALUE'
    EXEC sp_OAMethod @jsonOAuthCC, 'UpdateString', @success OUT, 'scope', 'https://graph.microsoft.com/.default'
    EXEC sp_OAMethod @jsonOAuthCC, 'UpdateString', @success OUT, 'token_endpoint', 'https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token'

    EXEC sp_OAMethod @jsonOAuthCC, 'Emit', @sTmp0 OUT
    EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
    --  --------------------------------------------------------------------------------------------------------

    --  Indicate that we want a JSON reply
    EXEC sp_OASetProperty @http, 'Accept', 'application/json;odata=verbose'

    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'sharepoint_hostname', 'example.sharepoint.com'
    DECLARE @sbJson int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJson OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://graph.microsoft.com/v1.0/sites/SITE_ID/drive/root/children', @sbJson
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonOAuthCC
        EXEC @hr = sp_OADestroy @sbJson
        RETURN
      END

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

    EXEC sp_OAMethod @json, 'LoadSb', @success OUT, @sbJson

    --  Iterate over the results and get each file's name, size, and last-modified date/time.

    DECLARE @numFiles int
    EXEC sp_OAMethod @json, 'SizeOfArray', @numFiles OUT, 'd.results'

    PRINT 'Number of Files in the SharePoint root folder = ' + @numFiles

    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numFiles
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i

        DECLARE @filename nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @filename OUT, 'd.results[i].Name'
        DECLARE @fileRelativeUri nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @fileRelativeUri OUT, 'd.results[i].ServerRelativeUrl'
        DECLARE @fileSize int
        EXEC sp_OAMethod @json, 'IntOf', @fileSize OUT, 'd.results[i].Length'



        PRINT @i + 1 + ': ' + @filename

        PRINT '    Relative URI: ' + @fileRelativeUri

        PRINT '    Size in Bytes: ' + @fileSize

        SELECT @i = @i + 1
      END

    --  The output of this program when I tested it:

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jsonOAuthCC
    EXEC @hr = sp_OADestroy @sbJson
    EXEC @hr = sp_OADestroy @json


END
GO