示例#1
0
        private static HttpStatusCode UploadAttachment(LoginUser _loginUser, CRMLinkErrors _crmErrors, TeamSupport.ServiceLibrary.Log _log, string incidentId, string encodedCredentials, ref string responseString, Attachment attachment, string hostName)
        {
            HttpStatusCode resultStatusCode = HttpStatusCode.BadRequest;
            string         error            = string.Empty;

            if (File.Exists(attachment.Path))
            {
                FileStream attachmentFileStream = new FileStream(attachment.Path, FileMode.Open, FileAccess.Read);
                int        fileSizeLimit        = 50000000; //vv 50MB. can we get this from Snow?

                if (attachmentFileStream.Length <= fileSizeLimit)
                {
                    try
                    {
                        string         url     = GetFullUrl(hostName, string.Format("api/now/attachment/file?table_name=incident&table_sys_id={0}&file_name={1}", incidentId, System.Web.HttpUtility.UrlEncode(attachment.FileName)));
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                        request.Method      = "POST";
                        request.ContentType = "multipart/form-data";
                        request.Accept      = "application/json";
                        request.Headers.Add("Authorization", "Basic " + encodedCredentials);

                        MemoryStream content = new MemoryStream();
                        StreamWriter writer  = new StreamWriter(content);
                        byte[]       data    = new byte[attachmentFileStream.Length + 1];
                        attachmentFileStream.Read(data, 0, data.Length);
                        attachmentFileStream.Close();
                        content.Write(data, 0, data.Length);
                        writer.WriteLine();
                        writer.Flush();
                        content.Seek(0, SeekOrigin.Begin);
                        request.ContentLength = content.Length;

                        using (Stream requestStream = request.GetRequestStream())
                        {
                            content.WriteTo(requestStream);
                            requestStream.Close();
                        }

                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {
                            _log.Write("Attachment \"" + attachment.FileName + "\" sent.");
                            Stream responseStream = response.GetResponseStream();

                            if (responseStream != null)
                            {
                                var streamReader = new StreamReader(responseStream);
                                responseString = streamReader.ReadToEnd();
                            }

                            resultStatusCode = response.StatusCode;
                            response.Close();
                        }

                        content.Flush();
                        content.Close();
                        attachment.SentToSnow = true;
                        attachment.Collection.Save();
                        _log.ClearCrmLinkError(attachment.AttachmentID.ToString(), string.Empty, _crmErrors);
                    }
                    catch (WebException webEx)
                    {
                        using (var response = (HttpWebResponse)webEx.Response)
                        {
                            Stream responseStream = response.GetResponseStream();

                            if (responseStream != null)
                            {
                                var    reader       = new StreamReader(responseStream);
                                string responseText = string.Format("Error code: {0} - {1}{2}{3}", response.StatusCode, response.StatusDescription, Environment.NewLine, reader.ReadToEnd());
                                throw new Exception(responseText);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message, ex.InnerException);
                    }
                }
                else
                {
                    error = string.Format("Attachment was not sent as its file size ({0}) exceeded the file size limit of {1}", attachmentFileStream.Length.ToString(), fileSizeLimit.ToString());
                }
            }
            else
            {
                error = "Attachment was not sent as it was not found on server";
            }

            if (!string.IsNullOrEmpty(error))
            {
                _crmErrors.LoadByOperationAndObjectId(attachment.OrganizationID, Enums.GetDescription(IntegrationType.ServiceNow), Enums.GetDescription(IntegrationOrientation.OutToServiceNow), "attachment", attachment.AttachmentID.ToString());
                _log.WriteToCrmErrorReport(_loginUser, error, attachment.OrganizationID, attachment.AttachmentID.ToString(), "attachment", "file", attachment.FileName, "create", IntegrationType.ServiceNow, Enums.GetDescription(IntegrationOrientation.OutToServiceNow), _crmErrors, logText: true);
            }

            return(resultStatusCode);
        }
示例#2
0
        public static string UpdloadAttachments(LoginUser _loginUser, CRMLinkErrors _crmErrors, TeamSupport.ServiceLibrary.Log _log, Data.Action action, string encodedCredentials, string result, string appId, string hostName)
        {
            Attachments attachments = new Attachments(_loginUser);

            attachments.LoadForIntegration(action.ActionID, IntegrationType.ServiceNow);

            foreach (Attachment attachment in attachments)
            {
                UploadAttachment(_loginUser, _crmErrors, _log, appId, encodedCredentials, ref result, attachment, hostName);
            }

            return(result);
        }