Working with a support ticket using tthe InstantKB .NET API



 In this article we'll show how to use the InstantKB .NET API to query & display information for a single support ticket within InstantKB. To use the code in this article you'll first need to ensure you reference the InstantKB assemblies from your Visual Studio project.

Getting Started

Add the necessary using statements to your C# class...

using InstantASP.InstantKB;

Simple Example

In this example we'll ignore all security checks and use the InstantKB .NET API to query and display some basic information for an existing support ticket. All methods to work with a support ticket can be found within the InstantASP.InstantKB.Business.TicketRepository class. You can see below we are obtaining information for a ticket with ID 3 and displaying this information.

// initialize our TicketRepository class
// notice the empty constructor if no
// InstantASP.InstantKB.Components.User 
// object is passed into the constructor
// we can query and display any ticket
// even if we didn't create the ticket
var t = new Business.TicketRepository();

// get ticket where InstantKB_Tickets.TicketID = 3
var ticket = t.SelectTicket(5);

// ensure we found a ticket
if (ticket != null)
{
// display some basic ticket information
Response.Write(string.Format("Title: {0}<br/>", ticket.TicketTitle));
Response.Write(string.Format("Created: {0}<br/>", ticket.TicketCreatedDate));
Response.Write(string.Format("Message: {0}<br/>", ticket.TicketText));
}

The InstantASP.InstantKB.Components.Ticket object returned by the SelectTicket method has several dozen properties allowing you to display all of the meta data associated with a support ticket.

The example below shows how to display some of this additional information such as status, priority, workflow details...

var t = new Business.TicketRepository();
var ticket = t.SelectTicket(5);

if (ticket != null)
{

// general details

Response.Write(string.Format("TicketID: {0}<br/>", ticket.TicketID));
Response.Write(string.Format("Escalated: {0}<br/>", ticket.TicketIsEscalated));
Response.Write(string.Format("Overdue: {0}<br/>", ticket.TicketOverdue));
Response.Write(string.Format("Archived: {0}<br/>", ticket.TicketIsArchived));

// created by

if (ticket.TicketCreatedUserID > 0)
{
var createdBy = Business.UserRepository.SelectUser(ticket.TicketCreatedUserID);
if (createdBy != null)
{
Response.Write(string.Format("Created By: {0}<br/>", createdBy.DisplayName));
}

}
else
{
Response.Write(string.Format("Created By: {0}<br/>", ticket.TicketCreatedUsername));
}

// category

if (ticket.TicketCategoryID > 0)
{

var c = new Business.CategoriesRepository();
var category = c.SelectCategory(ticket.TicketCategoryID);
if (category != null)
{
Response.Write("<strong>Category</strong><br/>");
Response.Write(string.Format("ID: {0}<br/>", category.CategoryID));
Response.Write(string.Format("Name: {0}<br/>", category.CategoryName));
}

}

// prioerity 

if (ticket.TicketPriority != null)
{
Response.Write("<strong>Priority</strong><br/>");
Response.Write(string.Format("ID: {0}<br/>", ticket.TicketPriority.PriorityID));
Response.Write(string.Format("Name: {0}<br/>", ticket.TicketPriority.Name));
}

// status

if (ticket.TicketStatus != null)
{
Response.Write("<strong>Status</strong><br/>");
Response.Write(string.Format("ID: {0}<br/>", ticket.TicketStatus.StatusID));
Response.Write(string.Format("Name: {0}<br/>", ticket.TicketStatus.Name));
}

// workflow step

if (ticket.TicketWorkFlowStep != null)
{
Response.Write("<strong>Work Flow</strong><br/>");
Response.Write(string.Format("Step ID: {0}<br/>", ticket.TicketWorkFlowStep.WorkFlowStepID));
Response.Write(string.Format("Step Name: {0}<br/>", ticket.TicketWorkFlowStep.Name));
}

// type

if (ticket.TicketType != null)
{
Response.Write("<strong>Type</strong><br/>");
Response.Write(string.Format("Type ID: {0}<br/>", ticket.TicketType.TypeID));
Response.Write(string.Format("Type Name: {0}<br/>", ticket.TicketType.Name));
}

}
 

Enforcing Security 

In the above example we don't perform any security checks to ensure the current authenticated user executing the server side .NET API has permission to view the requested support ticket. When using the SelectTicket method you will need to ensure you enforce appropriate security checks to ensure the user requesting the ticket has permission to view the ticket.

It's reccomended you check to ensure the user requesting the ticket is the author of the ticket or is currently authetnicated as a trusted administrator or support agent. In the below example only if the currently authenciasted user created ticket ID 5 or belongs to the support agent or administrator role would ticket information be displayed...

var t = new Business.TicketRepository();
var ticket = t.SelectTicket(5);
if (ticket != null)
{
var kbContext = HttpContext.KnowledgeBaseContext.Current;
var isAuthor = (kbContext.CurrentUser.UserID ==
ticket.TicketCreatedUserID);
var isAdmin = kbContext.CurrentUser.IsAdministrator;
var isAgent = kbContext.CurrentUser.IsModerator;
var isAuthorized = isAuthor | isAdmin | isAgent;
if (isAuthorized)
{
Response.Write(string.Format("TicketID: {0}<br/>", ticket.TicketID));
}
}

Ticket Replies

So far we've shown how to query and display information about a support ticket stored within InstantKB. A support ticket typically has several resplies both from support agents and users as your agents work with the user until the ticket is resolved. In the example below we show how to iterate through the replies within a ticket and display the text within each reply...

var t = new Business.TicketRepository();
var ticket = t.SelectTicket(12);
if (ticket != null)
{
var kbContext = HttpContext.KnowledgeBaseContext.Current;
var isAuthor = (kbContext.CurrentUser.UserID ==
ticket.TicketCreatedUserID);
var isAdmin = kbContext.CurrentUser.IsAdministrator;
var isAgent = kbContext.CurrentUser.IsModerator;
var isAuthorized = isAuthor | isAdmin | isAgent;
if (isAuthorized)
{

Response.Write(string.Format("TicketID: {0}<br/>", ticket.TicketID));
Response.Write(string.Format("Title: {0}<br/>", ticket.TicketTitle));
Response.Write(string.Format("Message: {0}<br/>", ticket.TicketText));

if (ticket.TicketReplyCollection != null)
{
var i = 1;
Response.Write("<strong>Replies</strong><br/>");
foreach (var reply in ticket.TicketReplyCollection)
{
Response.Write(string.Format("Reply: {0}<br/>", i));
Response.Write(string.Format("Message: {0}<br/>", reply.TicketText));
i += 1;
}
}
}

}

That's It!

We hope these examples help you work with support tickets via the InstantKB .NET API. If we can provide any further examples not covered here of course please don't hesitate to submit a support request and we'll happily create some documenation to asist.