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

Thank you for your feedback!


Searching topics using the InstantForum .NET API

New

 The code example below demonstrates how to use the InstantForum .NET API to query topics.

Getting Started

You'll first need to reference the InstantForum .NET API from your Visual Studio project or solution. Further information can be found here. Once you've included the InstantForum assemblies you'll need to bring in a few namespaces to use the code examples below.

Add the necessary using or import statements...

using InstantASP.Common.Enumerations;
using InstantASP.InstantForum;

Keyword Search

Here we return all posts with the keyword "Test" in either the post title or post text...

// we'll return a pagable set of results so here we set
// our current page index and page size
var pageIndex = 1;
var pageSize = 10;

// the SearchEventArgs object offers a number of properties 
// you can use to further filter or restrict searches
var args = new Components.SearchEventArgs()
{
Keywords = "Testing"
};

// get current user object
var currentUser = HttpContext.ForumContext.Current.CurrentUser;

// to restrict results based on the roles associated with the current
// user we pass the currentUser object into the construcotr of our topics business class
var t = new Business.Topics(currentUser);

// query our topics
var topics = t.SelectTopics(
pageIndex,
pageSize,
args);

// do we have results?
if (topics != null)
{
// let's show them
foreach (var topic in topics)
{
Response.Write(topic.Title + "<br/>");
}
}

Sorting Results

You can optionally sort results via the SearchEventArgs object as shown below...

// we'll return a pagable set of results so here we set
// our current page index and page size
var pageIndex = 1;
var pageSize = 10;

// the SearchEventArgs object offers a number of properties 
// you can use to further filter or restrict searches
var args = new Components.SearchEventArgs()
{
Keywords = "Testing",
SortBy = "DateStamp", // the name of the field to sort by within the InstantForum_Topics table
SortOrder = EnumSortOrder.DESC
};

// get current user object
var currentUser = HttpContext.ForumContext.Current.CurrentUser;

// to restrict results based on the roles associated with the current
// user we pass the currentUser object into the construcotr of our topics business class
var t = new Business.Topics(currentUser);

// query our topics
var topics = t.SelectTopics(
pageIndex,
pageSize,
args);

// do we have results?
if (topics != null)
{
// let's show them
foreach (var topic in topics)
{
Response.Write(topic.Title + "<br/>");
}
}

The SearchEventArgs object supports the following properties as explained below...

ForumID (int)

Used to return topics within a specific forum.

ForumIDs (int array)

Used to return topics within specific forums. For example...

args.ForumIDs = new int[] {1, 2, 3, 4}

UserID (int)

Used to return topics from a specific user.

ParentID (int) and ParentIDOperator (enum)

These properties can be used to control if only topics or only replies are retrurned. For example to return only topics (with no replies) you would use...

var args = new Components.SearchEventArgs()
{
Keywords = "Testing",
SortBy = "DateStamp", // the name of the field to sort by within the InstantForum_Topics table
SortOrder = EnumSortOrder.DESC,
ParentID = 0,
ParentIDOperator = EnumDataWith.Exact
};

Whilst to return only replies with no topics you would use...

var args = new Components.SearchEventArgs()
{
Keywords = "Testing",
SortBy = "DateStamp", // the name of the field to sort by within the InstantForum_Topics table
SortOrder = EnumSortOrder.DESC,
ParentID = 1,
ParentIDOperator = EnumDataWith.AtLeast
};

There are several other less important properties you can use with the SearchEventArgs object to further filter returned topics. All work in a similar fashion to those explained above.

That's It!

We hope this article helps you get started using the InstantForum .NET API to query and display topics within your applications. As always if we can assist further please don't hesitate to submit a support request.‚Äč