/// <summary> /// This method searches for a resource given a username. /// </summary> /// <param name="resourceUserName">Contains the resource username to search for</param> /// <returns>ID of the resource.</returns> public long FindResource(string resourceUserName) { long ret = -1; // Query Resource to get the owner of the lead StringBuilder strResource = new StringBuilder(); strResource.Append("<queryxml version=\"1.0\">"); strResource.Append("<entity>Resource</entity>"); strResource.Append("<query>"); strResource.Append("<field>UserName<expression op=\"equals\">"); strResource.Append(resourceUserName); strResource.Append("</expression></field>"); strResource.Append("</query></queryxml>"); ATWSResponse respResource = this.atwsServices.query(strResource.ToString()); if (respResource.ReturnCode > 0 && respResource.EntityResults.Length > 0) { // Get the ID for the resource ret = respResource.EntityResults[0].id; } return(ret); }
/// <summary> /// This method searches for a contact given an email address. /// </summary> /// <param name="contactEmail">The contact email address to search for.</param> /// <returns>The matching contact.</returns> public Contact FindContact(string contactEmail) { Contact contact = null; if (contactEmail.Length <= 0) { return(null); } // Query Contact to see if the contact is already in the system StringBuilder strResource = new StringBuilder(); strResource.Append("<queryxml version=\"1.0\">"); strResource.Append("<entity>Contact</entity>"); strResource.Append("<query>"); strResource.Append("<field>EMailAddress<expression op=\"equals\">"); strResource.Append(contactEmail); strResource.Append("</expression></field>"); strResource.Append("</query></queryxml>"); ATWSResponse respResource = this.atwsServices.query(strResource.ToString()); if (respResource.ReturnCode > 0 && respResource.EntityResults.Length > 0) { contact = (Contact)respResource.EntityResults[0]; } return(contact); }
/// <summary> /// This method will update a UDF Picklist for a given Contact. /// </summary> /// <param name="contact">Contact to update.</param> /// <param name="udfName">UDF Name to update.</param> /// <param name="udfPicklistLabel">UDF Picklist Label to update</param> /// <returns>Returns the updated Contact.</returns> public Contact UpdateContactUdfPicklist(Contact contact, string udfName, string udfPicklistLabel) { Contact retContact = null; UserDefinedField fldFieldToFind = FindUserDefinedField(contact.UserDefinedFields, udfName); Field[] fieldsUdfContact = this.atwsServices.getUDFInfo("Contact"); string strUdfPickListValue = PickListValueFromField(fieldsUdfContact, udfName, udfPicklistLabel); if (fldFieldToFind != null) { fldFieldToFind.Value = strUdfPickListValue; } else { fldFieldToFind = new UserDefinedField { Name = udfName, Value = strUdfPickListValue }; contact.UserDefinedFields = new[] { fldFieldToFind }; } Entity[] entUpdateContact = { contact }; ATWSResponse respUpdateContact = this.atwsServices.update(entUpdateContact); if (respUpdateContact.ReturnCode == -1) { throw new Exception("Could not update the Contact: " + respUpdateContact.EntityReturnInfoResults[0].Message); } if (respUpdateContact.ReturnCode > 0 && respUpdateContact.EntityResults.Length > 0) { retContact = (Contact)respUpdateContact.EntityResults[0]; } return(retContact); }
/// <summary> /// Gets the ticket using the ticket id. /// </summary> /// <param name="client">The API client.</param> /// <param name="ticketId">The id of the ticket to get.</param> /// <returns>A ticket entity or null if no results were returned.</returns> private Ticket GetTicket(ATWS client, string ticketId) { StringBuilder xml = new StringBuilder(); xml.AppendLine("<queryxml>"); xml.AppendLine("<entity>Ticket</entity>"); xml.AppendLine("<query>"); xml.Append("<field>id<expression op=\"equals\">").Append(ticketId).AppendLine("</expression></field>"); xml.AppendLine("</query>"); xml.AppendLine("</queryxml>"); ATWSResponse response = client.query(xml.ToString()); if (response.ReturnCode == -1) { StringBuilder message = new StringBuilder(); foreach (ATWSError atwsError in response.Errors) { message.Append(atwsError.Message).Append(Environment.NewLine); } throw new Exception(message.ToString()); } if (response.EntityResults.Length > 0) { return(response.EntityResults[0] as Ticket); } return(null); }
//end GetProjects public List <Task> GetTasks(TaskFilters filters = null) { List <Task> tasks = new List <Task>(); if (filters == null) { bool queryDone = false; long currentId = 0; while (!queryDone) { StringBuilder strTask = new StringBuilder(); strTask.Append("<queryxml version=\"1.0\">"); strTask.Append("<entity>Task</entity>"); strTask.Append("<query>"); strTask.Append("<field>Id<expression op=\"greaterthan\">"); strTask.Append(currentId); strTask.Append("</expression></field>"); strTask.Append("</query>"); strTask.Append("</queryxml>"); ATWSResponse respTask = this._atwsServices.query(strTask.ToString()); if (respTask.ReturnCode > 0 && respTask.EntityResults.Length > 0) { List <Task> temp = respTask.EntityResults.Cast <Task>().ToList(); currentId = temp[temp.Count - 1].id; tasks.AddRange(temp); temp = null; } else { queryDone = true; } } } else { StringBuilder strTask = new StringBuilder(); strTask.Append("<queryxml version=\"1.0\">"); strTask.Append("<entity>Task</entity>"); strTask.Append("<query>"); if (filters.ProjectID.HasValue) { strTask.Append("<field>ProjectID<expression op=\"equals\">"); strTask.Append(filters.ProjectID.ToString()); strTask.Append("</expression></field>"); } strTask.Append("</query></queryxml>"); ATWSResponse respTask = this._atwsServices.query(strTask.ToString()); if (respTask.ReturnCode > 0 && respTask.EntityResults.Length > 0) { tasks = respTask.EntityResults.Cast <Task>().ToList(); } } return(tasks); }
//end GetTickets /// <summary> /// This method searches for projects. /// </summary> /// <param name="filters">Contains the ProjectFilters object to search with</param> /// <returns>a list of matching projects.</returns> public List <Project> GetProjects(ProjectFilters filters = null) { List <Project> projects = new List <Project>(); if (filters == null) { bool queryDone = false; long currentId = 0; while (!queryDone) { StringBuilder strProject = new StringBuilder(); strProject.Append("<queryxml version=\"1.0\">"); strProject.Append("<entity>Project</entity>"); strProject.Append("<query>"); strProject.Append("<field>Id<expression op=\"greaterthan\">"); strProject.Append(currentId); strProject.Append("</expression></field>"); strProject.Append("</query>"); strProject.Append("</queryxml>"); ATWSResponse respProject = this._atwsServices.query(strProject.ToString()); if (respProject.ReturnCode > 0 && respProject.EntityResults.Length > 0) { List <Project> temp = respProject.EntityResults.Cast <Project>().ToList(); currentId = temp[temp.Count - 1].id; projects.AddRange(temp); temp = null; } else { queryDone = true; } } } else { StringBuilder strProject = new StringBuilder(); strProject.Append("<queryxml version=\"1.0\">"); strProject.Append("<entity>Project</entity>"); strProject.Append("<query>"); if (!string.IsNullOrEmpty(filters.ProjectNumber)) { strProject.Append("<field>ProjectNumber<expression op=\"equals\">"); strProject.Append(filters.ProjectNumber); strProject.Append("</expression></field>"); } strProject.Append("</query></queryxml>"); ATWSResponse respProject = this._atwsServices.query(strProject.ToString()); if (respProject.ReturnCode > 0 && respProject.EntityResults.Length > 0) { projects = respProject.EntityResults.Cast <Project>().ToList(); } } return(projects); }
/// <summary> /// The Page_Load Method. /// </summary> /// <param name="sender">The Sender.</param> /// <param name="e">The EventArgs.</param> protected void Page_Load(object sender, EventArgs e) { string ticketId = this.Request.Params["TicketID"]; if (string.IsNullOrEmpty(ticketId)) { return; } string rating = this.Request.Params["Rating"]; ATWS client = this.GetClient(); Ticket ticket = this.GetTicket(client, ticketId); if (ticket == null) { return; } // If the ticket has no UDF values populated the user defined field array will be empty. if (ticket.UserDefinedFields.Length == 0) { UserDefinedField responseRating = new UserDefinedField { Name = "Emoticon Response", Value = rating }; UserDefinedField responseDate = new UserDefinedField { Name = "Emoticon Response Date", Value = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture) }; ticket.UserDefinedFields = new UserDefinedField[2]; ticket.UserDefinedFields[0] = responseRating; ticket.UserDefinedFields[1] = responseDate; } else { UserDefinedField responseRating = ticket.UserDefinedFields.First(udf => udf.Name.Equals("Emoticon Response")); responseRating.Value = rating; UserDefinedField responseDate = ticket.UserDefinedFields.First(udf => udf.Name.Equals("Emoticon Response Date")); responseDate.Value = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); } ATWSResponse response = client.update(new Entity[] { ticket }); if (response.ReturnCode != -1) { return; } StringBuilder message = new StringBuilder(); foreach (ATWSError atwsError in response.Errors) { message.Append(atwsError.Message).Append(Environment.NewLine); } throw new Exception(message.ToString()); }
/// <summary> /// Creates a new Contact. /// </summary> /// <param name="accountId">Account ID of account to create contact.</param> /// <param name="firstName">Contact first name.</param> /// <param name="lastName">Contact last name.</param> /// <param name="email">Contact email address.</param> /// <param name="udfName">UDF Name to set.</param> /// <param name="udfValue">UDF Value to set</param> /// <returns>>Returns the new Contact.</returns> public Contact CreateContact(long accountId, string firstName, string lastName, string email, string udfName, string udfValue) { Contact retContact = null; // Time to create the Contact Contact contactAct = new Contact { AccountID = accountId, FirstName = firstName, LastName = lastName, EMailAddress = email.ToLower(), Active = "1" }; Field[] fieldsUdfContact = this.atwsServices.getUDFInfo("Contact"); // Create a container to hold all the UDF's List <UserDefinedField> udfContainer = new List <UserDefinedField>(); Field fldFieldToFind = FindField(fieldsUdfContact, udfName); if (fldFieldToFind != null) { udfContainer.Add(new UserDefinedField() { Name = udfName, Value = udfValue }); } // Time to add the UDF's from the container to the Contact contactAct.UserDefinedFields = udfContainer.ToArray(); Entity[] entContact = { contactAct }; ATWSResponse respContact = this.atwsServices.create(entContact); if (respContact.ReturnCode > 0 && respContact.EntityResults.Length > 0) { retContact = (Contact)respContact.EntityResults[0]; } else { if (respContact.EntityReturnInfoResults.Length > 0) { throw new Exception("Could not create the Contact: " + respContact.EntityReturnInfoResults[0].Message); } } return(retContact); }
/// <summary> /// Creates a ticket note by the provided resource id using Impersonation. /// </summary> /// <param name="assignResourceId">The resource id.</param> /// <returns>A new ticket note.</returns> public AccountNote CreateAccountNoteAs(long impersonateResourceId, long assignResourceId, DateTime startDateTime, DateTime endDateTime) { AccountNote retNote = null; Field[] fields = this.atwsServices.GetFieldInfo("AccountNote"); string actionTypeValue = PickListValueFromField(fields, "ActionType", "General"); AccountNote note = new AccountNote { id = 0, AccountID = 0, Note = "Test note created via impersonation", StartDateTime = startDateTime, EndDateTime = endDateTime, ActionType = actionTypeValue, AssignedResourceID = assignResourceId }; Entity[] entContact = { note }; this.atwsServices.AutotaskIntegrationsValue.ImpersonateAsResourceID = (int)impersonateResourceId; ATWSResponse respContact = this.atwsServices.create(entContact); if (respContact.ReturnCode > 0 && respContact.EntityResults.Length > 0) { retNote = (AccountNote)respContact.EntityResults[0]; } else { if (respContact.EntityReturnInfoResults.Length > 0) { throw new Exception("Could not create the Account Note: " + respContact.EntityReturnInfoResults[0].Message); } } return(retNote); }
public AutoTaskApiException(ATWSResponse queryResult) : base(string.Join(", ", queryResult.Errors.Select(e => e.Message))) { Response = queryResult; }
} //end Query(Type entity_type, List<QueryFilter> filters = null) /// <summary> /// Queries entities using instance /// </summary> /// <param name="entity">instance of entity to query</param> /// <param name="filters">query filters</param> /// <returns></returns> public IEnumerable <Entity> Query(Entities.Entity entity, QueryFilter filters = null) { //List<Entities.Entity> entities = null; if (!entity.CanQuery) { throw new AutotaskNETException($"The {entity.GetType().Name} entity can not be queried."); } else { //query entity //entities = new List<Entities.Entity>(); bool query_done = false; long current_id = 0; while (!query_done) { //create query StringBuilder query = new StringBuilder(); query.Append("<queryxml version=\"1.0\">"); query.Append($"<entity>{entity.GetType().Name}</entity>"); query.Append("<query>"); #region QueryFilters //use id to pull more than 500 records if id is not already a filter if (filters == null || !QueryFilter.ContainsCondition(new QueryCondition(filters), "id")) { query.Append($"<field>Id<expression op=\"greaterthan\">{current_id}</expression></field>"); } //handle all other filters if (filters != null) { string cond = this.ParseConditions(new QueryCondition(filters)); query.Append(cond); } #endregion //QueryFilters query.Append("</query>"); query.Append("</queryxml>"); //Console.WriteLine(query.ToString()); //submit query ATWSResponse response = this._atws.queryAsync(new queryRequest( new AutotaskIntegrations(), query.ToString())).Result.queryResult; //parse response if (response.ReturnCode > 0 && response.EntityResults.Length > 0) { List <Entities.Entity> temp_entities = new List <Entities.Entity>(); foreach (net.autotask.webservices.Entity atws_entity in response.EntityResults) { temp_entities.Add((Entities.Entity)Activator.CreateInstance(entity.GetType(), atws_entity)); } current_id = temp_entities.First(e => e.id == temp_entities.Max(m => m.id)).id; foreach (var i in temp_entities) { yield return(i); } //entities.AddRange(temp_entities); temp_entities = null; //########################################################################### if ((filters != null && QueryFilter.ContainsCondition( new QueryCondition(filters), "id")) || response.EntityResults.Length < 500) { query_done = true; } } else { //no results or error query_done = true; } } } //return entities; } //end Query(Entities.Entity entity, List<QueryFilter> filters = null)
/// <summary> /// This method searches for accounts. /// </summary> /// <param name="filters">Contains the AccountFilters object to search with</param> /// <returns>a list of matching accounts.</returns> public List <Account> GetAccounts(AccountFilters filters = null) { List <Account> accounts = new List <Account>(); if (filters == null) { bool queryDone = false; long currentId = 0; while (!queryDone) { StringBuilder strAccount = new StringBuilder(); strAccount.Append("<queryxml version=\"1.0\">"); strAccount.Append("<entity>Account</entity>"); strAccount.Append("<query>"); strAccount.Append("<field>Id<expression op=\"greaterthan\">"); strAccount.Append(currentId); strAccount.Append("</expression></field>"); strAccount.Append("</query>"); strAccount.Append("</queryxml>"); ATWSResponse respAccount = this._atwsServices.query(strAccount.ToString()); if (respAccount.ReturnCode > 0 && respAccount.EntityResults.Length > 0) { List <Account> temp = respAccount.EntityResults.Cast <Account>().ToList(); currentId = temp[temp.Count - 1].id; accounts.AddRange(temp); temp = null; } else { queryDone = true; } } } else { StringBuilder strAccount = new StringBuilder(); strAccount.Append("<queryxml version=\"1.0\">"); strAccount.Append("<entity>Account</entity>"); strAccount.Append("<query>"); if (filters.Id.HasValue) { strAccount.Append("<field>Id<expression op=\"equals\">"); strAccount.Append(filters.Id); strAccount.Append("</expression></field>"); } if (!string.IsNullOrEmpty(filters.AccountName)) { strAccount.Append("<field>AccountName<expression op=\"equals\">"); strAccount.Append(filters.AccountName); strAccount.Append("</expression></field>"); } strAccount.Append("</query>"); strAccount.Append("</queryxml>"); ATWSResponse respAccount = this._atwsServices.query(strAccount.ToString()); if (respAccount.ReturnCode > 0 && respAccount.EntityResults.Length > 0) { accounts = respAccount.EntityResults.Cast <Account>().ToList(); } } return(accounts); }
//end GetContacts /// <summary> /// This method searches for tickets. /// </summary> /// <param name="filters">Contains the TicketFilters object to search with</param> /// <returns>a list of matching tickets.</returns> public List <Ticket> GetTickets(TicketFilters filters = null) { List <Ticket> tickets = new List <Ticket>(); if (filters == null) { bool queryDone = false; long currentId = 0; while (!queryDone) { StringBuilder strTicket = new StringBuilder(); strTicket.Append("<queryxml version=\"1.0\">"); strTicket.Append("<entity>Ticket</entity>"); strTicket.Append("<query>"); strTicket.Append("<field>Id<expression op=\"greaterthan\">"); strTicket.Append(currentId); strTicket.Append("</expression></field>"); strTicket.Append("</query>"); strTicket.Append("</queryxml>"); ATWSResponse respTicket = this._atwsServices.query(strTicket.ToString()); if (respTicket.ReturnCode > 0 && respTicket.EntityResults.Length > 0) { List <Ticket> temp = respTicket.EntityResults.Cast <Ticket>().ToList(); currentId = temp[temp.Count - 1].id; tickets.AddRange(temp); temp = null; } else { queryDone = true; } } } else { StringBuilder strTicket = new StringBuilder(); strTicket.Append("<queryxml version=\"1.0\">"); strTicket.Append("<entity>Ticket</entity>"); strTicket.Append("<query>"); if (!string.IsNullOrEmpty(filters.TicketNumber)) { strTicket.Append("<field>TicketNumber<expression op=\"equals\">"); strTicket.Append(filters.TicketNumber); strTicket.Append("</expression></field>"); } if (filters.LastActivityDate.HasValue && !string.IsNullOrEmpty(filters.opLastActivityDate)) { strTicket.Append("<field>TicketNumber<expression op=\""); strTicket.Append(filters.opLastActivityDate); strTicket.Append("\">"); strTicket.Append(filters.TicketNumber); strTicket.Append("</expression></field>"); } strTicket.Append("</query></queryxml>"); ATWSResponse respTicket = this._atwsServices.query(strTicket.ToString()); if (respTicket.ReturnCode > 0 && respTicket.EntityResults.Length > 0) { tickets = respTicket.EntityResults.Cast <Ticket>().ToList(); } } return(tickets); }
/// <summary> /// Gets the list of active contacts in the database. /// </summary> /// <returns>A list of contact entities.</returns> private static List <Contact> GetContacts() { List <Contact> contacts = new List <Contact>(); StringBuilder xml = new StringBuilder(); xml.AppendLine("<queryxml>"); xml.AppendLine("<entity>Contact</entity>"); xml.AppendLine("<query>"); xml.AppendLine("<field>Active<expression op=\"equals\">1</expression></field>"); xml.AppendLine("</query>"); xml.AppendLine("</queryxml>"); ATWSResponse response = client.query(xml.ToString()); if (response.ReturnCode == -1) { StringBuilder message = new StringBuilder(); foreach (ATWSError atwsError in response.Errors) { message.Append(atwsError.Message).Append(Environment.NewLine); } throw new Exception(message.ToString()); } // The API returns a max of 500 records. If 500 are returned the query must be looped to return the rest. if (response.EntityResults.Length == 500) { contacts.AddRange(response.EntityResults.Cast <Contact>()); while (response.EntityResults.Length == 500) { // Results are returned in order or id. Making the next query get all results greater than the last // id value will get the next section of results. long lastId = response.EntityResults[response.EntityResults.Length - 1].id; xml = new StringBuilder(); xml.AppendLine("<queryxml>"); xml.AppendLine("<entity>Contact</entity>"); xml.AppendLine("<query>"); xml.Append("<field>id<expression op=\"greaterthan\">") .Append(lastId) .AppendLine("</expression></field>"); xml.AppendLine("<field>Active<expression op=\"equals\">1</expression></field>"); xml.AppendLine("</query>"); xml.AppendLine("</queryxml>"); response = client.query(xml.ToString()); if (response.ReturnCode == -1) { StringBuilder message = new StringBuilder(); foreach (ATWSError atwsError in response.Errors) { message.Append(atwsError.Message).Append(Environment.NewLine); } throw new Exception(message.ToString()); } contacts.AddRange(response.EntityResults.Cast <Contact>()); } } else { contacts.AddRange(response.EntityResults.Cast <Contact>()); } return(contacts); }