Tuesday, May 19, 2015

What's happening webpart is showing incorrect value in community site SharePoint 2013

Introduction

Most of us came across about this issue, which is count of Members, Discussion and Replies. Normally, These values are tracking in properties. In Some cases, It will not update properly. Let see how to update this.


Community site properties 

Open your community site in SharePoint Designer. Click Site options in top ribbon



Then we can able to see the parameters (properties) of site.


We can find the Community_MemberCount there, which is used to keep member's count of community.

Update the properties using Event receiver

We need to update the property when the user has leave from Community Member list. So, I have written the code in Community Member list event receiver to update the property. Members are not deleted from the list. They have changed the flag when user enforced to delete. So we need to code it when ItemUpdated


public override void ItemUpdated(SPItemEventProperties properties)

        {

            var web = properties.Web;

            var currentAllowUnsafeUpdatesVal = web.AllowUnsafeUpdates;
            try
            {
               var membersCountQueryCAML = "<Where><Eq><FieldRef Name='MemberStatusInt' /><Value Type='Integer'>1</Value></Eq></Where>";
            var membersCountQuery = new SPQuery();
            membersCountQuery.Query = membersCountQueryCAML;
            
                var membersCount = membersList.GetItems(membersCountQuery).Count;
                web.AllowUnsafeUpdates = true;
                if (web.AllProperties.ContainsKey("Community_MembersCount"))
                {
                     web.AllProperties["Community_MembersCount"] = membersCount.ToString();
                    web.Update();
                 }
                base.ItemUpdated(properties);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                web.AllowUnsafeUpdates = currentAllowUnsafeUpdatesVal;
            }
        }

Then, deploy / upgrade your solution in your community site. Now Whats happening webpart will give the correct member's count.

1 comment: