SQL Server guide

Copy a file or folder


Copies a file or folder to a different location (same drive or different drive). Supports dynamic naming using placeholder functions.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

SELECT *
FROM copy_item
WITH
(
    -- ##### SOURCE #####
    -- Source Site (leave blank to use default)
    -- , SiteId = 'abc.sharepoint.com,5304daff-xxxxxxxxxx'

    , DriveId = 'b!0zqxxxxxxxxxxxxxxxxxxxxxxxx'

    -- Item to copy (choose ONE)
    -- Path format: root:/path/to/item:
    , ItemId  = 'root:/myfile.pdf:'
	-- , ItemId  = 'root:/myfolder/myfile.pdf:'
    -- , ItemId = '01R65QTTRARZ42C4BN6FF2WOH3AONX4GUW'  -- by ID (preferred)


    -- ##### TARGET #####
    -- Target Site (optional, only used to resolve TargetDriveId)
    -- , TargetSiteId = 'xyz.sharepoint.com,5304daff-xxxxxxxxxx'

    -- Target drive (only required when copying to a different drive)
    -- , TargetDriveId = 'b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx'

    -- ##### TARGET FOLDER #####
    -- Choose ONE format: Path OR Id
    -- NOTE: Use ONLY ONE TargetType block

    , TargetType       = 'Path'
    , TargetFolderPath = '/somefolder'
	-- , TargetFolderPath = '/somefolder/subfolder'
    -- , TargetFolderPath = '/somefolder/<<yyyy-MM-dd,FUN_NOW>>'  -- folder must exist

    -- , TargetType     = 'Id'
    -- , TargetFolderId = '01R65QTTTBPH6V2AP36VD33CYYDXJSNHLN'
    -- , TargetFolderId = 'root'

    -- ##### OPTIONS #####
    -- New name for copied item (omit to keep source name)
    , TargetItemName = 'new_file_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf'

    -- What to do if target item already exists
    -- Ignored if TargetItemName is supplied
    -- , ConflictBehavior = 'replace'    -- fail | rename | replace

    -- Required for large files, folders, or cross-site/drive copy (async)
    -- , Prefer = 'respond-async'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY], 'SELECT *
FROM copy_item
WITH
(
    -- ##### SOURCE #####
    -- Source Site (leave blank to use default)
    -- , SiteId = ''abc.sharepoint.com,5304daff-xxxxxxxxxx''

    , DriveId = ''b!0zqxxxxxxxxxxxxxxxxxxxxxxxx''

    -- Item to copy (choose ONE)
    -- Path format: root:/path/to/item:
    , ItemId  = ''root:/myfile.pdf:''
	-- , ItemId  = ''root:/myfolder/myfile.pdf:''
    -- , ItemId = ''01R65QTTRARZ42C4BN6FF2WOH3AONX4GUW''  -- by ID (preferred)


    -- ##### TARGET #####
    -- Target Site (optional, only used to resolve TargetDriveId)
    -- , TargetSiteId = ''xyz.sharepoint.com,5304daff-xxxxxxxxxx''

    -- Target drive (only required when copying to a different drive)
    -- , TargetDriveId = ''b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx''

    -- ##### TARGET FOLDER #####
    -- Choose ONE format: Path OR Id
    -- NOTE: Use ONLY ONE TargetType block

    , TargetType       = ''Path''
    , TargetFolderPath = ''/somefolder''
	-- , TargetFolderPath = ''/somefolder/subfolder''
    -- , TargetFolderPath = ''/somefolder/<<yyyy-MM-dd,FUN_NOW>>''  -- folder must exist

    -- , TargetType     = ''Id''
    -- , TargetFolderId = ''01R65QTTTBPH6V2AP36VD33CYYDXJSNHLN''
    -- , TargetFolderId = ''root''

    -- ##### OPTIONS #####
    -- New name for copied item (omit to keep source name)
    , TargetItemName = ''new_file_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf''

    -- What to do if target item already exists
    -- Ignored if TargetItemName is supplied
    -- , ConflictBehavior = ''replace''    -- fail | rename | replace

    -- Required for large files, folders, or cross-site/drive copy (async)
    -- , Prefer = ''respond-async''
)')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM copy_item
WITH
(
    -- ##### SOURCE #####
    -- Source Site (leave blank to use default)
    -- , SiteId = ''abc.sharepoint.com,5304daff-xxxxxxxxxx''

    , DriveId = ''b!0zqxxxxxxxxxxxxxxxxxxxxxxxx''

    -- Item to copy (choose ONE)
    -- Path format: root:/path/to/item:
    , ItemId  = ''root:/myfile.pdf:''
	-- , ItemId  = ''root:/myfolder/myfile.pdf:''
    -- , ItemId = ''01R65QTTRARZ42C4BN6FF2WOH3AONX4GUW''  -- by ID (preferred)


    -- ##### TARGET #####
    -- Target Site (optional, only used to resolve TargetDriveId)
    -- , TargetSiteId = ''xyz.sharepoint.com,5304daff-xxxxxxxxxx''

    -- Target drive (only required when copying to a different drive)
    -- , TargetDriveId = ''b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx''

    -- ##### TARGET FOLDER #####
    -- Choose ONE format: Path OR Id
    -- NOTE: Use ONLY ONE TargetType block

    , TargetType       = ''Path''
    , TargetFolderPath = ''/somefolder''
	-- , TargetFolderPath = ''/somefolder/subfolder''
    -- , TargetFolderPath = ''/somefolder/<<yyyy-MM-dd,FUN_NOW>>''  -- folder must exist

    -- , TargetType     = ''Id''
    -- , TargetFolderId = ''01R65QTTTBPH6V2AP36VD33CYYDXJSNHLN''
    -- , TargetFolderId = ''root''

    -- ##### OPTIONS #####
    -- New name for copied item (omit to keep source name)
    , TargetItemName = ''new_file_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf''

    -- What to do if target item already exists
    -- Ignored if TargetItemName is supplied
    -- , ConflictBehavior = ''replace''    -- fail | rename | replace

    -- Required for large files, folders, or cross-site/drive copy (async)
    -- , Prefer = ''respond-async''
)'
EXEC (@MyQuery) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY]