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

Thank you for your feedback!


Searching articles via the InstantKB .NET API

 The following article details how to perform a search within InstantKB using the .NET API. All code examples are provided in C# however you can of course reference the InstantKB assemblies from your C# or VB.NET projects.

NOTE: Before attempting to use the following code examples you should ensure you've referenced the InstantKB assemblies.

Import the InstantKB namespace

Within the top of your C# code add the following using statements...

using InstantASP.InstantKB;
using InstantASP.InstantKB.Enumerations;

Basic Search With Role Based Security

The following code example shows how to search for articles that contains the keywords "Test Search"...

// get current authenticated user to pass into the ArticlesRepository 
// this ensures only articles we are allowed to view through role based security are returned
var identityProvider = Providers.UserIdentity.UserIdentityProvider.Instance();
var user = identityProvider.GetAuthenticatedUser();

// init articles repository passing in current authenticated user
var articlesRepository = new Business.ArticlesRepository(user);

// get page 1 with 20 results per page matching the supplied keywords
var pageIndex = 1;
var pageSize = 20;
var articles = articlesRepository.SelectArticles(pageIndex, pageSize, new Components.SearchEventArgs()
{
Keywords = "Test Search"
});

// check to ensure we have results
if (articles != null)
{
// loop through our results or bind to a control
foreach (var article in articles)
{
Response.Write(article.ArticleTitle);
Response.Write("<br/>";
}
}

The key method here is our SelectArticles method within the ArticlesRepository class. This methods accept 3 arguments. The first 2 arguments pageIndex and pageSize are simply used for pagination purposes. The third argument is our InstantASP.InstantKB.Components.SearchEventArgs object. This object represents the various search parameters you can pass into the SelectArticles method to filter the returned results.

NOTE: InstantKB has a ArticleRepository and ArticlesRepository class both within the InstantASP.InstantKB.Business namespace. These should not be confused. The ArticlesRepository class contains methods for working with collections of articles whilst the ArticleRepository class contains methods for working with a single article.

To ignore role based security checks and simply return all articles regardless of the roles associated with the article or it's containing tab or category you can simply construct a new ArticlesRepository  with an empty constructor - i.e. don't pass in a InstantASP.InstantKB.Components.User object.

Sorting Results

To sort returned results you can use the SortBy and SortOrder properties available on the SearchEventArgs object as shown below...

var articles = articlesRepository.SelectArticles(pageIndex, pageSize, new Components.SearchEventArgs()
{
Keywords = "Test Search"
SortBy = "ArticleModifiedDate",
SortOrder = EnumSortOrder.DESC
});

The SortBy property can be any valid column name from the InstantKB_Articles table.

Other Examples

 The SearchEventArgs object supports a number of additional properties which can be used to further filter search results. For example if you wanted to return all articles assigned to a paticular user you would use the following code...

var articles = articlesRepository.SelectArticles(pageIndex, pageSize, new Components.SearchEventArgs()
{
AssignedToUserID = 123,
SortBy = "ArticleModifiedDate",
SortOrder = EnumSortOrder.DESC
});

Or all articles within a specific category...

var articles = articlesRepository.SelectArticles(pageIndex, pageSize, new Components.SearchEventArgs()
{
CategoryID = 123,
SortBy = "ArticleCreatedDate",
SortOrder = EnumSortOrder.DESC
});

Or several categories

var articles = articlesRepository.SelectArticles(pageIndex, pageSize, new Components.SearchEventArgs()
{
CategoryIDs = new string[] {"123", "456", "567"},
SortBy = "ArticleModifiedDate",
SortOrder = EnumSortOrder.DESC
});

You can of course combine the various properties that allow you to filter results. or example the following code shows a more complex search query...

var articles = articlesRepository.SelectArticles(pageIndex, pageSize, new Components.SearchEventArgs()
{
TabID = 1,
CreatedByUserID = 123,
PriorityID = 4,
TypeID = 123,
Keywords = "test",
AccessType = EnumAccessType.Private,
SortBy = "ArticleModifiedDate",
SortOrder = EnumSortOrder.DESC
});

That's It!

We hope these examples help you get started with the InstantKB .NET API. If we can provide any further examples to assist with your specific implementation of course please don't hesitate to submit a new support request.