// Cancel an import public void Cancel() { // Clear the properties importdatatable = new DataTable(); importprogress = new ImportProgress(); importsettings = new ImportSettings(); }
// Load an XML Data set into the in memory data table private void LoadItemsXMLDataSet(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loading:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Loading; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); // Deserialize the XML into the current data table XmlSerializer xmlserializer = new XmlSerializer(typeof(DataTable)); FileStream filestream = new FileStream(importsettings.SourceFile, FileMode.Open); importdatatable = (DataTable)xmlserializer.Deserialize(filestream); filestream.Close(); // Tell the user what has happened importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loaded:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsCount = importdatatable.Rows.Count; importprogress.ImportStatusChange = false; currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); }
// Report progress private void ReportProgress(BackgroundWorker worker, ImportProgress currentprogress) { // Always report progress worker.ReportProgress(0, currentprogress); // Log progress if required if (importsettings.LoggingToFile) { Trace.WriteLine(currentprogress.ImportActivity); } }
public ImportProgress(ImportProgress suppliedprogress) { ImportItemsCount = suppliedprogress.ImportItemsCount; ImportProgressPercentage = suppliedprogress.ImportProgressPercentage; ImportItemsProcessed = suppliedprogress.ImportItemsProcessed; ImportItemsFailed = suppliedprogress.ImportItemsFailed; ImportItemsSucceeded = suppliedprogress.ImportItemsSucceeded; ImportLastItemProcessed = suppliedprogress.ImportLastItemProcessed; ImportActivity = suppliedprogress.ImportActivity; ImportActivityLevel = suppliedprogress.ImportActivityLevel; ImportStatus = suppliedprogress.ImportStatus; ImportStatusChange = suppliedprogress.ImportStatusChange; }
// ProgressChanged Event private void bgwImport_ProgressChanged(object sender, ProgressChangedEventArgs e) { // We will have been passed an object by the progress update. // this will be an ImportProgress object ImportProgress importprogress = e.UserState as ImportProgress; // If the status has changed we should update the UI if (importprogress.ImportStatusChange) { SetUIControlStatus(importprogress); } // We should update the UI with the last reported status SetUIProgressStatus(importprogress); }
// Iterate the file system folder to build the data table private void LoadItemsFileSystemFolder(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString(); importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Loading; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); // Ensure that the current data set contains the fields that we need. EnsureDataSet(); // Iterate the file system LoadFileSystemDirectoryRecursive(worker, importsettings.SourceFolder); }
// Iterate the file system folder to build the data table private void LoadItemsFileSystemFolder(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString(); importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Loading; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); // Ensure that the current data set contains the fields that we need. EnsureDataSet(); // Iterate the file system LoadFileSystemDirectoryRecursive(worker, importsettings.SourceFolder); }
protected void SetUIProgressStatus(ImportProgress currentprogress) { // We should inform the user of any activity LogToUser(currentprogress.ImportActivity); // We should update the number of items to be imported textItemsCount.Text = currentprogress.ImportItemsCount.ToString(); // We should update the number of items that have been processed textBoxItemsProcessed.Text = currentprogress.ImportItemsProcessed.ToString(); textBoxItemsFailed.Text = currentprogress.ImportItemsFailed.ToString(); textBoxItemsSucceeded.Text = currentprogress.ImportItemsSucceeded.ToString(); // We should update the progress bar progressBarImport.Value = currentprogress.ImportProgressPercentage; progressBarImport.Update(); }
// Import the items into SharePoint public void ImportItems(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString(); importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Importing; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); for (int i = importprogress.ImportLastItemProcessed; i < importdatatable.Rows.Count; i++) { ImportRow(importdatatable.Rows[i], worker); if (worker.CancellationPending) { importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + "Import Paused"; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Paused; importprogress.ImportStatusChange = true; ImportProgress currentrowprogress = new ImportProgress(importprogress); ReportProgress(worker, currentrowprogress); break; } } // Once the for loop has been completed the import of the items is complete // we can therefore update the import progress if (!worker.CancellationPending) { importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + "Completed"; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Completed; importprogress.ImportStatusChange = true; currentprogress = importprogress; ReportProgress(worker, currentprogress); } }
// Iterate through subdirectories public void LoadFileSystemDirectoryRecursive(BackgroundWorker worker, string strDirectory) { foreach (string strFileName in Directory.GetFiles(strDirectory)) { // We have a new data row for every file to import DataRow importdatarow = importdatatable.NewRow(); // Get the details of the file FileInfo fileinfo = new FileInfo(strFileName); importdatarow[importsettings.fieldSourceFileNameAndPath] = fileinfo.FullName; // We must record the destination details importdatarow[importsettings.fieldDestinationFolderUrl] = importsettings.Destination.DestinationFolderUrl; importdatarow[importsettings.fieldDestinationServerUrl] = importsettings.Destination.DestinationServerUrl; importdatarow[importsettings.fieldDestinationWebUrl] = importsettings.Destination.DestinationWebUrl; importdatarow[importsettings.fieldDestinationSubDirectories] = Get_FolderURL_FromDirectory(strDirectory); importdatarow[importsettings.fieldDestinationFileName] = fileinfo.Name; // Add the entry to the data set - the data set will be iterated later during the import importdatatable.Rows.Add(importdatarow); } // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":" + strDirectory; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsCount = importdatatable.Rows.Count; importprogress.ImportStatusChange = false; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); foreach (string strSubDirectory in Directory.GetDirectories(strDirectory)) { LoadFileSystemDirectoryRecursive(worker, strSubDirectory); } }
protected void SetUIControlStatus(ImportProgress currentprogress) { if (currentprogress.ImportStatus == ImportProgress.Status.New) { groupBoxDestination.Enabled = true; groupBoxImportSettings.Enabled = true; groupBoxSelectDestination.Enabled = true; groupBoxLogging.Enabled = true; groupBoxSourceType.Enabled = true; groupBoxSourceFolder.Enabled = true; groupBoxSourceFile.Enabled = true; groupBoxMetaData.Enabled = true; groupBoxAuthentication.Enabled = true; groupBoxExceptions.Enabled = true; buttonStartImport.Enabled = true; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = false; } if (currentprogress.ImportStatus == ImportProgress.Status.Loading) { groupBoxDestination.Enabled = false; groupBoxImportSettings.Enabled = false; groupBoxSelectDestination.Enabled = false; groupBoxLogging.Enabled = false; groupBoxSourceType.Enabled = false; groupBoxSourceFolder.Enabled = false; groupBoxSourceFile.Enabled = false; groupBoxMetaData.Enabled = false; groupBoxAuthentication.Enabled = false; groupBoxExceptions.Enabled = false; buttonStartImport.Enabled = false; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = false; } if (currentprogress.ImportStatus == ImportProgress.Status.Importing) { groupBoxDestination.Enabled = false; groupBoxImportSettings.Enabled = false; groupBoxSelectDestination.Enabled = false; groupBoxLogging.Enabled = false; groupBoxSourceType.Enabled = false; groupBoxSourceFolder.Enabled = false; groupBoxSourceFile.Enabled = false; groupBoxMetaData.Enabled = false; groupBoxAuthentication.Enabled = false; groupBoxExceptions.Enabled = false; buttonStartImport.Enabled = false; buttonPauseImport.Enabled = true; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = false; } if (currentprogress.ImportStatus == ImportProgress.Status.Paused) { groupBoxDestination.Enabled = false; groupBoxImportSettings.Enabled = false; groupBoxSelectDestination.Enabled = false; groupBoxLogging.Enabled = false; groupBoxSourceType.Enabled = false; groupBoxSourceFolder.Enabled = false; groupBoxSourceFile.Enabled = false; groupBoxMetaData.Enabled = false; groupBoxAuthentication.Enabled = false; groupBoxExceptions.Enabled = false; buttonStartImport.Enabled = false; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = true; buttonCancelImport.Enabled = true; } if (currentprogress.ImportStatus == ImportProgress.Status.Completed) { groupBoxDestination.Enabled = true; groupBoxImportSettings.Enabled = true; groupBoxSelectDestination.Enabled = true; groupBoxLogging.Enabled = true; groupBoxSourceType.Enabled = true; groupBoxSourceFolder.Enabled = true; groupBoxSourceFile.Enabled = true; groupBoxMetaData.Enabled = true; groupBoxAuthentication.Enabled = true; groupBoxExceptions.Enabled = true; buttonStartImport.Enabled = true; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = true; } }
// Load an XML Data set into the in memory data table private void LoadItemsXMLDataSet(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loading:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Loading; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); // Deserialize the XML into the current data table XmlSerializer xmlserializer = new XmlSerializer(typeof(DataTable)); FileStream filestream = new FileStream(importsettings.SourceFile, FileMode.Open); importdatatable = (DataTable)xmlserializer.Deserialize(filestream); filestream.Close(); // Tell the user what has happened importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loaded:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsCount = importdatatable.Rows.Count; importprogress.ImportStatusChange = false; currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); }
// Load a CSV file into the in memory data table private void LoadItemsCSVFile(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loading:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Loading; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); // Ensure that the current data set contains the fields that we need. EnsureDataSet(); // Load the CSV file into the current data set string strLine; string[] strArray; string[] csvfields; char[] charArray = new char[] { ',' }; FileStream filestream = new FileStream(importsettings.SourceFile, FileMode.Open); StreamReader streamreader = new StreamReader(filestream); // Read the first line from the file strLine = streamreader.ReadLine(); // Split the header csvfields = strLine.Split(charArray); // Add any columns to the data table as required foreach (string strcolumnname in csvfields) { // If there is not already a data table entry for the column name then add it if (!importdatatable.Columns.Contains(strcolumnname)) { importdatatable.Columns.Add(strcolumnname); } } // Read the first data line strLine = streamreader.ReadLine(); // Loop through the CSV file until the end while (strLine != null) { strArray = strLine.Split(charArray); DataRow importdatarow = importdatatable.NewRow(); // Load any meta data for (int i = 0; i <= strArray.GetUpperBound(0); i++) { // Use the field name from the header to tell what column should be populated. importdatarow[csvfields[i]] = strArray[i].Trim(); } // Get the details of the file FileInfo fileinfo = new FileInfo(importdatarow[importsettings.fieldSourceFileNameAndPath].ToString()); // We must record the destination details importdatarow[importsettings.fieldDestinationFolderUrl] = importsettings.Destination.DestinationFolderUrl; importdatarow[importsettings.fieldDestinationServerUrl] = importsettings.Destination.DestinationServerUrl; importdatarow[importsettings.fieldDestinationWebUrl] = importsettings.Destination.DestinationWebUrl; //importdatarow[importsettings.fieldDestinationSubDirectories] = Get_FolderURL_FromDirectory(strDirectory); importdatarow[importsettings.fieldDestinationFileName] = fileinfo.Name; importdatatable.Rows.Add(importdatarow); strLine = streamreader.ReadLine(); } streamreader.Close(); filestream.Close(); // Tell the user what has happened importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loaded:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsCount = importdatatable.Rows.Count; importprogress.ImportStatusChange = false; currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); }
// Import a row into the destination - Add the file and then set the meta data private void ImportRow(DataRow datarow, BackgroundWorker worker) { try { // Get the details for the current file string strContextURL = datarow[importsettings.fieldDestinationServerUrl].ToString() + datarow[importsettings.fieldDestinationWebUrl].ToString(); string strDestination = datarow[importsettings.fieldDestinationFolderUrl].ToString() + datarow[importsettings.fieldDestinationSubDirectories].ToString() + "/" + datarow[importsettings.fieldDestinationFileName].ToString(); string strRootFolder = datarow[importsettings.fieldDestinationFolderUrl].ToString(); string strSource = datarow[importsettings.fieldSourceFileNameAndPath].ToString(); string strSubDirectories = datarow[importsettings.fieldDestinationSubDirectories].ToString(); // Get the file info object and store the meta data FileInfo fileinfo = new FileInfo(strSource); // Create a context var context = ImportHelper.GetContext(strContextURL, importsettings.authenticationsettings); // A folder may be needed. ProcessSubDirectories(context, strRootFolder, strSubDirectories); // Process the file ProcessFile(fileinfo, datarow); // First we need to add the file to SharePoint AddFile(context, fileinfo, strDestination); // Now we need to map any required meta data AddMetaData(context, datarow, strDestination); // Notch up another complete item importprogress.ImportItemsSucceeded = importprogress.ImportItemsSucceeded + 1; // Clear exception (in case there was a previous exception) datarow[importsettings.fieldException] = null; // Let the user know what we did importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Imported:" + strSource + ":To:" + strDestination; importprogress.ImportActivityLevel = 2; importprogress.ImportItemsProcessed = importprogress.ImportItemsProcessed + 1; importprogress.ImportStatusChange = false; float floatRatio = (float)importprogress.ImportItemsProcessed / (float)importprogress.ImportItemsCount; importprogress.ImportProgressPercentage = (int)(floatRatio * 100); ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); } catch (Exception ex) { importprogress.ImportItemsFailed = importprogress.ImportItemsFailed + 1; importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":" + datarow[importsettings.fieldSourceFileNameAndPath].ToString() + ":Exception" + ex.Message; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsProcessed = importprogress.ImportItemsProcessed + 1; importprogress.ImportStatusChange = false; float floatRatio = (float)importprogress.ImportItemsProcessed / (float)importprogress.ImportItemsCount; importprogress.ImportProgressPercentage = (int)(floatRatio * 100); ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); datarow[importsettings.fieldException] = ex.Message; } }
// Iterate through subdirectories public void LoadFileSystemDirectoryRecursive(BackgroundWorker worker, string strDirectory) { foreach (string strFileName in Directory.GetFiles(strDirectory)) { // We have a new data row for every file to import DataRow importdatarow = importdatatable.NewRow(); // Get the details of the file FileInfo fileinfo = new FileInfo(strFileName); importdatarow[importsettings.fieldSourceFileNameAndPath] = fileinfo.FullName; // We must record the destination details importdatarow[importsettings.fieldDestinationFolderUrl] = importsettings.Destination.DestinationFolderUrl; importdatarow[importsettings.fieldDestinationServerUrl] = importsettings.Destination.DestinationServerUrl; importdatarow[importsettings.fieldDestinationWebUrl] = importsettings.Destination.DestinationWebUrl; importdatarow[importsettings.fieldDestinationSubDirectories] = Get_FolderURL_FromDirectory(strDirectory); importdatarow[importsettings.fieldDestinationFileName] = fileinfo.Name; // Add the entry to the data set - the data set will be iterated later during the import importdatatable.Rows.Add(importdatarow); } // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":" + strDirectory; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsCount = importdatatable.Rows.Count; importprogress.ImportStatusChange = false; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); foreach (string strSubDirectory in Directory.GetDirectories(strDirectory)) { LoadFileSystemDirectoryRecursive(worker, strSubDirectory); } }
// Cancel an import public void Cancel() { // Clear the properties importdatatable = new DataTable(); importprogress = new ImportProgress(); importsettings = new ImportSettings(); }
// Load a CSV file into the in memory data table private void LoadItemsCSVFile(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loading:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Loading; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); // Ensure that the current data set contains the fields that we need. EnsureDataSet(); // Load the CSV file into the current data set string strLine; string[] strArray; string[] csvfields; char[] charArray = new char[] { ',' }; FileStream filestream = new FileStream(importsettings.SourceFile, FileMode.Open); StreamReader streamreader = new StreamReader(filestream); // Read the first line from the file strLine = streamreader.ReadLine(); // Split the header csvfields = strLine.Split(charArray); // Add any columns to the data table as required foreach (string strcolumnname in csvfields) { // If there is not already a data table entry for the column name then add it if (!importdatatable.Columns.Contains(strcolumnname)) { importdatatable.Columns.Add(strcolumnname); } } // Read the first data line strLine = streamreader.ReadLine(); // Loop through the CSV file until the end while (strLine != null) { strArray = strLine.Split(charArray); DataRow importdatarow = importdatatable.NewRow(); // Load any meta data for (int i = 0; i <= strArray.GetUpperBound(0); i++) { // Use the field name from the header to tell what column should be populated. importdatarow[csvfields[i]] = strArray[i].Trim(); } // Get the details of the file FileInfo fileinfo = new FileInfo(importdatarow[importsettings.fieldSourceFileNameAndPath].ToString()); // We must record the destination details importdatarow[importsettings.fieldDestinationFolderUrl] = importsettings.Destination.DestinationFolderUrl; importdatarow[importsettings.fieldDestinationServerUrl] = importsettings.Destination.DestinationServerUrl; importdatarow[importsettings.fieldDestinationWebUrl] = importsettings.Destination.DestinationWebUrl; //importdatarow[importsettings.fieldDestinationSubDirectories] = Get_FolderURL_FromDirectory(strDirectory); importdatarow[importsettings.fieldDestinationFileName] = fileinfo.Name; importdatatable.Rows.Add(importdatarow); strLine = streamreader.ReadLine(); } streamreader.Close(); filestream.Close(); // Tell the user what has happened importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Loaded:" + importsettings.SourceFile; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsCount = importdatatable.Rows.Count; importprogress.ImportStatusChange = false; currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); }
protected void SetUIProgressStatus(ImportProgress currentprogress) { // We should inform the user of any activity LogToUser(currentprogress.ImportActivity); // We should update the number of items to be imported textItemsCount.Text = currentprogress.ImportItemsCount.ToString(); // We should update the number of items that have been processed textBoxItemsProcessed.Text = currentprogress.ImportItemsProcessed.ToString(); textBoxItemsFailed.Text = currentprogress.ImportItemsFailed.ToString(); textBoxItemsSucceeded.Text = currentprogress.ImportItemsSucceeded.ToString(); // We should update the progress bar progressBarImport.Value = currentprogress.ImportProgressPercentage; progressBarImport.Update(); }
public ImportProgress(ImportProgress suppliedprogress) { ImportItemsCount = suppliedprogress.ImportItemsCount; ImportProgressPercentage = suppliedprogress.ImportProgressPercentage; ImportItemsProcessed = suppliedprogress.ImportItemsProcessed; ImportItemsFailed = suppliedprogress.ImportItemsFailed; ImportItemsSucceeded = suppliedprogress.ImportItemsSucceeded; ImportLastItemProcessed = suppliedprogress.ImportLastItemProcessed; ImportActivity = suppliedprogress.ImportActivity; ImportActivityLevel = suppliedprogress.ImportActivityLevel; ImportStatus = suppliedprogress.ImportStatus; ImportStatusChange = suppliedprogress.ImportStatusChange; }
// Report progress private void ReportProgress(BackgroundWorker worker, ImportProgress currentprogress) { // Always report progress worker.ReportProgress(0, currentprogress); // Log progress if required if (importsettings.LoggingToFile) { Trace.WriteLine(currentprogress.ImportActivity); } }
// Import a row into the destination - Add the file and then set the meta data private void ImportRow(DataRow datarow, BackgroundWorker worker) { try { // Get the details for the current file string strContextURL = datarow[importsettings.fieldDestinationServerUrl].ToString() + datarow[importsettings.fieldDestinationWebUrl].ToString(); string strDestination = datarow[importsettings.fieldDestinationFolderUrl].ToString() + datarow[importsettings.fieldDestinationSubDirectories].ToString() + "/"+datarow[importsettings.fieldDestinationFileName].ToString() ; string strRootFolder = datarow[importsettings.fieldDestinationFolderUrl].ToString(); string strSource = datarow[importsettings.fieldSourceFileNameAndPath].ToString(); string strSubDirectories = datarow[importsettings.fieldDestinationSubDirectories].ToString(); // Get the file info object and store the meta data FileInfo fileinfo = new FileInfo(strSource); // Create a context var context = ImportHelper.GetContext(strContextURL, importsettings.authenticationsettings); // A folder may be needed. ProcessSubDirectories(context, strRootFolder, strSubDirectories); // Process the file ProcessFile(fileinfo, datarow); // First we need to add the file to SharePoint AddFile(context, fileinfo, strDestination); // Now we need to map any required meta data AddMetaData(context, datarow, strDestination); // Notch up another complete item importprogress.ImportItemsSucceeded = importprogress.ImportItemsSucceeded + 1; // Clear exception (in case there was a previous exception) datarow[importsettings.fieldException] = null; // Let the user know what we did importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":Imported:" + strSource + ":To:" + strDestination; importprogress.ImportActivityLevel = 2; importprogress.ImportItemsProcessed = importprogress.ImportItemsProcessed + 1; importprogress.ImportStatusChange = false; float floatRatio = (float)importprogress.ImportItemsProcessed / (float)importprogress.ImportItemsCount; importprogress.ImportProgressPercentage = (int)(floatRatio * 100); ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); } catch (Exception ex) { importprogress.ImportItemsFailed = importprogress.ImportItemsFailed + 1; importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + ":" + datarow[importsettings.fieldSourceFileNameAndPath].ToString() + ":Exception"+ex.Message; importprogress.ImportActivityLevel = 1; importprogress.ImportItemsProcessed = importprogress.ImportItemsProcessed + 1; importprogress.ImportStatusChange = false; float floatRatio = (float)importprogress.ImportItemsProcessed / (float)importprogress.ImportItemsCount; importprogress.ImportProgressPercentage = (int)(floatRatio * 100); ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); datarow[importsettings.fieldException] = ex.Message; } }
protected void SetUIControlStatus(ImportProgress currentprogress) { if (currentprogress.ImportStatus == ImportProgress.Status.New) { groupBoxDestination.Enabled = true; groupBoxImportSettings.Enabled = true; groupBoxSelectDestination.Enabled = true; groupBoxLogging.Enabled = true; groupBoxSourceType.Enabled = true; groupBoxSourceFolder.Enabled = true; groupBoxSourceFile.Enabled = true; groupBoxMetaData.Enabled = true; groupBoxAuthentication.Enabled = true; groupBoxExceptions.Enabled = true; buttonStartImport.Enabled = true; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = false; } if (currentprogress.ImportStatus == ImportProgress.Status.Loading) { groupBoxDestination.Enabled = false; groupBoxImportSettings.Enabled = false; groupBoxSelectDestination.Enabled = false; groupBoxLogging.Enabled = false; groupBoxSourceType.Enabled = false; groupBoxSourceFolder.Enabled = false; groupBoxSourceFile.Enabled = false; groupBoxMetaData.Enabled = false; groupBoxAuthentication.Enabled = false; groupBoxExceptions.Enabled = false; buttonStartImport.Enabled = false; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = false; } if (currentprogress.ImportStatus == ImportProgress.Status.Importing) { groupBoxDestination.Enabled = false; groupBoxImportSettings.Enabled = false; groupBoxSelectDestination.Enabled = false; groupBoxLogging.Enabled = false; groupBoxSourceType.Enabled = false; groupBoxSourceFolder.Enabled = false; groupBoxSourceFile.Enabled = false; groupBoxMetaData.Enabled = false; groupBoxAuthentication.Enabled = false; groupBoxExceptions.Enabled = false; buttonStartImport.Enabled = false; buttonPauseImport.Enabled = true; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = false; } if (currentprogress.ImportStatus == ImportProgress.Status.Paused) { groupBoxDestination.Enabled = false; groupBoxImportSettings.Enabled = false; groupBoxSelectDestination.Enabled = false; groupBoxLogging.Enabled = false; groupBoxSourceType.Enabled = false; groupBoxSourceFolder.Enabled = false; groupBoxSourceFile.Enabled = false; groupBoxMetaData.Enabled = false; groupBoxAuthentication.Enabled = false; groupBoxExceptions.Enabled = false; buttonStartImport.Enabled = false; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = true; buttonCancelImport.Enabled = true; } if (currentprogress.ImportStatus == ImportProgress.Status.Completed) { groupBoxDestination.Enabled = true; groupBoxImportSettings.Enabled = true; groupBoxSelectDestination.Enabled = true; groupBoxLogging.Enabled = true; groupBoxSourceType.Enabled = true; groupBoxSourceFolder.Enabled = true; groupBoxSourceFile.Enabled = true; groupBoxMetaData.Enabled = true; groupBoxAuthentication.Enabled = true; groupBoxExceptions.Enabled = true; buttonStartImport.Enabled = true; buttonPauseImport.Enabled = false; buttonResumeImport.Enabled = false; buttonCancelImport.Enabled = true; } }
// Import the items into SharePoint public void ImportItems(BackgroundWorker worker) { // Tell the user what is happening importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString(); importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Importing; importprogress.ImportStatusChange = true; ImportProgress currentprogress = new ImportProgress(importprogress); ReportProgress(worker, currentprogress); for (int i = importprogress.ImportLastItemProcessed; i < importdatatable.Rows.Count; i++) { ImportRow(importdatatable.Rows[i], worker); if (worker.CancellationPending) { importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + "Import Paused"; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Paused; importprogress.ImportStatusChange = true; ImportProgress currentrowprogress = new ImportProgress(importprogress); ReportProgress(worker, currentrowprogress); break; } } // Once the for loop has been completed the import of the items is complete // we can therefore update the import progress if (!worker.CancellationPending) { importprogress.ImportActivity = MethodBase.GetCurrentMethod().Name.ToString() + "Completed"; importprogress.ImportActivityLevel = 0; importprogress.ImportStatus = ImportProgress.Status.Completed; importprogress.ImportStatusChange = true; currentprogress = importprogress; ReportProgress(worker, currentprogress); } }