/// <summary> /// Creates the ATWS client for the API. /// </summary> /// <returns>The ATWS client.</returns> private ATWS GetClient() { string username = ConfigurationManager.AppSettings["Username"]; string password = ConfigurationManager.AppSettings["Password"]; ATWS service = new ATWS { Url = ConfigurationManager.AppSettings["BaseURL"] }; try { ATWSZoneInfo zoneInfo = service.getZoneInfo(username); string urlZone = zoneInfo.URL; service = new ATWS { Url = urlZone }; NetworkCredential credentials = new NetworkCredential(username, password); CredentialCache cache = new CredentialCache { { new Uri(service.Url), "Basic", credentials } }; service.Credentials = cache; } catch (Exception exception) { throw new Exception("Error creating web service: " + exception.Message); } return(service); }
/// <summary> /// The submitButton_Click method. /// </summary> /// <param name="sender">The Sender.</param> /// <param name="e">The EventArgs.</param> protected void SubmitButtonClick(object sender, EventArgs e) { this.error.Visible = false; string email = this.exampleInputEmail.Text; ArrayList emails = (ArrayList)this.Session["Emails"]; List <Contact> contacts = (List <Contact>) this.Session["Contacts"]; if (string.IsNullOrEmpty(email) || !emails.Contains(email)) { this.error.Visible = true; this.error.InnerText = "Email doesn't exist in the database."; return; } if (!this.exampleInputFile.HasFile) { this.error.Visible = true; this.error.InnerText = "Please select a file to upload."; return; } Contact contact = contacts.Find(c => c.EMailAddress.ToString().Equals(email)); Account account = GetAccount(contact.AccountID); Attachment attachment = new Attachment { Info = new AttachmentInfo { FullPath = this.exampleInputFile.FileName, ParentID = contact.AccountID, ParentType = 1, // Account Publish = 1, // All Autotask Users Title = this.exampleInputFile.FileName, Type = "FILE_ATTACHMENT" }, Data = this.exampleInputFile.FileBytes }; long result = client.CreateAttachment(attachment); if (result > 0) { ATWSZoneInfo zoneInfo = client.getZoneInfo(ConfigurationManager.AppSettings["Username"]); this.success.Visible = true; this.accountCommand.NavigateUrl = zoneInfo.WebUrl + "Autotask/AutotaskExtend/ExecuteCommand.aspx?Code=OpenAccount&AccountID=" + attachment.Info.ParentID; this.accountCommand.Text = account?.AccountName.ToString(); } else { this.error.Visible = true; this.error.InnerText = "The file failed to upload. Please try again."; } }