Work with forums & roles via the .NET API

Roles (also referred to as member groups within InstantForum) are used to control which forums are visible to users. Only forums associated with one or more of the current users roles will be displayed to the user. Forums that are no associated with any of the current users roles will never be displayed.

In this article we'll show how to programmatically associate roles with forums within InstantForum using the InstantForum .NET API.

Adding Roles To New Forums

The InstantForum forum object accepts a Roles property which allows you to programmatically associate roles with specific forums.

For example say we wanted to add a forum and only allow users who belong to roles 3, 4 & 5 to view our forum we would do the following...

// build our roles
var roles = new Collections.ForumRolesCollection();
roles.Add(new Components.ForumRole(3));
roles.Add(new Components.ForumRole(4));
roles.Add(new Components.ForumRole(5));

// build a new category
var newCategory = new Components.Forum()
Name = "My Restricted Category",
IsCategory = true,
Expanded = true,
Roles = roles

// add category with roles
var newCategoryId = Business.Forums.InsertUpdateForum(newCategory);

// lets add a new child forum to our category
if (newCategoryId > 0)

var newForum = new Components.Forum()
Name = "My Restricted Forum",
ParentID = newCategoryId,
Expanded = true,
Roles = roles

// add child forum to category with same roles
var newForumId = Business.Forums.InsertUpdateForum(newForum);


Adding Roles to Existing Forums

The code example below shows how to update roles for an existing forum...

var existingForumId = 4;
var f = new InstantASP.InstantForum.Business.Forums();
var existingForum = forums.SelectForum(forumId);

if (existingForum != null)
existingForum.Roles = roles;

Deleting all Roles Associated with a forum

The following example shows how to delete all roles associated with the forum with id 123.

var forumId = 123;

Get all roles for a specific forum

The code below shows how to iterate over all roles associated with a specific forum.

// get all roles for a specific forum
var forums = new InstantASP.InstantForum.Business.Forums();
var forum = forums.SelectForum(forumId);
if (forum != null)
foreach (var role in forum.Roles)

Get all forums associated with the current users roles

To ensure you return only the forums associated with the current authenticated users roles you should ensure you pass in the current authenticated user object into the InstantASP.InstantForum.Business.Forums constructor.

For example to ensure only the forum associated with the current authenticated users roles are returned we would use...

// get authenticated user
var identityProvider = InstantASP.InstantForum.Providers.UserIdentity.UserIdentityProvider.Instance();
var user = identityProvider.GetAuthenticatedUser();

// notice user object is passed into constructor
var f = new InstantASP.InstantForum.Business.Forums(user);
var forums = f.SelectForums();
if (forums != null)
foreach (var forum in forums)

Get all forums regardless of roles

To return all forums regardless of user role associations simply ensure you don't provide a user object within the InstantASP.InstantForum.Business.Forums constructor as shown below..

// notte the empty constructor
var f = new InstantASP.InstantForum.Business.Forums();
var forums = f.SelectForums();

That's It!

We hope these examples help you work with forums and roles through the InstantForum .NET API. If we've missed anything or you'd like to see any specific examples of course please don't hesitate to submit a support request or contact us.