Managing badges

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The following sample code shows how you can get a badge as a BadgeInfo object:

 

[C#]

 

using CMS.SiteProvider;

 

...

 

        // Get Badge object by ID

        BadgeInfo bi = BadgeInfoProvider.GetBadgeInfo(1);

 

        // Get Badge object by badge code name

        BadgeInfo bi2 = BadgeInfoProvider.GetBadgeInfo("Admin");

 

The following sample code shows how you can get a DataSet containing badges from the system:

 

[C#]

 

using System.Data;

using CMS.SiteProvider;

 

...

 

        string where = "BadgeTopLimit > 50";

        string orderby = "BadgeTopLimit DESC";

        int topN = 10;

        string selectedColumns = "BadgeID, BadgeTopLimit, BadgeImageURL";

 

        // Get dataset of badges according to the given parameters

        DataSet ds = BadgeInfoProvider.GetBadges(where, orderby);

 

        DataSet ds2 = BadgeInfoProvider.GetBadges(where, orderby, topN, selectedColumns);

 

The following sample code shows how you can create a new badge:

 

[C#]

 

using CMS.SiteProvider;

 

...

 

      // Create new badge object

      BadgeInfo bi = new BadgeInfo();

 

      // Set properties

       bi.BadgeDisplayName = "Test badge";

       bi.BadgeName = "TestBadge";

       bi.BadgeTopLimit = 50;

       bi.BadgeImageURL = "~/App_Themes/Default/Images/Objects/CMS_Badge/Default/siteadmin.gif";

       bi.BadgeIsAutomatic = true;

 

      // Save to database

      BadgeInfoProvider.SetBadgeInfo(bi);

 

The following sample code shows how you can modify the display name of an existing badge:

 

[C#]

 

using CMS.SiteProvider;

 

...

 

        // Get badge object

        BadgeInfo bi = BadgeInfoProvider.GetBadgeInfo("TestBadge");

 

        // If badge exists

        if (bi != null)

        {

            // Update property

            bi.BadgeDisplayName += " updated";

 

            // Save changed object to database

            BadgeInfoProvider.SetBadgeInfo(bi);

        }

 

The following sample code shows how you can delete a badge from the system:

 

[C#]

 

using CMS.SiteProvider;

 

...

 

        // Get badge object

        BadgeInfo bi = BadgeInfoProvider.GetBadgeInfo("TestBadge");

 

        // If badge exists

        if (bi != null)

        {

            // Delete from database

            BadgeInfoProvider.DeleteBadgeInfo(bi);

        }

 

The following sample code shows how you can update the activity points of a user:

 

[C#]

 

using CMS.SiteProvider;

using CMS.CMSHelper;

 

...

 

        // Get user

       UserInfo ui = UserInfoProvider.GetUserInfo("gold");

 

       // If user exists

       if (ui != null)

       {

           // Update activity points of user

          BadgeInfoProvider.UpdateActivityPointsToUser(ActivityPointsEnum.BlogCommentPost, ui.UserID, CMSContext.CurrentSiteName, true);

       }

 

The following sample code shows how you can assign a badge to a user:

 

[C#]

 

using CMS.SiteProvider;

 

...

 

       // Get user object

       UserInfo ui = UserInfoProvider.GetUserInfo("Username");

 

       // Get badge object

       BadgeInfo bi = BadgeInfoProvider.GetBadgeInfo("Member");

 

       // If both exists

       if ((ui != null) && (ui != null))

       {

           // Add badge to user settings

           ui.UserSettings.UserBadgeID = bi.BadgeID;

 

           // Update user object in database

           UserInfoProvider.SetUserInfo(ui);

 

       }

 

Page url: http://devnet.kentico.com/docs/5_5r2/devguide/index.html?api_membership_managing_badges.htm