locked
How to check if user exists in a particular sharepoint group or not programatically RRS feed

  • Question

  • Hi,

    How to check if user exists in a particular sharepoint group or not programatically?

    Manoj


    Knowledge is power.

    Tuesday, October 30, 2012 6:57 AM

Answers

  • Hi Manoj,

    Please try this console application:

    using System;
    using System.Linq;
    using Microsoft.SharePoint;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string userName = "PERSEUS\\dmitry.kaloshin";
                string groupName = "Home Members";
                using (SPSite spSite = new SPSite("http://perseus"))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPUser user = spWeb.EnsureUser(userName);
                        if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName)))
                        {
                            Console.WriteLine("User " + userName + " is a member of group " + groupName);
                        }
                        else
                        {
                            Console.WriteLine("User " + userName + " is NOT a member of group " + groupName);
                        }
                    }
                }
            }
        }
    }


    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Tuesday, October 30, 2012 1:24 PM
  • SPUser user = SPContext.Current.Web.CurrentUser;
    SPGroupCollection groupColl = user.Groups;

    This will give you a collection of groups where the current user is a member.

    Now call groupColl.Item("GroupName") and check if it eturns the group. If yes, the user is member of the group else not.


    Thanks, Bhadr :) Mark as Answer if it solves your problem. Vote As Helpful if the post is helpful for you.

    Tuesday, October 30, 2012 9:05 AM

All replies

  • SPUser user = SPContext.Current.Web.CurrentUser;
    SPGroupCollection groupColl = user.Groups;

    This will give you a collection of groups where the current user is a member.

    Now call groupColl.Item("GroupName") and check if it eturns the group. If yes, the user is member of the group else not.


    Thanks, Bhadr :) Mark as Answer if it solves your problem. Vote As Helpful if the post is helpful for you.

    Tuesday, October 30, 2012 9:05 AM
  • Hi Manoj,

    Please try this console application:

    using System;
    using System.Linq;
    using Microsoft.SharePoint;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string userName = "PERSEUS\\dmitry.kaloshin";
                string groupName = "Home Members";
                using (SPSite spSite = new SPSite("http://perseus"))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPUser user = spWeb.EnsureUser(userName);
                        if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName)))
                        {
                            Console.WriteLine("User " + userName + " is a member of group " + groupName);
                        }
                        else
                        {
                            Console.WriteLine("User " + userName + " is NOT a member of group " + groupName);
                        }
                    }
                }
            }
        }
    }


    Dmitry

    Twitter Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog | Мой Блог

    Tuesday, October 30, 2012 1:24 PM