public static string GetTickets(RestCommand command) { string xml = ""; bool hasBeenFiltered = false; int totalRecords = 0; if (command.IsPaging) { try { TicketsView tickets = new TicketsView(command.LoginUser); tickets.LoadAllTicketIds(command.Organization.OrganizationID, command.Filters, command.PageNumber, command.PageSize); hasBeenFiltered = true; XmlTextWriter writer = Tickets.BeginXmlWrite("Tickets"); foreach (int ticketTypeId in tickets.GroupBy(g => g.TicketTypeID).Select(p => p.Key).ToList()) { try { TicketsView ticketsResult = new TicketsView(command.LoginUser); ticketsResult.LoadByTicketIDList(command.Organization.OrganizationID, ticketTypeId, tickets.Where(w => w.TicketTypeID == ticketTypeId).Select(p => p.TicketID).ToList()); foreach (DataRow row in ticketsResult.Table.Rows) { int ticketId = (int)row["TicketID"]; Tags tags = new Tags(command.LoginUser); tags.LoadByReference(ReferenceType.Tickets, ticketId); tags = tags ?? new Tags(command.LoginUser); ticketsResult.WriteXml(writer, row, "Ticket", true, !hasBeenFiltered ? command.Filters : new System.Collections.Specialized.NameValueCollection(), tags); } } catch (Exception ex) { ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). Paging."); } } if (tickets.Count > 0) { totalRecords = tickets[0].TotalRecords; } writer.WriteElementString("TotalRecords", totalRecords.ToString()); xml = Tickets.EndXmlWrite(writer); } catch (Exception ex) { ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). Paging. SQL filtering generation failed."); throw new RestException(HttpStatusCode.InternalServerError, "There was an error processing your request. Please contact TeamSupport.com", ex); } } else { //No Paging if (command.Filters["TicketTypeID"] != null) { try { TicketsView tickets = new TicketsView(command.LoginUser); int ticketTypeID = int.Parse(command.Filters["TicketTypeID"]); TicketType ticketType = TicketTypes.GetTicketType(command.LoginUser, ticketTypeID); if (ticketType.OrganizationID != command.Organization.OrganizationID) { throw new Exception(); } try { tickets.LoadByTicketTypeID(ticketTypeID, command.Organization.OrganizationID, command.Filters); } catch (Exception ex) { //if something fails use the old method tickets.LoadByTicketTypeID(ticketTypeID); ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). No Paging. SQL filtering generation failed, fell into old method."); } xml = tickets.GetXml("Tickets", "Ticket", true, command.Filters); xml = AddTagsToTickets(xml, command); } catch (Exception ex) { throw new RestException(HttpStatusCode.NotAcceptable, "Invalid TicketTypeID to filter.", ex); } } else { TicketTypes ticketTypes = new TicketTypes(command.LoginUser); ticketTypes.LoadByOrganizationID(command.Organization.OrganizationID); TicketsView tickets = new TicketsView(command.LoginUser); XmlTextWriter writer = Tickets.BeginXmlWrite("Tickets"); foreach (TicketType ticketType in ticketTypes) { try { tickets.LoadByTicketTypeID(ticketType.TicketTypeID, command.Organization.OrganizationID, command.Filters); } catch (Exception ex) { if (ex is System.Data.SqlClient.SqlException && ex.Message.ToLower().Contains("variable names must be unique within a query batch or stored procedure")) { throw ex; } else { //if something fails use the old method tickets.LoadByTicketTypeID(ticketType.TicketTypeID); ExceptionLogs.LogException(command.LoginUser, ex, "API", "RestTickets. GetTickets(). No Paging. No TicketTypeId filter. SQL filtering generation failed, fell into old method."); } } foreach (DataRow row in tickets.Table.Rows) { int ticketId = (int)row["TicketID"]; Tags tags = new Tags(command.LoginUser); tags.LoadByReference(ReferenceType.Tickets, ticketId); tags = tags ?? new Tags(command.LoginUser); tickets.WriteXml(writer, row, "Ticket", true, command.Filters, tags); } } xml = Tickets.EndXmlWrite(writer); } } return(xml); }