Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- public static string getstudentId = string.Empty;
- public static string getStudentcurrentStatus = string.Empty;
- public static string getStudenAddInf = string.Empty;
- public static string getLookid = string.Empty;
- public static string getStudenLoc = string.Empty;
- public static int strFileCount;
- public static string logFilePath = @"D:LogsLog-" + System.DateTime.Today.ToString("MM-dd-yyyy") + "." + "txt";//required to provide this path for storing logs
- public static string subfolderName = string.Empty;
- public static int Parentfilescount;
- public static string educationsection = "ABC";//metadata column name has to be initialize here
- public static void Main(string[] args)
- {
- //enter library name from where you want to copy
- /
- string strDislib = "Test Doc"; //enter destination library to where you want to copy
- string userName = "XXXXXXXXXXXXXXXX"; //login credentials
- Console.WriteLine("Enter your password.");
- SecureString password = GetPassword();
- try
- {
- using (var clientContext = new ClientContext("XXXXXXXXXXXXX/")) // operating site URL
- {
- string createtxt = "";
- WriteLog(createtxt);
- clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
- List list = clientContext.Web.Lists.GetByTitle(strDislib);
- // GetTermIdForTerm(clientContext);
- getfilefromSource(clientContext, strDislib, strSourcelib); // initial point for the moving of documents
- // testmethod(clientContext, strDislib, strSourcelib);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error is: " + ex.Message);
- string errorMsg = ex.Message;
- WriteLog("Error :" + errorMsg);
- }
- }
- private static void getfilefromSource(ClientContext clientContext, string strDislib, string strSourcelib)
- {
- try
- {
- string targetFileUrl = "XXXXXXXXXXXXXXXXXXX" + strDislib + "/";
- List list = clientContext.Web.Lists.GetByTitle(strSourcelib);
- List dislist = clientContext.Web.Lists.GetByTitle(strDislib);
- clientContext.Load(dislist);
- clientContext.Load(dislist.RootFolder);
- clientContext.Load(dislist.RootFolder.Folders);
- clientContext.Load(dislist.RootFolder.Files);
- clientContext.Load(list);
- clientContext.Load(list.RootFolder);
- clientContext.Load(list.RootFolder.Folders); // we are loading all folders
- clientContext.Load(list.RootFolder.Files); // loading files
- clientContext.ExecuteQuery();
- FolderCollection fcol = list.RootFolder.Folders;
- List<string> lstFile = new List<string>();
- int i = 0;
- foreach (Folder f in fcol) //iterating through folder collection
- {
- if (f.Name.ToString().Trim() != "Forms")
- {
- string getstudentId = string.Empty;
- string getStudentcurrentStatus = string.Empty;
- string getStudenAddInf = string.Empty;
- string getStudenLoc = string.Empty;
- string strStuid = string.Empty;
- string strStuName = string.Empty;
- string strStuIdName = string.Empty;
- string foldername = string.Empty;
- subfolderName = string.Empty;
- foldername = f.Name.ToString();
- int getcount = foldername.Length - 6 ;(eg:12345, Sachin Reddy)
- strStuid = foldername.Substring(0, 5); //Extracting Student ID
- strStuName = foldername.Substring(6, getcount); //Extracting Student Name
- //strStuName= strStuName.First().ToString().ToUpper() +String.Join("", strStuName.Skip(1)).ToLower();// fisrt Uppercase,remaining Lowercase
- strStuIdName = strStuid + " - " + strStuName; //40581-student name
- // Forms
- if (i < 500)
- {
- moveDoc(clientContext, strDislib, strSourcelib, f, targetFileUrl, dislist, subfolderName, strStuIdName, strStuName, strStuid);//parent level folder Files will move alone
- string getfolderName = f.Name.ToString();
- FolderCollection subfcol = f.Folders;
- clientContext.Load(subfcol);
- clientContext.Load(subfcol);
- clientContext.ExecuteQuery();
- int subfolderCount = subfcol.Count;
- 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
- {
- Web web = clientContext.Web;
- var folder = web.GetFolderByServerRelativeUrl(f.ServerRelativeUrl);
- folder.DeleteObject();
- web.Context.ExecuteQuery();
- string logmsg = "Deleted folder from source " + f.Name;
- Console.WriteLine(logmsg);
- WriteLog(logmsg);
- }
- else
- {
- string logInsidesubfolder1 = f.Name + " Subfolder is available for current folder and hence cannot delete current folder";
- Console.WriteLine(logInsidesubfolder1);
- // WriteLog(logInsidesubfolder);
- foreach (Folder subf in subfcol)
- {
- subfolderName = subf.Name.ToString();
- string logInsidesubfolder = subf.Name + " Subfolder is available. Reading files in Subfolder has been initialized";
- Console.WriteLine(logInsidesubfolder);
- WriteLog(logInsidesubfolder);
- moveDoc(clientContext, strDislib, strSourcelib, subf, targetFileUrl, dislist, subfolderName, strStuIdName, strStuName, strStuid);//Moving Subfolder documents
- }
- 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
- }
- }
- Console.WriteLine("All files moved ,folder Name :" + f.Name.ToString());//confirmation message for moving all files
- Console.WriteLine("Count :" + i);
- i++;
- }
- }
- }
- catch (Exception e)
- {
- string errorMsg = e.Message;
- WriteLog("Error :" + errorMsg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement