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

Thank you for your feedback!


Adding forums using the InstantForum .NET API

The following code examples demonstrates how to programmatically create categories & forums using the InstantForum .NET API from your own C# code.

Creating New Forums

The below code examples shows the minimum rpoerpties you need to provide on our forum object before you can add a forum. You'll notice we simply constraint a forum object and pass this into the static InsertUpdateForum method within the InstantASP.InstantForum.Business.Forums class.

using InstantASP.InstantForum;

private void CreateSomeForums()
{

// create first homepage category to hold forums
int cat_1_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Category 1",
IsCategory = true,
Expanded = true
});

if (cat_1_id > 0)
{
// add a forum into the category
int forum_1_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Forum In Category 1",
ParentID = cat_1_id
});

if (forum_1_id > 0)
{
// add a child forum into our first forum
int forum_1_child_1_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Child Forum In Category 1 / Forum 1",
ParentID = forum_1_id
});
}

// add a forum into the category
int forum_2_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Forum In Category 1",
ParentID = cat_1_id
});

if (forum_2_id > 0)
{
// add a child forum into our second forum
int forum_2_child_1_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Child Forum In Category 1 / Forum 2",
ParentID = forum_2_id
});
}

}

// create a second homepage category
int cat_2_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Category 2",
IsCategory = true,
Expanded = true
});

if (cat_2_id > 0)
{
// add a forum into the category
int forum_1_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Forum In Category 2",
ParentID = cat_2_id
});

if (forum_1_id > 0)
{
// add a child forum to the first forum
int forum_1_child_1_id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Child Forum In Category 2 / Forum 1",
ParentID = forum_1_id
});
}
}



}

The Result

After running this code you can see the forums we've added below...

Updating Existing Forums

To update an existing forum you would use code very similar to above but this time ensure you populate the ForumID property of the forum object for the forum you wish to update.

For example say we wanted to update a forum within our InstantForum_Forums table with a ForumID of 16 we would use the following code...

// get forum 16
Components.Forum existingForum = new Business.Forums().SelectForum(16);

// make sure we found an existing forum with id 16
if (existingForum != null)
{
// update forum 16 with a new title
existingForum.Name = "My Updated Title";

// pass forum 16 back into our InsertUpdateForum method
Business.Forums.InsertUpdateForum(existingForum);
}

Adding Roles

Our forum object also accepts a Roles property which allows you toprogrammatically associate roles or member groups with specific forums as you add them through code to control access.

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...

Collections.ForumRolesCollection roles = 
new Collections.ForumRolesCollection();
roles.Add(new Components.ForumRole(3));
roles.Add(new Components.ForumRole(4));
roles.Add(new Components.ForumRole(5));

// add specific roles to category
int id = Business.Forums.InsertUpdateForum(new Components.Forum()
{
Name = "My Restricted Category",
IsCategory = true,
Expanded = true,
Roles = roles
});