public OutlookServiceResponse PostAttachments(OutlookServiceRequest request) { OutlookServiceResponse response = new OutlookServiceResponse(); try { response = GetAttachmentsFromExchangeServer(request); } catch (Exception ex) { response.IsError = true; response.Message = ex.Message; } return(response); }
private OutlookServiceResponse GetAttachmentsFromExchangeServer(OutlookServiceRequest request) { int processedCount = 0; List <string> attachmentNames = new List <string>(); foreach (ArtifactDetails attachment in request.Attachments) { // Prepare a web request object. HttpWebRequest webRequest = WebRequest.CreateHttp(request.EwsUrl); webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", request.AttachmentToken)); webRequest.PreAuthenticate = true; webRequest.AllowAutoRedirect = false; webRequest.Method = "POST"; webRequest.ContentType = "text/xml; charset=utf-8"; // Construct the SOAP message for the GetAttchment operation. byte[] bodyBytes = Encoding.UTF8.GetBytes(string.Format(GetAttachmentSoapRequest, attachment.Id)); webRequest.ContentLength = bodyBytes.Length; Stream requestStream = webRequest.GetRequestStream(); requestStream.Write(bodyBytes, 0, bodyBytes.Length); requestStream.Close(); // Make the request to the Exchange server and get the response. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); // If the response is okay, create an XML document from the // response and process the request. if (webResponse.StatusCode == HttpStatusCode.OK) { Stream responseStream = webResponse.GetResponseStream(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(responseStream); //Trace.Write(xmlDocument.InnerXml); string content = GetContent(xmlDocument); var fileData = System.Convert.FromBase64String(content); MemoryStream ms = new MemoryStream(fileData); //GetAttachmentDetails(attachment); //Write file to SharePoint library try { string siteUrl = "https://nylonline.sharepoint.com/sites/ibm"; using (ClientContext spContext = new ClientContext(siteUrl)) { Web spWeb = spContext.Web; spContext.Credentials = new SharePointOnlineCredentials("*****@*****.**", GetSecureString("kmp@2017")); spContext.Load(spWeb); spContext.ExecuteQuery(); string title = spWeb.Title; var targetFileUrl = String.Format("{0}/{1}", "/IT Business Management Documents", attachment.Name); ms.Position = 0; var list = spContext.Web.Lists.GetByTitle("IT Business Management Documents"); spContext.Load(list.RootFolder); spContext.ExecuteQuery(); var fileUrl = Path.Combine(list.RootFolder.ServerRelativeUrl, (attachment.Name)); Microsoft.SharePoint.Client.File.SaveBinaryDirect(spContext, fileUrl, ms, true); //Microsoft.SharePoint.Client.File.SaveBinaryDirect(spContext, targetFileUrl, ms, true); spContext.ExecuteQuery(); Microsoft.SharePoint.Client.File newFile = spContext.Web.GetFileByServerRelativeUrl(fileUrl); ListItem item = newFile.ListItemAllFields; UpdateTaxonomyFields(item, attachment); } } catch (Exception ex) { processedCount++; attachmentNames.Add("ERROR: " + ex.Message); } // Close the response stream. responseStream.Close(); webResponse.Close(); processedCount++; attachmentNames.Add(attachment.Name); } } OutlookServiceResponse response = new OutlookServiceResponse { AttachmentNames = attachmentNames.ToArray(), AttachmentsProcessed = processedCount }; response.IsError = false; return(response); }