Rename a file or folder
Renames a file or folder. You can also move it to a different location. 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 move_rename_item
WITH
(
-- ##### SOURCE #####
-- Source Site (leave blank to use default)
-- , SiteId = 'abc.sharepoint.com,5304daff-xxxxxxxxxx'
-- Source Drive
, DriveId = 'b!0zqxxxxxxxxxxxxxxxxxxxxxxxx'
-- Item to move or rename (FILE or FOLDER)
-- 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 required when moving across sites)
-- , TargetSiteId = 'xyz.sharepoint.com,5304daff-xxxxxxxxxx'
-- Target Drive (optional, only required when moving across drives)
-- , TargetDriveId = 'b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx'
-- ##### TARGET FOLDER #####
-- Optional: specify ONLY if moving item to a different 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 #####
-- Required when renaming (omit if only moving)
, TargetItemName =
'new_file_name_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf'
-- Required for large files or cross-site/drive move (async)
-- , Prefer = 'respond-async'
);
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY], 'SELECT *
FROM move_rename_item
WITH
(
-- ##### SOURCE #####
-- Source Site (leave blank to use default)
-- , SiteId = ''abc.sharepoint.com,5304daff-xxxxxxxxxx''
-- Source Drive
, DriveId = ''b!0zqxxxxxxxxxxxxxxxxxxxxxxxx''
-- Item to move or rename (FILE or FOLDER)
-- 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 required when moving across sites)
-- , TargetSiteId = ''xyz.sharepoint.com,5304daff-xxxxxxxxxx''
-- Target Drive (optional, only required when moving across drives)
-- , TargetDriveId = ''b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx''
-- ##### TARGET FOLDER #####
-- Optional: specify ONLY if moving item to a different 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 #####
-- Required when renaming (omit if only moving)
, TargetItemName =
''new_file_name_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf''
-- Required for large files or cross-site/drive move (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 move_rename_item
WITH
(
-- ##### SOURCE #####
-- Source Site (leave blank to use default)
-- , SiteId = ''abc.sharepoint.com,5304daff-xxxxxxxxxx''
-- Source Drive
, DriveId = ''b!0zqxxxxxxxxxxxxxxxxxxxxxxxx''
-- Item to move or rename (FILE or FOLDER)
-- 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 required when moving across sites)
-- , TargetSiteId = ''xyz.sharepoint.com,5304daff-xxxxxxxxxx''
-- Target Drive (optional, only required when moving across drives)
-- , TargetDriveId = ''b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx''
-- ##### TARGET FOLDER #####
-- Optional: specify ONLY if moving item to a different 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 #####
-- Required when renaming (omit if only moving)
, TargetItemName =
''new_file_name_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf''
-- Required for large files or cross-site/drive move (async)
-- , Prefer = ''respond-async''
);'
EXEC (@MyQuery) AT [LS_TO_SHAREPOINT_ONLINE_IN_GATEWAY]