Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Adding tickets using the InstantKB .NET API

New

This article demonstrates how to add support tickets into InstantKB via the InstantKB .NET API. To use the InstantKB .NET API you will first need to reference the InstantKB assemblies from your .NET project and include the InstantKB database connection string within the web.config or app.config file.

Adding Tickets Programmatically

The code example below demonstrates how to add tickets into InstantKB using the .NET API. For readability purposes we've kept this code within a single method however for maintainability you may wish to break-up this code into more reusable functions. We've also included the full namespace for all API calls within this example. You could of course add a using statement to the top of your C# class to minimize typing. I.e. "using InstantASP.InstantKB".

The following C# code shows how to add a support ticket within InstantKB...

// local variables

var emailAddress = "email@domain.com";
var subject = "Ticket Title";
var message = "Ticket Message";

// does the user with supplied email address exist?
var user = InstantASP.InstantKB.Business.UserRepository.SelectUser(emailAddress);
// we didn't find a user with the supplied email
if (user.UserID == 0)
{
// new users default primary role
var primaryRoleId = 0;
var role = InstantASP.Common.Business.Roles.SelectRole(InstantASP.Common.Enumerations.EnumRequiredRoles.Member);
if (role != null)
primaryRoleId = role.RoleID;

// add the user
var newUserId = Business.UserRepository.InsertUpdateUser(
new Components.User()
{
Username = emailAddress,
PhotoImage = "",
PrimaryRoleID = primaryRoleId,
Password = ""
});
if (newUserId > 0)
user = InstantASP.InstantKB.Business.UserRepository.SelectUser(newUserId);
}

// ensure we have a user we can associate with the new ticket
if (user?.UserID > 9)
{

// get need to identify which tab to add the ticket to
var tabId = 0;
var tr = new InstantASP.InstantKB.Business.TabsRepository();
var tabs = tr.SelectTabs();
if (tabs != null)
{
// filter all ticket tabs
  tabs = tabs.FilterByTabContentType(InstantASP.InstantKB.Enumerations.EnumTabContentType.SubmitTicket);
foreach (var tab in tabs)
{
// not a custom ticket tab
if (!tab.IsCustomTab)
tabId = tab.TabID;
}
}

// we already need to place tickets within a tab
if (tabId > 0)
{
// add ticket     
var t = new InstantASP.InstantKB.Business.TicketRepository(user);
var ticket = t.InsertUpdateTicket(new Components.Ticket()
{
TicketTabID = tabId,
TicketCreatedUserID = user.UserID,
TicketModifiedUserID = user.UserID,
TicketTitle = subject,
TicketText = message,
TicketCreatedDate = System.DateTime.Now,
TicketModifiedDate = System.DateTime.Now,
TicketChannel = InstantKB.Enumerations.EnumChannel.Web
}, true, true, true);

if (ticket != null)
{
// show our new ticket ID
Response.Write(ticket.TicketID.ToString());

}
}
else
{
throw new Exception("No tab could be found to add the ticket to");
}

}

Updating An Existing Ticket

The following code example demonstrates how to update an existing ticket...

var t = new InstantASP.InstantKB.Business.TicketRepository();
var ticket = t.SelectTicket(1234);

// did we find a ticket with id 1234
if (ticket != null)
{
// update assigned user ID for ticket id 1234
ticket.TicketAssignedUserID = 5;
// update ticket priority
ticket.TicketPriorityID = 4;

// update the ticket
t.InsertUpdateTicket(ticket);

}

Note: In this example it's up to you to ensure you enforce the security checks to ensure the user has permission to update the supplied ticket ID. The following example shows how to get the current authenticated user and only allow updating of tickets if the user is a member of the support agent or administrator roles.

// get current authenticated user 
var authenticatedUser = InstantASP.InstantKB.Providers.UserIdentity.UserIdentityProvider.Instance().GetAuthenticatedUser();
// is the user making the request an administrator or support agent?
if (authenticatedUser.IsModerator || authenticatedUser.IsAdministrator)
{

var t = new InstantASP.InstantKB.Business.TicketRepository(authenticatedUser);
var ticket = t.SelectTicket(1234);

// did we find a ticket with id 1234
if (ticket != null)
{
// update assigned user ID for ticket id 1234
ticket.TicketAssignedUserID = 5;
// update ticket priority
ticket.TicketPriorityID = 4;

// update the ticket
t.InsertUpdateTicket(ticket);

}

}

That's It!

We hope this example helps. Should you require any specific code example of course please don't hesitate to open a support request and we'll happily assist as quickly as possible.