//***********************************add Subfolders in document library if rootfolder path has more than more than one folder******************************// public void CreateSubFolder(ClientContext clientContext, string path, int i, string[] SubFolder, Folder folder, string destinationLigraryTitle) { int cnt = i; cnt++; try { Folder subFolder = folder.Folders.Add(SubFolder[i]); clientContext.ExecuteQuery(); FolderPAthForPermission += "\\" + SubFolder[i]; //_______________Add Permissions for the folder______________________// SpPermissions.GetPermmssion(clientContext, folder, path, destinationLigraryTitle); if (cnt < SubFolder.Length) { CreateSubFolder(clientContext, path, cnt, SubFolder, subFolder, destinationLigraryTitle); } else { System.IO.DirectoryInfo DirectoriInfo = new System.IO.DirectoryInfo(path); UploadFolder(clientContext, DirectoriInfo, subFolder, destinationLigraryTitle); } } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } }
//***********************************add folders in document library if rootfolder path has more than more than one folder******************************// public void CreateFolder(ClientContext clientContext, string path, string destinationLigraryTitle) { try { string[] splitpath = path.Split(Convert.ToChar(92)); Web web = clientContext.Web; var query = clientContext.LoadQuery(web.Lists.Where(p => p.Title == destinationLigraryTitle)); clientContext.ExecuteQuery(); List documentsLibrary = query.FirstOrDefault(); clientContext.Load(documentsLibrary.RootFolder); var folder = documentsLibrary.RootFolder; clientContext.ExecuteQuery(); folder = documentsLibrary.RootFolder.Folders.Add(splitpath[1]); clientContext.ExecuteQuery(); FolderPAthForPermission += splitpath[0] + "\\" + splitpath[1]; //_______________Add Permissions for the folder______________________// SpPermissions.GetPermmssion(clientContext, folder, FolderPAthForPermission, destinationLigraryTitle); if (splitpath.Length >= 2) { CreateSubFolder(clientContext, path, 2, splitpath, folder, destinationLigraryTitle); } else { System.IO.DirectoryInfo DirectoriInfo = new System.IO.DirectoryInfo(path); UploadFolder(clientContext, DirectoriInfo, folder, destinationLigraryTitle); } } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } }
private void LoadSubDirs(string directory, int PresentFolderid, int Rootid) { FileInformation Sub = new FileInformation(); SubFoldersPath.Add(directory); string[] SubdirectoryEntries = Directory.GetDirectories(directory); //Identifying and retriving the all sub folders string[] AllFilesinDirectory = Directory.GetFiles(directory); //Identifying and retriving the all sub files foreach (String FilePath in AllFilesinDirectory) // we have all file now getting file info and upload it into database { try { DAL.Model.FileInfo fileInfo = GetFileInfo(FilePath); //getting File info Sub.UploadFile(fileInfo, PresentFolderid); //upload into database } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } } foreach (string subdirectory in SubdirectoryEntries) //all subfolders { try { SubFolder FolderDetails = GetFolderdetails(subdirectory); //getting sub folder details int FolderID = Sub.UploadSubFolder(FolderDetails, Rootid); //upload the sub folder into datatbase and that return id after insert LoadSubDirs(subdirectory, FolderID, Rootid); //the id is used for uploading file } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } } }
// public int monthvalue; public string Getdata(string Typeofinfo, string Monthvalue) { try { using (SqlConnection connection = new SqlConnection()) { connection.ConnectionString = (databaselink); connection.Open(); string query = "(select count(" + Typeofinfo + ") from FileInfo f where DATEDIFF(month,f." + Typeofinfo + ",GETDATE())<" + Monthvalue + ")"; SqlCommand command = new SqlCommand(query, connection); SqlDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { value = dataReader[0].ToString(); } return(value); } } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); return(null); } }
private SubFolder GetFolderdetails(string directoryPath) // getting sub Folder details { SubFolder Folder = new SubFolder(); try { DirectoryInfo DirectoryInfo = new DirectoryInfo(directoryPath); int LastIndexOf = directoryPath.LastIndexOf("\\"); String FolderName = directoryPath.Substring(LastIndexOf + 1); if (FolderName.Length > 128) //here checking the folder name length { throw new Exception(string.Format("Folder name is above 128 characters")); } else { char[] GetInvalidCharacters = Path.GetInvalidFileNameChars(); //getting all invalid characters if (FolderName.Length - FolderName.Replace(".", "").Length <= 1) //checking the file has more than two dots { foreach (char OneCharacter in GetInvalidCharacters) //checking the each character in file name { if (FolderName.Contains(OneCharacter)) { throw new Exception(string.Format("Folder name can't contains \"*:<>?//\\|. ")); } } Folder.Name = FolderName; } else { throw new Exception(string.Format("Folder name can't contains \"*:<>?//\\|. ")); } } if (directoryPath.Length > 260) //checking the file path length { throw new Exception(string.Format("Folder path can't more than 260 characters")); } else { Folder.Path = directoryPath; } string OwnerOfFolder = System.IO.File.GetAccessControl(directoryPath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); Folder.Path = directoryPath; Folder.CreatedOn = DirectoryInfo.CreationTime; Folder.ModifiedOn = DirectoryInfo.LastAccessTime; Folder.ModifiedBy = OwnerOfFolder; Folder.CreatedBy = OwnerOfFolder; } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } return(Folder); }
/*----------------- For Connectng To Sharepoint After Given Credentials-------------------------*/ private void Btnconnect_Click(object sender, EventArgs e) { UserName = Tbusername.Text; Password = new SecureString(); foreach (char c in Tbpassword.Text) { Password.AppendChar(c); } Url = Tbsiteurl.Text; Lberror.Text = "Please wait connecting to sharepoint"; try { Clientcontext = new ClientContext(Url); Clientcontext.Credentials = new SharePointOnlineCredentials(UserName, Password); } catch (Microsoft.SharePoint.Client.IdcrlException ex) { ErrrorLog.ErrorlogWrite(ex); Lberror.Text = "The sign-in name or password does not match one in the Microsoft account system"; } catch (Microsoft.SharePoint.Client.ClientRequestException ex) { ErrrorLog.ErrorlogWrite(ex); Lberror.Text = "Cannot contact site ysou are specified"; } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); Lberror.Text = "Please check the data what you provided"; } try { /*----------------- Retriving All Document Library's from Sharepoint Assigning To Combobox-------------------------*/ Web Webobject = Clientcontext.Web; var DocumentLibrary = Clientcontext.LoadQuery(Webobject.Lists.Where(l => l.BaseTemplate == 101)); Clientcontext.ExecuteQuery(); IList <string> DocLibraryList = new List <string>(); foreach (var List in DocumentLibrary) { DocLibraryList.Add(List.Title); } Lberror.Text = ""; CbDocumentlibrary.DataSource = DocLibraryList; CbDocumentlibrary.DisplayMember = "Name"; } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); Lberror.Text = "Please check the data what you provided"; } }
public DAL.Model.FileInfo GetFileInfo(string FilePath) //getting file details { DAL.Model.FileInfo Filedetails = new DAL.Model.FileInfo(); try { System.IO.FileInfo File = new System.IO.FileInfo(FilePath); if (File.Name.Length > 128) { throw new Exception(string.Format("File name is above 128 characters")); } else { char[] GetInvalidCharacters = Path.GetInvalidFileNameChars(); if (File.Name.Length - File.Name.Replace(".", "").Length <= 1) { Filedetails.Name = File.Name; foreach (char Charactor in GetInvalidCharacters) { if (File.Name.Contains(Charactor)) { throw new Exception(string.Format("File name can't contains \"*:<>?//\\|. ")); } } } else { throw new Exception(string.Format("File name can't contains \"*:<>?//\\|. ")); } } if (File.DirectoryName.Length > 260) { throw new Exception(string.Format("File path can't more than 260 characters")); } else { Filedetails.Path = File.DirectoryName; } Filedetails.Type = File.Extension; float lengthbytes = (File.Length / 1024f) / 1024f; double length = Math.Round(((File.Length / 1024f) / 1024f), 4, MidpointRounding.AwayFromZero); Filedetails.Size = Convert.ToString(length); Filedetails.Size = Filedetails.Size + " MB"; Filedetails.CreatedOn = File.CreationTime; Filedetails.FileAccessed = File.LastAccessTime; Filedetails.IsFilePathOK = true; Filedetails.IsFilesupported = true; Filedetails.ModifiedOn = File.LastWriteTime; Filedetails.CreatedBy = System.IO.File.GetAccessControl(FilePath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); Filedetails.ModifiedBy = System.IO.File.GetAccessControl(FilePath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } return(Filedetails); }
// ------------------------------Sending the data to database-------------------------// private void Run_Click(object sender, EventArgs e) { SelectFolder selectFolder = new SelectFolder(); try { string folderpath = ""; foreach (string path in RootFoldersPath) // getting each folderpath in list { folderpath = path; } if (folderpath == "" || folderpath == null) // if folderpath is empty { throw new Exception("Please Browse the folder"); //throw exception } else { Errorlabel2.Text = ""; Errorlabel1.Text = ""; string[] subfolders = Directory.GetDirectories(folderpath, "*", SearchOption.AllDirectories); // getting all subfolders into variable string[] files = Directory.GetFiles(folderpath, "*", SearchOption.AllDirectories); // getting all files into variable foreach (String Rootfolder in RootFoldersPath) //Getting each rootfolder from list { try { DAL.Repository.FileInformation dbcall = new DAL.Repository.FileInformation(); RootFolder RootFolderDetails = selectFolder.GetRootFolderdetails(Rootfolder); //Getting rootfolder details int ID = dbcall.UploadRootFolder(RootFolderDetails); //Upload into the database and method is return the inserted id. ID is usefully for files selectFolder.GetSubDirectories(Rootfolder, ID); //Getting all sub folders and files } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } } } /*----------------- Assigning the Data to FolderComboBox Again-------------------------*/ GetRootFolderRepository getRootFolder = new GetRootFolderRepository(); CBfoldername.DataSource = getRootFolder.BindingFolders(); CBfoldername.DisplayMember = "Name"; CBfoldername.ValueMember = "Path"; FileInfoTable = RepObj.GetFileInfoTable(); } catch (Exception Ex) { Errorlabel1.Text = Ex.Message; ErrrorLog.ErrorlogWrite(Ex); } SelectedFolders.Items.Clear(); MessageBox.Show("Run Completed"); }
private void DisplayGrid_Click(object sender, EventArgs e) { try { this.DataGridView.Visible = true; //retreiving the data from the database and Fill the data to Gridview DataSet dataSet = RepObj.DataGrid(InformationBox.SelectedValue.ToString(), MonthsBox.SelectedItem.ToString()); DataGridView.DataSource = dataSet.Tables["FileInfo"]; } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } }
//***********************************Upload Doccument From the directory******************************// public void UploadDocument(ClientContext clientContext, string sourceFilePath, string serverRelativeDestinationPath) { try { using (var FileStream = new FileStream(sourceFilePath, FileMode.Open)) { var fileInfo = new FileInfo(sourceFilePath); Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeDestinationPath, FileStream, true); } } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } }
private void DisplayCountButton_Click(object sender, EventArgs e) { try { //getting the text from (InformationBox)Combobox to the SelectedInfoLabel SelectedInfoLabel.Text = InformationBox.SelectedItem.ToString(); SelectedInfoLabel.Text = InformationBox.SelectedValue.ToString(); SelectedInfoLabel.Visible = true; DisplayGridButton.Visible = true; //this line of code loads the InformationBox data and MonthsBox data and displays it in the DisplayGridButton DisplayGridButton.Text = RepObj.Getdata(InformationBox.SelectedValue.ToString(), MonthsBox.SelectedItem.ToString()); } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } }
// ------------------------------Removing the selected Folders from the list and listbox -------------------------// private void Remove_Click(object sender, EventArgs e) { try { string Folderpath = ""; foreach (string path in RootFoldersPath) // getting each folderpath in list { Folderpath = path; } if ((Folderpath == "" || Folderpath == null) && (SelectedFolders.Text == "" || SelectedFolders.Text == null)) // if browsing textbox is empty { throw new Exception("Please Browse the folder"); } else if (SelectedFolders.Text == "" || SelectedFolders.Text == null || SelectedFolders.SelectedItem.ToString() == null) // if not selected the foldername to remove { throw new Exception("Please Select Folder"); } else { Errorlabel2.Text = ""; Errorlabel1.Text = ""; string foldername = SelectedFolders.SelectedItem.ToString(); // selecting the foldername to remove SelectedFolders.Items.Remove(foldername); // removing the selected folders in listbox String RemovingItem = ""; foreach (String path in RootFoldersPath) // getting each folderpath in list { int LastIndexOf = path.LastIndexOf("\\"); String folderName = path.Substring(LastIndexOf + 1); // getting foldername in list if (folderName.Equals(foldername)) // if selected foldername equals to list foldername { RemovingItem = path; } } RootFoldersPath.Remove(RemovingItem); // removing the selected path from list } } catch (Exception Ex) { Errorlabel1.Text = Ex.Message; // exception message ErrrorLog.ErrorlogWrite(Ex); } }
//*********************************** add subfolder in doccument library and create SubFolder From the directory******************************// public void UploadFolder(ClientContext clientContext, System.IO.DirectoryInfo folderInfo, Folder folder, string destinationLigraryTitle) { System.IO.FileInfo[] Files = null; System.IO.DirectoryInfo[] SubDirs = null; try { Files = folderInfo.GetFiles("*.*"); } catch (UnauthorizedAccessException ex) { ErrrorLog.ErrorlogWrite(ex); } catch (System.IO.DirectoryNotFoundException ex) { ErrrorLog.ErrorlogWrite(ex); } if (Files != null) { foreach (System.IO.FileInfo Fi in Files) { Console.WriteLine(Fi.FullName); clientContext.Load(folder); clientContext.ExecuteQuery(); UploadDocument(clientContext, Fi.FullName, folder.ServerRelativeUrl + "/" + Fi.Name); } SubDirs = folderInfo.GetDirectories(); foreach (System.IO.DirectoryInfo DirectoriInfo in SubDirs) { Folder subFolder = folder.Folders.Add(DirectoriInfo.Name); clientContext.ExecuteQuery(); try { //_______________Add Permissions for the folder______________________// SpPermissions.GetPermmssion(clientContext, subFolder, Path.GetFullPath(DirectoriInfo.ToString()), destinationLigraryTitle); } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } UploadFolder(clientContext, DirectoriInfo, subFolder, destinationLigraryTitle); } } }
// ------------------------------Adding the browsed Folders to the list and listbox -------------------------// private void Add_Click(object sender, EventArgs e) { try { Errorlabel2.Text = ""; Errorlabel1.Text = ""; string folderpath; if (SelectedPathFolder.Text == "" || SelectedPathFolder.Text == null) //throwing error when listbox is empty { throw new Exception("Please Browse the folder"); } else { Errorlabel1.Text = ""; folderpath = SelectedPathFolder.Text; //assigning browsed folderpath to variable RootFoldersPath.Add(folderpath); // adding the selected folderpath to list Boolean IfItemIsExists = false; foreach (string PresentFilePath in RootFoldersPath) // getting each folderpath in list { if (folderpath.Equals(PresentFilePath)) // comparing with folderpath in list with present folderpath { IfItemIsExists = true; } } if (IfItemIsExists) // if exists removing the folderpath in list { RootFoldersPath.Remove(folderpath); int ExistedFilePlace = folderpath.LastIndexOf("\\"); String ExistedfileName = folderpath.Substring(ExistedFilePlace + 1); SelectedFolders.Items.Remove(ExistedfileName); } RootFoldersPath.Add(folderpath); // adding folderpath to list int LastIndexOf = folderpath.LastIndexOf("\\"); // getting foldername instead of taking the folderpath String fileName = folderpath.Substring(LastIndexOf + 1); SelectedFolders.Items.Add(fileName); // adding foldername to listbox SelectedPathFolder.Text = ""; // empty the browsing textbox } } catch (Exception Ex) { Errorlabel1.Text = Ex.Message; // error message if not browsed ErrrorLog.ErrorlogWrite(Ex); // error message to log file } }
/*-----------------retreiving the data from the database and Fill the data to ReportViewer-------------------------*/ //private void LoadProductsReport() //{ // reportViewer1.LocalReport.ReportEmbeddedResource = "RDLCReport.rptProducts.rdlc"; // reportViewer1.LocalReport.DataSources.Clear(); // reportViewer1.LocalReport.DataSources.Add(ReportDataSource("dsProducts", sql_stmt)); //reportViewer1.ProcessingMode = ProcessingMode.Local; ////reportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc"); //DataTable dsCustomers = DatatableObj; //ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers); //reportViewer1.LocalReport.DataSources.Clear(); //reportViewer1.LocalReport.DataSources.Add(datasource); //ReportDataSource datasource = new ReportDataSource("FileInfo", DatatableObj.Rows); // this.reportViewer1.LocalReport.DataSources.Clear(); //this.reportViewer1.LocalReport.DataSources.Add(datasource); // this.reportViewer1.RefreshReport(); //LocalReport r = new LocalReport(); //r.DataSources.Add(rptData); //reportViewer1.LocalReport.DataSources.Add(rptData); //} private void SelectedInfoButton_Click(object sender, EventArgs e) { try { //this.DataGridView.Visible = true; DataSet dataSet = RepObj.GetInformationToRpt(InformationBox.SelectedValue.ToString(), MonthsBox.SelectedItem.ToString()); reportViewer1.Visible = true; reportViewer1.Reset(); reportViewer1.LocalReport.ReportEmbeddedResource = "UploadFolder.Report2.rdlc"; reportViewer1.LocalReport.DataSources.Clear(); ReportDataSource rptData = new ReportDataSource("FileInfo", dataSet); reportViewer1.LocalReport.DataSources.Add(rptData); FileInfoBindingSource.Add(rptData); reportViewer1.LocalReport.Refresh(); } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } }
IList <string> SubFoldersPath = new List <string>(); // list for SubFoldersPath public void GetSubDirectories(String RootFolderPath, int RootFolderId) //It contains the sub folders and files { FileInformation Dbcall = new FileInformation(); string[] SubdirectoryEntries = Directory.GetDirectories(RootFolderPath); //Identifying and retriving the all sub folders string[] AllFilesinDirectory = Directory.GetFiles(RootFolderPath); //Identifying and retriving the all sub files if (AllFilesinDirectory.Length > 0) //checking the root folders is contains the files or not { SubFoldersPath.Add(RootFolderPath); SubFolder FolderDetails = GetFolderdetails(RootFolderPath); //Retriving the folder information int PresentFolderID = Dbcall.UploadSubFolder(FolderDetails, RootFolderId); //upload into the database foreach (String FilePath in AllFilesinDirectory) { try { DAL.Model.FileInfo FileInfo = GetFileInfo(FilePath); //Retriving the file information Dbcall.UploadFile(FileInfo, PresentFolderID); //upload into the database } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } } } foreach (string subdirectory in SubdirectoryEntries) { try { SubFolder FolderDetails = GetFolderdetails(subdirectory); int FolderID = Dbcall.UploadSubFolder(FolderDetails, RootFolderId); //upload into the database LoadSubDirs(subdirectory, FolderID, RootFolderId); //the sub folder is contains sub folder so call sub directory } catch (Exception Ex) { ErrrorLog.ErrorlogWrite(Ex); } } }
//***********************************add folder in doccument library if rootfolder path have one folder ******************************// public void UploadFoldersRecursively(ClientContext clientContext, string sourceFolder, string destinationLigraryTitle) { try { Web web = clientContext.Web; var query = clientContext.LoadQuery(web.Lists.Where(p => p.Title == destinationLigraryTitle)); clientContext.ExecuteQuery(); List documentsLibrary = query.FirstOrDefault(); var folder = documentsLibrary.RootFolder; System.IO.DirectoryInfo DirectoriInfo = new System.IO.DirectoryInfo(sourceFolder); clientContext.Load(documentsLibrary.RootFolder); clientContext.ExecuteQuery(); folder = documentsLibrary.RootFolder.Folders.Add(DirectoriInfo.Name); clientContext.ExecuteQuery(); //_______________Add Permissions for the folder______________________// SpPermissions.GetPermmssion(clientContext, folder, sourceFolder, destinationLigraryTitle); UploadFolder(clientContext, DirectoriInfo, folder, destinationLigraryTitle); } catch (Exception ex) { ErrrorLog.ErrorlogWrite(ex); } }