Wednesday, August 24, 2016

SharePoint check if user is member of a community programmatically C#

This post explains how to check if current user is member of a SharePoint community using C #.

The first method that i know is by using 

SPGroup members = web.AssociatedMemberGroup;

and browse users list. The problem I encountered is that the member list always contains the user even if he leaves the community.After some research I understand that SharePoint does not remove the user from the list of members when the user leaves but changes the property MemberStatusInt from 1 to 2.

Below how to check whether a user is always community member

SPWeb web = siteColl.RootWeb;

//Get Community Root

foreach (SPList list in web.Lists)
{

// get members list 
    if (list.Title.Equals("Community Members"))
{

//CAML query to check if current user is following community
     SPQuery query = new SPQuery();
     query.Query = "<Where><And><Eq><FieldRef Name= \"Member\"/><Value Type=\"Integer\"><UserID />          </Value></Eq><Eq><FieldRef Name= \"MemberStatusInt\"/><Value Type=\"Integer\">1</Value></Eq> </And></Where>";
SPListItemCollection colection = list.GetItems(query);

// if colection.Count==0 => user not following comunity 

}

}

I hope that this post helps you and sorry for my English.


by Saifeddine Sibouih via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment