Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. function moveFiles(sourceLib, destLib){
  2.  
  3. var context = new SP.ClientContext.get_current();
  4. var web = context.get_web();
  5. var folderSrc = web.getFolderByServerRelativeUrl(sourceLib);
  6. context.load(folderSrc,'Files');
  7. context.executeQueryAsync(
  8. function() {
  9. console.log("Got the source folder right here!");
  10. var files = folderSrc.get_files();
  11. var e = files.getEnumerator();
  12. var dest = []; //Just to check it got the file path right
  13. while (e.moveNext()) {
  14. var file = e.get_current();
  15. var destLibUrl = destLib + "/" + file.get_name();
  16. dest.push(destLibUrl); //delete this when we're happy we got the file paths right
  17. file.moveTo(destLibUrl, SP.MoveOperations.overwrite);
  18. }
  19. console.log(dest); //delete this when we're happy we got the file paths right
  20. context.executeQueryAsync(function() { console.log("Files moved successfully!");}, function(sender, args) {console.log("error: ") + args.get_message()});
  21. },
  22. function(sender, args){console.log("Sorry, something messed up: " + args.get_message());}
  23. );
  24.  
  25. //Usage:
  26. var srcLibrary = 'Temp Library';
  27. var destLibrary = 'Pages';
  28.  
  29. moveFiles(srcLibrary, destLibrary);
  30.  
  31. public static void CopyDocuments(string srcUrl, string destUrl, string srcLibrary, string destLibrary, Login _login)
  32. {
  33. // set up the src client
  34. SP.ClientContext srcContext = new SP.ClientContext(srcUrl);
  35. srcContext.AuthenticationMode = SP.ClientAuthenticationMode.FormsAuthentication;
  36. srcContext.FormsAuthenticationLoginInfo = new SP.FormsAuthenticationLoginInfo(_login.UserName, _login.Password);
  37.  
  38. // set up the destination context (in your case there is no needs to create a new context, because it would be the same library!!!!)
  39. SP.ClientContext destContext = new SP.ClientContext(destUrl);
  40. destContext.AuthenticationMode = SP.ClientAuthenticationMode.FormsAuthentication;
  41. destContext.FormsAuthenticationLoginInfo = new SP.FormsAuthenticationLoginInfo(_login.UserName, _login.Password);
  42.  
  43. // get the list and items
  44. SP.Web srcWeb = srcContext.Web;
  45. SP.List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
  46. SP.ListItemCollection col = srcList.GetItems(new SP.CamlQuery());
  47. srcContext.Load(col);
  48. srcContext.ExecuteQuery();
  49.  
  50. // get the new list
  51. SP.Web destWeb = destContext.Web;
  52. destContext.Load(destWeb);
  53. destContext.ExecuteQuery();
  54.  
  55. foreach (var doc in col)
  56. {
  57. try
  58. {
  59. if (doc.FileSystemObjectType == SP.FileSystemObjectType.File)
  60. {
  61. // get the file
  62. SP.File f = doc.File;
  63. srcContext.Load(f);
  64. srcContext.ExecuteQuery();
  65.  
  66. // build new location url
  67. string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + f.Name;
  68.  
  69. // read the file, copy the content to new file at new location
  70. SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
  71. SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
  72. }
  73.  
  74. if (doc.FileSystemObjectType == SP.FileSystemObjectType.Folder)
  75. {
  76. // load the folder
  77. srcContext.Load(doc);
  78. srcContext.ExecuteQuery();
  79.  
  80. // get the folder data, get the file collection in the folder
  81. SP.Folder folder = srcWeb.GetFolderByServerRelativeUrl(doc.FieldValues["FileRef"].ToString());
  82. SP.FileCollection fileCol = folder.Files;
  83.  
  84. // load everyting so we can access it
  85. srcContext.Load(folder);
  86. srcContext.Load(fileCol);
  87. srcContext.ExecuteQuery();
  88.  
  89. foreach (SP.File f in fileCol)
  90. {
  91. // load the file
  92. srcContext.Load(f);
  93. srcContext.ExecuteQuery();
  94.  
  95. string[] parts = null;
  96. string id = null;
  97.  
  98. if (srcLibrary == "My Files")
  99. {
  100. // these are doc sets
  101. parts = f.ServerRelativeUrl.Split('/');
  102. id = parts[parts.Length - 2];
  103. }
  104. else
  105. {
  106. id = folder.Name;
  107. }
  108.  
  109. // build new location url
  110. string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + id + "/" + f.Name;
  111.  
  112. // read the file, copy the content to new file at new location
  113. SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
  114. SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. Log("File Error = " + ex.ToString());
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement