Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
How programmatically copy or move a file or folder in SharePoint Online - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2014-09-02

It seems like the new O365 Client library has got some more features now and one of the commonly used Copy / Move the files or folders functionality from a library to a new destination is available now.

A new utility class Microsoft.SharePoint.Client.MoveCopyUtil is available on the latest version of the O365 Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.MoveCopyUtil has the following functions:

  • public static void MoveFile(ClientRuntimeContext context, string srcUrl, string destUrl)
  • public static void CopyFile(ClientRuntimeContext context, string srcUrl, string destUrl)
  • public static void MoveFolder(ClientRuntimeContext context, string srcUrl, string destUrl)
  • public static void CopyFolder(ClientRuntimeContext context, string srcUrl, string destUrl)

I just tried the above method for the DocumentSet / Files and it seems to be Copying / Moving the objects with the metadata and also fires the Remote event receivers.

Example:

ClientContext context = new ClientContext("https://****.sharepoint.com/sites/*/*");
string password = "***";
var secure = new SecureString();
foreach (var c in password.ToCharArray()) secure.AppendChar(c);
 
context.Credentials = new SharePointOnlineCredentials("*@*.onmicrosoft.com", secure);
context.Load(context.Web, w => w.Title, w => w.Id);
context.Load(context.Site, s => s.Id);
                       
ClientAction query = new ClientActionInvokeStaticMethod(context, "{c668c5ca-bbdd-435f-8008-502f3180cf20}", "CopyFolder", new object[]
{
"https://***.sharepoint.com/sites/*/**/*/*/<<lib>>/PWR1_2", // PWR1_2 => Source DocumentSet
"https://****.sharepoint.com/sites/*/*/*/*/<<lib>>/PWR1_2_Copied"// PWR1_2_Copied => Copied DocumentSet
});
context.AddQuery(query); 
 
context.ExecuteQuery();

The above script copies the PWR1_2 DocumentSet in the same library as PWR1_2_Copied with all the metadata and files within it.

Note: This feature is not yet published yet. 
(Visited 2,513 times, 1 visits today)

About the author 

Balamurugan Kailasam

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.