/// <summary> /// To create file inside SharePoint folder /// </summary> /// <param name="folderPath">Folder path</param> /// <param name="filename">Filename</param> /// <param name="extension">Extension</param> /// <param name="memoryStream">Stream of the memory</param> /// <param name="clientContext">ClientContext object</param> /// <returns>filename</returns> private static string CreateFileInsideFolder(string folderPath, string filename, string extension, MemoryStream memoryStream, ClientContext clientContext) { FileCreationInformation newFile = new FileCreationInformation(); filename = UploadHelperFunctionsUtility.RemoveSpecialChar(filename); using (var stream = memoryStream) { stream.Seek(0, SeekOrigin.Begin); newFile.ContentStream = stream; if (!string.Equals(extension, ConstantStrings.EmailFileExtension, StringComparison.Ordinal)) { filename += extension; } newFile.Url = filename; newFile.Overwrite = true; Folder destinationFolder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); clientContext.Load(destinationFolder); clientContext.ExecuteQuery(); Microsoft.SharePoint.Client.File fileToUpload = destinationFolder.Files.Add(newFile); clientContext.Load(fileToUpload); clientContext.ExecuteQuery(); } return(filename); }
/// <summary> /// Acts as entry point from service to place the request to upload email/attachment. Reads the web request headers and requests applicable methods based on headers. /// </summary> /// <param name="requestObject">The request object.</param> /// <param name="client">The client object</param> /// <param name="serviceRequest">The Service request object</param> /// <param name="soapRequest">The SOAP request</param> /// <param name="attachmentOrMailID">The attachment or mail identifier.</param> /// <param name="isMailUpload">Mail upload check</param> /// <param name="fileName">Name of the file.</param> /// <param name="folderPath">The folder path.</param> /// <param name="isFirstCall">Determines if it is first call or not</param> /// <param name="message">Reference object for the message to be returned</param> /// <param name="originalFileName">Original file name of the attachment</param> /// <returns>It returns a string object, that contains the execution status of the function.</returns> internal static string Upload(RequestObject requestObject, Client client, ServiceRequest serviceRequest, string soapRequest, string attachmentOrMailID, bool isMailUpload, string fileName, string folderPath, bool isFirstCall, ref string message, string originalFileName) { string result = ConstantStrings.UploadFailed; try { if (null != requestObject && null != client && null != serviceRequest && !string.IsNullOrWhiteSpace(soapRequest) && !string.IsNullOrWhiteSpace(attachmentOrMailID) && !string.IsNullOrWhiteSpace(fileName) && !string.IsNullOrWhiteSpace(folderPath)) { string documentLibraryName = serviceRequest.DocumentLibraryName; //// Make the request to the Exchange server and get the response. HttpWebResponse webResponse = UploadHelperFunctionsUtility.GetWebResponse(serviceRequest.EwsUrl, serviceRequest.AttachmentToken, soapRequest, attachmentOrMailID); if (!isFirstCall) { XmlDocument xmlDocument = RetrieveXMLDocument(webResponse); string attachmentID = string.Empty; //// Check original file name is empty if (!string.IsNullOrWhiteSpace(originalFileName)) { attachmentID = UploadHelperFunctionsUtility.GetAttachmentID(xmlDocument, originalFileName); } else { attachmentID = UploadHelperFunctionsUtility.GetAttachmentID(xmlDocument, fileName); } if (!string.IsNullOrWhiteSpace(attachmentID)) { attachmentOrMailID = attachmentID; } //// Make the request to the Exchange server and get the response. webResponse = UploadHelperFunctionsUtility.GetWebResponse(serviceRequest.EwsUrl, serviceRequest.AttachmentToken, ServiceConstantStrings.AttachmentSoapRequest, attachmentOrMailID); } //// If the response is okay, create an XML document from the response and process the request. if (webResponse.StatusCode == HttpStatusCode.OK) { result = UploadFilesMail(serviceRequest.Overwrite, serviceRequest.PerformContentCheck, serviceRequest.AllowContentCheck, documentLibraryName, webResponse, isMailUpload, requestObject, client, fileName, folderPath, ref message); } if (string.IsNullOrWhiteSpace(message) && result.Equals(ConstantStrings.UploadFailed) && isFirstCall) { result = Upload(requestObject, client, serviceRequest, ServiceConstantStrings.MailSoapRequest, serviceRequest.MailId, isMailUpload, fileName, folderPath, false, ref message, originalFileName); } } else { result = ConstantStrings.UploadFailed; } } catch (Exception exception) { Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName); result = ConstantStrings.UploadFailed; } return(result); }
/// <summary> /// Reads the XMLDocument and determines whether the request is to upload entire mail/attachment/.eml file/.msg file and calls respective method. /// </summary> /// <param name="isOverwrite">Overwrite check</param> /// <param name="documentLibraryName">Name of the document library</param> /// <param name="folderName">Name of the folder</param> /// <param name="webResponse">HTTP web response to get the response stream</param> /// <param name="isMailUpload">Mail Upload Flag</param> /// <param name="requestObject">request object for web</param> /// <param name="client">Service Client Object</param> /// <param name="fileName">Name of the file</param> /// <param name="folderPath">upload folder path</param> /// <param name="message">Reference object for the message to be returned</param> /// <returns>Returns whether File Uploaded successfully or failed</returns> internal static string UploadFilesMail(bool isOverwrite, bool isContentCheckRequired, bool allowContentCheck, string documentLibraryName, HttpWebResponse webResponse, bool isMailUpload, RequestObject requestObject, Client client, string fileName, string folderPath, ref string message) { bool isMsg = true; MailMetaData mailMetaData = new MailMetaData(); var bytes = (dynamic)null; string mailMessage = string.Empty; string originalName = string.Empty; string xmlPath = string.Empty; string result = ConstantStrings.UploadSucceeded; ContentCheckDetails contentCheck = null; try { XmlDocument xmlDocument = RetrieveXMLDocument(webResponse); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable); nsmgr.AddNamespace("s", ConstantStrings.SoapEnvelopURI); nsmgr.AddNamespace("m", ConstantStrings.ExchangeServiceMessage); nsmgr.AddNamespace("t", ConstantStrings.ExchangeServiceTypes); string extension = System.IO.Path.GetExtension(fileName).Trim(); string uploadFileName = UploadHelperFunctionsUtility.RemoveSpecialChar(fileName); if (xmlDocument.SelectSingleNode("/s:Envelope/s:Body/m:GetAttachmentResponse/m:ResponseMessages/m:GetAttachmentResponseMessage/m:Attachments/t:FileAttachment/t:Content", nsmgr) != null) { isMsg = false; } if (string.IsNullOrEmpty(extension) && isMsg) { uploadFileName = uploadFileName + ConstantStrings.EmailFileExtension; } using (ClientContext clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(client.Url), requestObject.RefreshToken)) { if (isMailUpload) { bytes = GetEmailProperties(xmlDocument, ref mailMetaData); } else { bytes = UploadHelperFunctionsUtility.GetStream(xmlDocument, nsmgr, isMailUpload, extension, isMsg); } if (null != bytes) { using (MemoryStream memoryStream = new MemoryStream(bytes)) { contentCheck = new ContentCheckDetails(uploadFileName, mailMetaData.mailSubject, memoryStream.Length, mailMetaData.mailSender, mailMetaData.sentDate); } } if (!isOverwrite && !isContentCheckRequired && UploadHelperFunctionsUtility.CheckDuplicateDocument(clientContext, documentLibraryName, isMailUpload, folderPath, contentCheck, uploadFileName, allowContentCheck, ref message)) { result = ConstantStrings.UploadFailed; } else if (isContentCheckRequired) { message = UploadHelperFunctionsUtility.PerformContentCheckUtility(isMailUpload, folderPath, isMsg, xmlDocument, nsmgr, extension, uploadFileName, clientContext); result = ConstantStrings.UploadFailed; } else { if (isMailUpload) //Upload entire Email { UploadMail(requestObject, client, folderPath, fileName, documentLibraryName, xmlDocument, ref message); } else { if (string.IsNullOrEmpty(extension) && isMsg) //Upload .msg file { UploadAttachedMailExtBlank(requestObject, client, folderPath, fileName, documentLibraryName, xmlDocument, ref message); } else { if (string.Equals(extension, ConstantStrings.EmailFileExtension, StringComparison.OrdinalIgnoreCase)) { UploadEMLFile(documentLibraryName, requestObject, client, folderPath, fileName, ref message, xmlDocument, nsmgr, ref mailMetaData, ref bytes, extension); } else { //get original name xmlPath = "/s:Envelope/s:Body/m:GetAttachmentResponse/m:ResponseMessages/m:GetAttachmentResponseMessage/m:Attachments/t:FileAttachment/t:Name"; originalName = RetrieveNodeValue(xmlDocument, nsmgr, xmlPath, true); //get attachment content xmlPath = "/s:Envelope/s:Body/m:GetAttachmentResponse/m:ResponseMessages/m:GetAttachmentResponseMessage/m:Attachments/t:FileAttachment/t:Content"; mailMessage = RetrieveNodeValue(xmlDocument, nsmgr, xmlPath, false); bytes = Convert.FromBase64String(mailMessage); using (MemoryStream memoryStream = new MemoryStream(bytes)) { mailMetaData.mailImportance = string.Empty; mailMetaData.mailSubject = string.Empty; mailMetaData.originalName = originalName; if (!UploadToFolder(requestObject, client, folderPath, fileName, string.Empty, memoryStream, documentLibraryName, mailMetaData, ref message)) { result = ConstantStrings.UploadFailed; } } } } } if (!string.IsNullOrEmpty(message)) { result = ConstantStrings.UploadFailed; } } } } catch (Exception exception) { Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName); result = ConstantStrings.UploadFailed; } return(result); }