Sunday, April 21, 2013

How to change Community Reputation Settings from code

Community site is one of  the new SharePoint 2013 social features that provides your users with a forum like site where they can collaborate and share their knowledge.

If you are new to this feature and want to learn more about it I suggest you check out this site :
Overview of communities in SharePoint Server 2013 .

To me one of the most interesting components it offers is the ability to track your users engagement on the site and to assign them with reward points for their efforts.

When you create a community site you can change different settings that are connected with the way people are awarded with points and the way this points are presented to others.  You can access this settings from the "Community Tools" web part on the default start page of the community site or you can go to
Site Settings -> Community Administration. From here you can access Community Reputation Settings page.On this page you can change:
  • Member achievements point system - Amount of points users will receive for their actions
  • Achievement level points - Specify the number of points required for members to reach each achievement level.
  • Achievement level presentation - Specify whether achievement levels are represented as a series of boxes or as a textual title. you can also change the textual title for each level.
This is the way you can change this settings through administration but how to change them from code?
Well for this we will first have to look what makes a Community Site.
We can see that Community Site contains several list:
  • Badges - here you can define badges that you can award then to your members
  • Categories - categories for your discussions
  • Community Members - list with all your users that will be involved in your community
  • Discussions List - place where you actually save your discussions
Badges and Categories are pretty simple SharePoint lists and there isn't much special about them. For reputation system Community Members  and Discussions List hold the key information.

Community Members holds users that are part of the community but it also stores their reputation information in these fields:
  • Discussions - number of discussions user started
  • Replies - number of replies user made
  • Best replies - number of times users replies where marked as best 
  • Reputation Score - current score that user has
  • Badge - badge that user was awarded with 
So we see this is the place where users reputation status is stored, but where are the reputation settings? Well they are stored within your Discussions List, more specifically in its property bag.

And this is how you can access them from code:

 
using (SPSite site = new SPSite("http://sp2013/"))
            {
                using (SPWeb web = site.AllWebs["community"])
                {
                    SPList disscussion = web.Lists["Discussions List"];

                    //Get achievement point that are awarded to user based on his action.; they are returned as a string with values like this:
                    //Creating a new post;Replying to a post;Member's post or reply gets liked;Member's reply gets marked as 'Best Reply' 
                    //for example : 10;5;10;20
                    string achievementPoints = disscussion.RootFolder.GetProperty("AchievementPoints").ToString();

                    //Get the text that describes levels user can achieve; there are 5 levels; to access eac text justchange the text in the property name 
                    //for example : Level2Text, Level3Text etc.
                    string level1Text = disscussion.RootFolder.GetProperty("Level1Text").ToString();


                    // Get the numbers at which users reach another level; it is returned as string that look like this
                    // Level1Threshold;Level2Threshold;Level3Threshold;....
                    // for example : 100;1000;2000;5000;10000
                   string levelThresholds = disscussion.RootFolder.GetProperty("LevelThresholds").ToString();

                    //Get the property that tells us are the levels displayed as text or as image (blue squares)
                    // It returnes a string specifiying True or False
                   string displayAchievementAsImage = disscussion.RootFolder.GetProperty("DisplayAchievementAsImage").ToString();
                }
            
            }
You can also change their values with SetProperty method like this :

  using (SPSite site = new SPSite("http://sp2013/"))
            {
                using (SPWeb web = site.AllWebs["community"])
                {
                    SPList disscussion = web.Lists["Discussions List"];

                    // be sure to formt your string correctly
                    string newAchievementsPoints = "1;20;50;200";

                    disscussion.RootFolder.SetProperty("AchievementPoints", newAchievementsPoints);

                    //dont forget to update
                    disscussion.Update();
                }
            }

1 comment:

  1. This was helpful, but in addition I had to solve how can I deactivate the achievement points system? Provider Hosted App solution is:
    list.RootFolder.Properties["AchievementPointsEnabled"] = "FALSE";

    ReplyDelete