This isn't anything new, but maybe it could help someone. Sometimes your application requires to get membership of current user. In that case you could use code bellow.
//searching result
bool containsGroupName = false;
//current context
SPContext currentContext;
try
{
//Gets the current context
currentContext = SPContext.Current;
}
catch (InvalidOperationException)
{
currentContext = null;
}
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
//loop through user's groups and comparing if some group contain required name
SPGroupCollection usersGroups = currentContext.Web.CurrentUser.Groups;
foreach (SPGroup group in usersGroups)
{
if ("GroupToFind" == group.Name)
containsGroupName = true;
}
}
No comments:
Post a Comment