Top Posters Role Management Community Server Job

 

"Rocket City Web" of the Community Server forums asked if it is possible to limit top posting members to a certain forum. At first, I had though the member points system would be perfect for this. However, Member Points is an add-on and not available for the "Personal Edition" of Community Server. Dave Burke mentioned that you should just add top posting users to a specific Role and grant that Role access. I thought this would be perfect for a CSJob!

This was created in all of 15 minutes and it is all packaged up nicely for you here.

Here's the code:

public class AddPostersToRoleJob : IJob
{
private int minPosts = -1;
private string roleName = String.Empty;

public AddPostersToRoleJob() { }

public void Execute(XmlNode node)
{
try { minPosts = int.Parse(node.Attributes["minPosts"].Value); }
catch { }

try { roleName = node.Attributes["roleName"].Value; }
catch { }

// It is required that the job be setup properly
if (minPosts <= 0 || String.IsNullOrEmpty(roleName))
return;

SiteSettingsManager.IterateSiteSettings(new SiteSettingsListIterator(AddPosters));
}

private void AddPosters(int settingsid)
{
CSContext.Create(settingsid);

// get the users.
UserQuery q = new UserQuery();
q.PageSize = int.MaxValue;
UserSet users = Users.GetUsers(q, false);

// If there are no users, get out.
if (!users.HasResults)
return;

// Loop Through the users
foreach (User u in users.Users)
{
// if the posts are above the specified min
if (u.TotalPosts >= minPosts)
{
// and they are not already in the role
if (!u.IsInRoles(new string[] { roleName }))
{
// add them to the role
Roles.AddUserToRole(u.Username, roleName);
}
}
}

}
}

The routine is fairly straight forward. When the job executes it gets the specified minimum posts and role name from the communityserver.config file. The one part that could have some discussion is the SiteSettingsManager.IterateSiteSetting method. I like to programatically loop through all sites in my CS installation, but you could hard code the default SettingsID of 1000 if performance is an issue.

Let me know how it works out for you if you use it!


Published Wednesday, January 10, 2007 10:46 AM

Comments

# re: Top Posters Role Management Community Server Job

Wednesday, January 10, 2007 11:10 AM by Gary McPherson

Nice!

# re: Top Posters Role Management Community Server Job

Thursday, January 11, 2007 9:40 AM by Dana

You rock!!  Thanks very much.

# CS Byte for January 11, 2007

Thursday, January 11, 2007 10:06 AM by Dave Burke

blog bits Ben Tiedt, the dev lead on Community Server 3.0's new theming engine, codename Chameleon, is

# Job that limits forum posting to the top posting members of a given forum

Thursday, January 11, 2007 9:57 PM by Daily News List Blog

Sean Kearney (CarKnee) with a sweet Community Server Job that limits forum posting to the top posting

# Blocking new users by email address in Community Server

Monday, January 22, 2007 11:27 AM by Sean Kearney's (CarKnee) Blog

A couple of members on the Community Server forums wanted a way to block new members based on known SPAM

# Job that limits forum posting to the top posting members of a given forum

Monday, March 12, 2007 12:19 AM by Community Server Bits

Sean Kearney (CarKnee) with a sweet Community Server Job that limits forum posting to the top posting

Powered by Community Server (Non-Commercial Edition), by Telligent Systems