private void OnExportCompleted(object sender, ExportCompletedEventArgs e)
        {
            GlymaExportUserState userState = e.UserState as GlymaExportUserState;
            if (userState != null)
            {
                try
                {
                    ProgressRecord progressRecord = new ProgressRecord(0, "Exporting Glyma Map", string.Format("Creating output file: {0}", Output));
                    progressRecord.SecondsRemaining = -1;
                    progressRecord.PercentComplete = 100;

                    lock (pr_lock)
                    {
                        this.ProgressRecord = progressRecord;
                        ProgressEvent.Set(); //notify of change to progress
                    }

                    //Delete the file if it already exists before the move operation
                    if (File.Exists(Output))
                    {
                        File.Delete(Output);
                    }
                    File.Move(e.FileLocation, Output);

                    progressRecord = new ProgressRecord(0, "Exporting Glyma Map", "Export completed");
                    progressRecord.SecondsRemaining = -1;
                    progressRecord.PercentComplete = 100;
                    progressRecord.RecordType = ProgressRecordType.Completed;

                    lock (pr_lock)
                    {
                        this.ProgressRecord = progressRecord;
                        ProgressEvent.Set(); //notify of change to progress
                    }
                }
                catch (Exception ex) 
                {
                    lock (msg_lock)
                    {
                        this.Message = string.Format("Failed to move the temporary file '{0}' to the output file location.\r\n\r\nException: {1}", e.FileLocation, ex);
                        LogMessageEvent.Set();
                    }
                }
                finally
                {
                    userState.Completed.Set(); //signal that the export has completed
                    Completed.Set(); //signal that the Cmdlet has completed
                }
            }
        }
        private void exportUtility_ExportCompleted(object sender, ExportCompletedEventArgs e)
        {
            GlymaExportUserState userState = e.UserState as GlymaExportUserState;
            if (userState != null)
            {
                SPWorkItem workItem = userState.StateObject as SPWorkItem;
                if (workItem != null)
                {
                    using (SPSite site = new SPSite(workItem.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(workItem.WebId))
                        {
                            try
                            {
                                if (e.Status == ExportStatus.Error)
                                {
                                    WriteExportStatus(workItem, ExportStatus.Error);
                                    LogMessage(workItem, e.ErrorMessage);
                                }
                                else if (e.Status == ExportStatus.Completed)
                                {
                                    SPList exportsList = web.Lists[workItem.ParentId];
                                    SPListItem exportItem = exportsList.GetItemByUniqueId(workItem.ItemGuid);

                                    if (exportItem != null)
                                    {
                                        // Read the contents of the file
                                        byte[] fileContents = null;
                                        Stream fs = null;
                                        try
                                        {
                                            fs = File.OpenRead(e.FileLocation);
                                            fileContents = new byte[fs.Length];
                                            fs.Read(fileContents, 0, (int)fs.Length);

                                            // Add the file to the ListItem
                                            SPAttachmentCollection attachments = exportItem.Attachments;
                                            string fileName = Path.GetFileName(e.FileLocation);
                                            string extention = fileName.Substring(fileName.LastIndexOf('.'));
                                            fileName = e.MapName + extention;
                                            attachments.Add(fileName, fileContents);
                                            exportItem.Update();

                                            // Mark as completed
                                            WriteExportStatus(workItem, ExportStatus.Completed);
                                            WriteProgress(workItem, 1);
                                            if (userState.UseVerboseLogging)
                                            {
                                                LogMessage(workItem, "Export file copied to list successfully.");
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            WriteExportStatus(workItem, ExportStatus.Error);
                                            LogMessage(workItem, "Failed reading the exported file: {0}. {1}", e.FileLocation, ex.Message); //this will append the log message
                                        }
                                        finally
                                        {
                                            if (fs != null)
                                            {
                                                fs.Close();
                                            }
                                        }

                                        //Try delete the temporary file, don't change the status to Error if this fails but log it.
                                        try
                                        {
                                            DeleteTempFile(e.FileLocation);
                                        }
                                        catch (Exception ex)
                                        {
                                            if (workItem != null)
                                            {
                                                //the status will still be Completed but an error occurred that should be monitored.
                                                LogMessage(workItem, "Failed to delete temp export file: {0}. ", ex.Message);
                                            }
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                userState.Completed.Set(); //work item has completed
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        protected virtual void OnExportCompleted(object sender, ExportCompletedEventArgs e)
        {
            try
            {
                bool fileCreated = CreateFile();

                string safeFileName = CreateSafeFileName(RootMap.Name);
                if (fileCreated)
                {
                    var eventArgs = new ExportCompletedEventArgs(e.FileLocation, UserState, safeFileName, e.Status, e.ErrorMessage);
                    if (ExportCompleted != null)
                    {
                        ExportCompleted(sender, eventArgs);
                    }
                }
                else
                {
                    var eventArgs = new ExportCompletedEventArgs(e.FileLocation, UserState, safeFileName, ExportStatus.Error, "Error occurred when creating the export file.");
                    if (ExportCompleted != null)
                    {
                        ExportCompleted(sender, eventArgs);
                    }
                }
            }
            catch (Exception ex)
            {
                OnExceptionRaised(sender, "Error occurred when creating the export file.", ex);
            }
        }