How to Move or rename item (File or Folder) — OneDrive Connector for C#

Introduction

In this article we will delve deeper into OneDrive and C# integration, and will learn how to move or rename item (file or folder). We are continuing from where we left off. By this time, you must have created ODBC Data Source and connected to OneDrive. So, let's not waste time and begin.

Use Query Builder to generate SQL query

  1. The first thing you have to do is open Query Builder:

    ZappySys API Driver - OneDrive
    OneDrive Connector can be used to integrate OneDrive and your defined data source, e.g. Microsoft SQL, Oracle, Excel, Power BI, etc. Get, write, delete OneDrive data in a few clicks!
    OnedriveDSN
    Open Query Builder in API ODBC Driver to read and write data to REST API
  2. Then simply select the Move or rename item (File or Folder) endpoint (action).

  3. Continue by configuring the Required parameters. You can also set optional parameters too.

  4. Move on by hitting Preview Data button to preview the results.

  5. If you see the results you need, simply copy the generated query:

    Move or rename item (File or Folder)
    Required Parameters
    Operation Select the value from the dropdown
    Source Drive Id Select the value from the dropdown
    Source File or Folder Id you like to move or rename Select the value from the dropdown
    Target Folder Id (e.g. 01R65Qxxxxxxx123 -OR- root) - Max 200 items listed Select the value from the dropdown
    Target Item Name (Optional for Move - e.g. Myfile.pdf or MyFolder) Select the value from the dropdown
    Optional Parameters
    Default Group or User Id (additional Scopes needed to list - If fails enter manually)
    Target Drive Id (Select if different than source DriveId)
    Source Search Type (For UI Only - Default=Recursive - i.e. Blank)
    Source Search Folder (For UI Only - Helps to narrow down File Selection DropDown) - Max 200 Listed
    Target Search Type (For UI Only - Default=Recursive - i.e. Blank)
    Target Search Folder (For UI Only - Helps to narrow down File Selection DropDown) - Max 200 Listed
    Prefer Async Operation (Enable if you get error)
    SELECT * FROM move_rename_item
    WITH(
        --Source Drive
    	  DriveId='me'
    	--OR--
    	--DriveId='b!7HBaTtrbekqMYJQ-OqV5Q3hrcOIQoyhGiAoWjqWFenIlIJ-Us7DMQ6jvyrsWMJPx'
    	
    	--Source Item
    	--Item Id (File or Folder) you like to move or rename
    	, ItemId='01R65QTTRARZ42C4BN6FF2WOH3AONX4GUW'
    
    	--Target Drive Id (Optional): Only needed if moving file to a different Drive (e.g. another User's drive)
    	--, TargetDriveId='me'
    	--OR
    	--, TargetDriveId='b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx'
    
    	--Target Folder
    	, TargetFolderId='01R65QTTTBPH6V2AP36VD33CYYDXJSNHLN'
    	--OR (for moving to root)
    	--, TargetFolderId='root'
    	
    	--Target Item Name (New Name) after Move (Optional) - If you do not wish to change name after Move then do not supply below
    	--, TargetItemName='new_file_name_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf' 
    	
    	--Uncomment Below if you get error. Usually needed for large file or cross site operations
    	--, Prefer='respond-async'	
    )
    Query Builder
  6. That's it! You can use this query in C#.

Let's not stop here and explore SQL query examples, including how to use them in Stored Procedures and Views (virtual tables) in the next steps.

SQL query examples

Use these SQL queries in your C# data source:

How to Move a File or Folder in OneDrive

This example shows how to move a file or folder to a different location in OneDrive. It also shows how to use Placeholder Functions to name file daynamically. For example to add timestamp it uses <<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>

SELECT * FROM move_rename_item
WITH(
    --Source Drive
	  DriveId='me'
	--OR--
	--DriveId='b!7HBaTtrbekqMYJQ-OqV5Q3hrcOIQoyhGiAoWjqWFenIlIJ-Us7DMQ6jvyrsWMJPx'
	
	--Source Item
	--Item Id (File or Folder) you like to move or rename
	, ItemId='01R65QTTRARZ42C4BN6FF2WOH3AONX4GUW'

	--Target Drive Id (Optional): Only needed if moving file to a different Drive (e.g. another User's drive)
	--, TargetDriveId='me'
	--OR
	--, TargetDriveId='b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx'

	--Target Folder
	, TargetFolderId='01R65QTTTBPH6V2AP36VD33CYYDXJSNHLN'
	--OR (for moving to root)
	--, TargetFolderId='root'
	
	--Target Item Name (New Name) after Move (Optional) - If you do not wish to change name after Move then do not supply below
	--, TargetItemName='new_file_name_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf' 
	
	--Uncomment Below if you get error. Usually needed for large file or cross site operations
	--, Prefer='respond-async'	
)

How to Rename a File or Folder in OneDrive

This example shows how to rename a file or folder and you can also move to a different location in OneDrive.. It also shows how to use Placeholder Functions to name file daynamically. For example to add timestamp it uses <<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>

SELECT * FROM move_rename_item
WITH(
    --Source Drive
	DriveId='me'
	--OR--
	--DriveId='b!7HBaTtrbekqMYJQ-OqV5Q3hrcOIQoyhGiAoWjqWFenIlIJ-Us7DMQ6jvyrsWMJPx'

	--Source Item you like to rename or move
	, ItemId='01R65QTTRARZ42C4BN6FF2WOH3AONX4GUW'

	--Target Drive Id (Optional): Only needed if moving file to a different Drive (e.g. another User's drive)
    --, TargetDriveId='me'
	--OR
	--, TargetDriveId='b!0zqXLXXJh0uxxxxxxxxxxxxxxxxxxxxx'
	
	--Target Folder (Optional): If you are doing Rename and Move both in one action then supply new Folder Id too else keep it blank for just rename
	--, TargetFolderId='01R65QTTTBPH6V2AP36VD33CYYDXJSNHLN'
	--, TargetFolderId='root'
	
	--Target Item Name (New name)
	, TargetItemName='new_file_name_<<yyyy-MM-dd-HH-mm-ss-fff,FUN_NOW>>.pdf' 
	
	--Uncomment Below if you get error. Usually needed for large file or cross site operations
	--, Prefer='respond-async'	
)

Stored Procedures and Views

Create Custom Stored Procedure

You can create procedures to encapsulate custom logic and then only pass handful parameters rather than long SQL to execute your API call.

Steps to create Custom Stored Procedure in ZappySys Driver. You can insert Placeholders anywhere inside Procedure Body. Read more about placeholders here

  1. Go to Custom Objects Tab and Click on Add button and Select Add Procedure:
    ZappySys Driver - Add Stored Procedure

  2. Enter the desired Procedure name and click on OK:
    ZappySys Driver - Add Stored Procedure Name

  3. Select the created Stored Procedure and write the your desired stored procedure and Save it and it will create the custom stored procedure in the ZappySys Driver:
    Here is an example stored procedure for ZappySys Driver. You can insert Placeholders anywhere inside Procedure Body. Read more about placeholders here

    CREATE PROCEDURE [usp_get_orders]
        @fromdate = '<<yyyy-MM-dd,FUN_TODAY>>'
     AS
        SELECT * FROM Orders where OrderDate >= '<@fromdate>';
    

    ZappySys Driver - Create Custom Stored Procedure

  4. That's it now go to Preview Tab and Execute your Stored Procedure using Exec Command. In this example it will extract the orders from the date 1996-01-01:

    Exec usp_get_orders '1996-01-01';

    ZappySys Driver - Execute Custom Stored Procedure

  5. Let's generate the SQL Server Query Code to make the API call using stored procedure. Go to Code Generator Tab, select language as SQL Server and click on Generate button the generate the code.
    As we already created the linked server for this Data Source, in that you just need to copy the Select Query and need to use the linked server name which we have apply on the place of [MY_API_SERVICE] placeholder.

    SELECT * FROM OPENQUERY([LS_TO_ONEDRIVE_IN_GATEWAY], 'EXEC usp_get_orders @fromdate=''1996-07-30''')

    ZappySys Driver - Generate SQL Server Query

  6. Now go to SQL served and execute that query and it will make the API call using stored procedure and provide you the response.
    ZappySys Driver - Generate SQL Server Query

Create Custom Virtual Table

ZappySys API Drivers support flexible Query language so you can override Default Properties you configured on Data Source such as URL, Body. This way you don't have to create multiple Data Sources if you like to read data from multiple EndPoints. However not every application support supplying custom SQL to driver so you can only select Table from list returned from driver.

If you're dealing with Microsoft Access and need to import data from an SQL query, it's important to note that Access doesn't allow direct import of SQL queries. Instead, you can create custom objects (Virtual Tables) to handle the import process.

Many applications like MS Access, Informatica Designer wont give you option to specify custom SQL when you import Objects. In such case Virtual Table is very useful. You can create many Virtual Tables on the same Data Source (e.g. If you have 50 URLs with slight variations you can create virtual tables with just URL as Parameter setting.

  1. Go to Custom Objects Tab and Click on Add button and Select Add Table:
    ZappySys Driver - Add Table

  2. Enter the desired Table name and click on OK:
    ZappySys Driver - Add Table Name

  3. And it will open the New Query Window Click on Cancel to close that window and go to Custom Objects Tab.

  4. Select the created table, Select Text Type AS SQL and write the your desired SQL Query and Save it and it will create the custom table in the ZappySys Driver:
    Here is an example SQL query for ZappySys Driver. You can insert Placeholders also. Read more about placeholders here

    SELECT
      "ShipCountry",
      "OrderID",
      "CustomerID",
      "EmployeeID",
      "OrderDate",
      "RequiredDate",
      "ShippedDate",
      "ShipVia",
      "Freight",
      "ShipName",
      "ShipAddress",
      "ShipCity",
      "ShipRegion",
      "ShipPostalCode"
    FROM "Orders"
    Where "ShipCountry"='USA'

    ZappySys Driver - Create Custom Table

  5. That's it now go to Preview Tab and Execute your custom virtual table query. In this example it will extract the orders for the USA Shipping Country only:

    SELECT * FROM "vt__usa_orders_only"

    ZappySys Driver - Execute Custom Virtual Table Query

  6. Let's generate the SQL Server Query Code to make the API call using stored procedure. Go to Code Generator Tab, select language as SQL Server and click on Generate button the generate the code.
    As we already created the linked server for this Data Source, in that you just need to copy the Select Query and need to use the linked server name which we have apply on the place of [MY_API_SERVICE] placeholder.

    SELECT * FROM OPENQUERY([LS_TO_ONEDRIVE_IN_GATEWAY], 'EXEC [usp_get_orders] ''1996-01-01''')

    ZappySys Driver - Generate SQL Server Query

  7. Now go to SQL served and execute that query and it will make the API call using stored procedure and provide you the response.
    ZappySys Driver - Generate SQL Server Query

Move or rename item (File or Folder) in C#

We are at the point where we can reiterate on how to use the SQL query, Stored Procedures or Views in C#:

  1. Create a new Console project and use this code to read the data:

    "OnedriveDSN";
    Make ZappySys Driver call in c#

  2. Press F5 to run the code and read the data:
    ZappySys Driver Output in c#

  3. Here is the code in text format:

    using System;
    using System.Data.Odbc;
    
    namespace ConsoleApp {
        class Program {
            static void Main() {
                var dsn = "OnedriveDSN";
                using (var conn = new OdbcConnection(String.Format("DSN={0}", dsn)))
                {
                    conn.Open();
                    var cmd = new OdbcCommand("SELECT * FROM Products", conn);
                    
                    //Increase the timeout duration from the default 30 seconds, which may be insufficient in certain scenarios
                    cmd.CommandTimeout=600; // 600-Seconds
                    
                    var rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        for (int i = 0; i < rdr.FieldCount; i++)
                        {
                                Console.Write("{0}\t", rdr[i]);
                        }
                        Console.WriteLine();
                    }
                }
                Console.ReadKey();
            }
        }
    }
    

More integrations

Other application integration scenarios for OneDrive

All
Data Integration
Database
BI & Reporting
Productivity
Programming Languages
Automation & Scripting
ODBC applications