Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. class Program
  2.  
  3.  
  4. {
  5.  
  6.  
  7. public static string getstudentId = string.Empty;
  8.  
  9.  
  10. public static string getStudentcurrentStatus = string.Empty;
  11.  
  12.  
  13. public static string getStudenAddInf = string.Empty;
  14.  
  15.  
  16. public static string getLookid = string.Empty;
  17.  
  18.  
  19. public static string getStudenLoc = string.Empty;
  20.  
  21.  
  22. public static int strFileCount;
  23.  
  24.  
  25. public static string logFilePath = @"D:LogsLog-" + System.DateTime.Today.ToString("MM-dd-yyyy") + "." + "txt";//required to provide this path for storing logs
  26.  
  27.  
  28. public static string subfolderName = string.Empty;
  29.  
  30.  
  31.  
  32.  
  33.  
  34. public static int Parentfilescount;
  35.  
  36.  
  37. public static string educationsection = "ABC";//metadata column name has to be initialize here
  38.  
  39.  
  40.  
  41. public static void Main(string[] args)
  42.  
  43.  
  44. {
  45.  
  46. //enter library name from where you want to copy
  47.  
  48.  
  49. /
  50.  
  51. string strDislib = "Test Doc"; //enter destination library to where you want to copy
  52.  
  53.  
  54.  
  55.  
  56. string userName = "XXXXXXXXXXXXXXXX"; //login credentials
  57.  
  58.  
  59.  
  60.  
  61.  
  62. Console.WriteLine("Enter your password.");
  63.  
  64.  
  65. SecureString password = GetPassword();
  66.  
  67.  
  68. try
  69.  
  70.  
  71. {
  72.  
  73.  
  74. using (var clientContext = new ClientContext("XXXXXXXXXXXXX/")) // operating site URL
  75.  
  76.  
  77. {
  78.  
  79.  
  80. string createtxt = "";
  81.  
  82.  
  83. WriteLog(createtxt);
  84.  
  85.  
  86. clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
  87.  
  88.  
  89. List list = clientContext.Web.Lists.GetByTitle(strDislib);
  90.  
  91.  
  92. // GetTermIdForTerm(clientContext);
  93.  
  94.  
  95. getfilefromSource(clientContext, strDislib, strSourcelib); // initial point for the moving of documents
  96.  
  97. // testmethod(clientContext, strDislib, strSourcelib);
  98.  
  99. }
  100.  
  101.  
  102. }
  103.  
  104.  
  105. catch (Exception ex)
  106.  
  107. {
  108.  
  109.  
  110. Console.WriteLine("Error is: " + ex.Message);
  111.  
  112.  
  113. string errorMsg = ex.Message;
  114.  
  115.  
  116. WriteLog("Error :" + errorMsg);
  117.  
  118.  
  119. }
  120.  
  121.  
  122. }
  123.  
  124. private static void getfilefromSource(ClientContext clientContext, string strDislib, string strSourcelib)
  125.  
  126.  
  127. {
  128. try
  129.  
  130.  
  131. {
  132.  
  133. string targetFileUrl = "XXXXXXXXXXXXXXXXXXX" + strDislib + "/";
  134.  
  135. List list = clientContext.Web.Lists.GetByTitle(strSourcelib);
  136.  
  137. List dislist = clientContext.Web.Lists.GetByTitle(strDislib);
  138.  
  139. clientContext.Load(dislist);
  140.  
  141.  
  142. clientContext.Load(dislist.RootFolder);
  143.  
  144. clientContext.Load(dislist.RootFolder.Folders);
  145.  
  146.  
  147. clientContext.Load(dislist.RootFolder.Files);
  148.  
  149. clientContext.Load(list);
  150.  
  151.  
  152. clientContext.Load(list.RootFolder);
  153.  
  154.  
  155. clientContext.Load(list.RootFolder.Folders); // we are loading all folders
  156.  
  157.  
  158. clientContext.Load(list.RootFolder.Files); // loading files
  159.  
  160. clientContext.ExecuteQuery();
  161.  
  162.  
  163. FolderCollection fcol = list.RootFolder.Folders;
  164.  
  165.  
  166. List<string> lstFile = new List<string>();
  167.  
  168.  
  169. int i = 0;
  170.  
  171.  
  172. foreach (Folder f in fcol) //iterating through folder collection
  173.  
  174. {
  175.  
  176.  
  177. if (f.Name.ToString().Trim() != "Forms")
  178.  
  179.  
  180. {
  181.  
  182. string getstudentId = string.Empty;
  183.  
  184.  
  185. string getStudentcurrentStatus = string.Empty;
  186.  
  187.  
  188. string getStudenAddInf = string.Empty;
  189.  
  190.  
  191. string getStudenLoc = string.Empty;
  192.  
  193.  
  194. string strStuid = string.Empty;
  195.  
  196.  
  197. string strStuName = string.Empty;
  198.  
  199.  
  200. string strStuIdName = string.Empty;
  201.  
  202.  
  203. string foldername = string.Empty;
  204.  
  205.  
  206. subfolderName = string.Empty;
  207.  
  208. foldername = f.Name.ToString();
  209.  
  210.  
  211.  
  212. int getcount = foldername.Length - 6 ;(eg:12345, Sachin Reddy)
  213.  
  214.  
  215. strStuid = foldername.Substring(0, 5); //Extracting Student ID
  216.  
  217.  
  218. strStuName = foldername.Substring(6, getcount); //Extracting Student Name
  219.  
  220.  
  221. //strStuName= strStuName.First().ToString().ToUpper() +String.Join("", strStuName.Skip(1)).ToLower();// fisrt Uppercase,remaining Lowercase
  222.  
  223.  
  224. strStuIdName = strStuid + " - " + strStuName; //40581-student name
  225.  
  226.  
  227.  
  228.  
  229.  
  230. // Forms
  231.  
  232.  
  233. if (i < 500)
  234.  
  235.  
  236. {
  237.  
  238.  
  239. moveDoc(clientContext, strDislib, strSourcelib, f, targetFileUrl, dislist, subfolderName, strStuIdName, strStuName, strStuid);//parent level folder Files will move alone
  240.  
  241.  
  242. string getfolderName = f.Name.ToString();
  243.  
  244.  
  245. FolderCollection subfcol = f.Folders;
  246.  
  247.  
  248. clientContext.Load(subfcol);
  249.  
  250.  
  251. clientContext.Load(subfcol);
  252.  
  253.  
  254. clientContext.ExecuteQuery();
  255.  
  256. int subfolderCount = subfcol.Count;
  257.  
  258.  
  259. if ((subfolderCount == 0) && (Parentfilescount == 0)) // if parent level folder files moving is completed and there are no sub folders then paarent folder will get deleted
  260.  
  261.  
  262. {
  263.  
  264.  
  265. Web web = clientContext.Web;
  266.  
  267.  
  268. var folder = web.GetFolderByServerRelativeUrl(f.ServerRelativeUrl);
  269.  
  270.  
  271. folder.DeleteObject();
  272.  
  273.  
  274. web.Context.ExecuteQuery();
  275.  
  276.  
  277. string logmsg = "Deleted folder from source " + f.Name;
  278.  
  279.  
  280. Console.WriteLine(logmsg);
  281.  
  282. WriteLog(logmsg);
  283.  
  284.  
  285. }
  286.  
  287.  
  288. else
  289.  
  290.  
  291. {
  292.  
  293.  
  294. string logInsidesubfolder1 = f.Name + " Subfolder is available for current folder and hence cannot delete current folder";
  295.  
  296.  
  297.  
  298.  
  299.  
  300. Console.WriteLine(logInsidesubfolder1);
  301.  
  302.  
  303. // WriteLog(logInsidesubfolder);
  304.  
  305.  
  306. foreach (Folder subf in subfcol)
  307.  
  308. {
  309.  
  310. subfolderName = subf.Name.ToString();
  311.  
  312. string logInsidesubfolder = subf.Name + " Subfolder is available. Reading files in Subfolder has been initialized";
  313.  
  314. Console.WriteLine(logInsidesubfolder);
  315.  
  316.  
  317. WriteLog(logInsidesubfolder);
  318.  
  319.  
  320. moveDoc(clientContext, strDislib, strSourcelib, subf, targetFileUrl, dislist, subfolderName, strStuIdName, strStuName, strStuid);//Moving Subfolder documents
  321.  
  322.  
  323.  
  324. }
  325.  
  326.  
  327. CheckFiles(clientContext, strDislib, strSourcelib, f);//this function will check wheter folder and subfolder are having files or not also delete the tree if there are no files available
  328.  
  329.  
  330. }
  331.  
  332.  
  333. }
  334.  
  335.  
  336. Console.WriteLine("All files moved ,folder Name :" + f.Name.ToString());//confirmation message for moving all files
  337.  
  338.  
  339. Console.WriteLine("Count :" + i);
  340.  
  341.  
  342. i++;
  343.  
  344.  
  345. }
  346.  
  347.  
  348.  
  349.  
  350.  
  351. }
  352.  
  353.  
  354. }
  355.  
  356.  
  357. catch (Exception e)
  358.  
  359.  
  360. {
  361.  
  362.  
  363. string errorMsg = e.Message;
  364.  
  365.  
  366. WriteLog("Error :" + errorMsg);
  367.  
  368.  
  369. }
  370.  
  371.  
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement