private string BuildDestinationFilename(string filename, SPFolder destFolder) { if (destFolder.FileExists(filename)) { filename = string.Format("{0}_(ItemId_{1}){2}", Path.GetFileNameWithoutExtension(filename), _sourceListItem.ID, Path.GetExtension(filename)); } return filename; }
private void CopyAttachmentsAndMetaDataToDestinationLibrary(SPFolder destinationFolder, SPContentType destinationContentType) { InfoPathHelper ipHelper; ipHelper = new InfoPathHelper(this.__ActivationProperties); try { ipHelper.LoadForm(); } catch { this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowError, Constants.Workflow.ERROR_LOAD_INFOPATH_FORM_DATA, string.Empty); } try { //attachment path not exist or is null, log and exit function if (ipHelper.NodeIsNullOrNotExistAt(this.AttachmentFieldPath)) { this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowComment, string.Format(Constants.Workflow.ERROR_INFOPATH_FORM_VALUE_IS_NULL_OR_NOT_EXIST, this.AttachmentFieldPath), string.Empty); return; } //log message when description path is null or not exist if (ipHelper.NodeIsNullOrNotExistAt(this.DescriptionFieldPath)) { this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowComment, string.Format(Constants.Workflow.ERROR_LOAD_DESCRIPTION_INFOPATH_FORM_VALUE, this.DescriptionFieldPath), string.Empty); } //log message when destination description field name not exist var descriptionField = destinationFolder.DocumentLibrary.Fields.Cast<SPField>().ToList().FirstOrDefault(p => string.Compare(p.Title, this.DescriptionFieldName.Trim(), true) == 0); if (descriptionField == null) { this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowComment, string.Format(Constants.Workflow.ERROR_FIELD_NAME_NOT_EXIST_ON_LIBRARY, this.DescriptionFieldName), string.Empty); } List<InfoPathAttachment> attachments = ipHelper.GetFilesFromPath(this.AttachmentFieldPath, this.DescriptionFieldPath); if (attachments.Count == 0) { this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowComment, string.Format(Constants.Workflow.ERROR_NO_ATTACHMENT, this.AttachmentFieldPath), string.Empty); return; } foreach (InfoPathAttachment attachment in attachments) { string destinationFileURL = destinationFolder.Url + "/" + attachment.Filename.ConvertToValidSharePointFileName(); //file exist if (!this.overrideDetinationFile && destinationFolder.FileExists(attachment.Filename)) { string errorMessage = string.Format(Constants.Workflow.ERROR_FILE_EXIST, attachment.Filename, this.DestinationFolderUrl); this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowComment, errorMessage, string.Empty); continue; } if (UsingAccount == "System") { UploadWithSystemAccount(destinationFolder, destinationContentType, descriptionField, attachment, destinationFileURL); } else { UploadWithOriginatorUser(destinationFolder, destinationContentType, descriptionField, attachment, destinationFileURL); } this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowComment, string.Format(Constants.Workflow.UPLOAD_FILE_SUCESSFULLY, attachment.Filename, this.DestinationFolderUrl), string.Empty); } } catch { this.__ActivationProperties.LogToWorkflowHistory(SPWorkflowHistoryEventType.WorkflowError, Constants.Workflow.ERROR_UPLOADING_FILE, string.Empty); } }