Badges


List of examples:

Creating a badge




// Creates a new badge object
BadgeInfo newBadge = new BadgeInfo();

// Sets the properties for the new badge
newBadge.BadgeDisplayName = "New badge";
newBadge.BadgeName = "NewBadge";
newBadge.BadgeTopLimit = 50;
newBadge.BadgeImageURL = "Objects/CMS_Badge/Default/siteadmin.gif";
newBadge.BadgeIsAutomatic = true;

// Saves the badge to the database
BadgeInfoProvider.SetBadgeInfo(newBadge);


> Back to list of examples

Updating a badge




// Gets the badge
BadgeInfo updateBadge = BadgeInfoProvider.GetBadgeInfo("NewBadge");
if (updateBadge != null)
{
    // Updates the badge properties
    updateBadge.BadgeDisplayName = updateBadge.BadgeDisplayName.ToLowerCSafe();

    // Saves the badge changes to the database
    BadgeInfoProvider.SetBadgeInfo(updateBadge);
}


> Back to list of examples

Updating multiple badges




// Gets all badges whose code name starts with 'NewBadge'
var badges = BadgeInfoProvider.GetBadges().WhereStartsWith("BadgeName", "NewBadge");

// Loops through individual badges
foreach (BadgeInfo modifyBadge in badges)
{
    // Updates the badge properties
    modifyBadge.BadgeDisplayName = modifyBadge.BadgeDisplayName.ToUpper();

    // Saves the changed badge to the database
    BadgeInfoProvider.SetBadgeInfo(modifyBadge);
}


> Back to list of examples

Assigning a badge to a user




// Gets the user 
UserInfo user = UserInfoProvider.GetUserInfo("Andy");

// Gets the badge
BadgeInfo badge = BadgeInfoProvider.GetBadgeInfo("NewBadge");

if ((user != null) && (badge != null))
{
    // Assigns the badge to the user
    user.UserSettings.UserBadgeID = badge.BadgeID;

    // Saves the updated user object to the database
    UserInfoProvider.SetUserInfo(user);
}


> Back to list of examples

Updating activity points for users




// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");

if (user != null)
{
    // Adds activity points for the user
    // The number of points depends on the type of the activity (blog comment post in this case) and the number of points assigned to the activity in the settings
    BadgeInfoProvider.UpdateActivityPointsToUser(ActivityPointsEnum.BlogCommentPost, user.UserID, SiteContext.CurrentSiteName, true);
}


> Back to list of examples

Removing a badge from a user




// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");

if (user != null)
{
    //Removes the user's current badge
    user.UserSettings.UserBadgeID = 0;

    // Saves the updated user to the database
    UserInfoProvider.SetUserInfo(user);
}


> Back to list of examples

Deleting a badge




// Gets the badge
BadgeInfo deleteBadge = BadgeInfoProvider.GetBadgeInfo("NewBadge");

if (deleteBadge != null)
{
    // Deletes the badge
    BadgeInfoProvider.DeleteBadgeInfo(deleteBadge);
}


> Back to list of examples