locked
Checking whether a user is active or not in SharePoint site using object model RRS feed

  • Question

  • Hi All,

    Is it possible to check the users (All the users in the SharePoint groups) is active or not using object model ?
    I want to retrieve all the users in a SharePoint site and check whether the user is active or not.

    Any help would be greatly appreciated.

    Cheers! Maruthu | http://sharepoint-works.blogspot.com

    Thursday, April 26, 2012 11:23 AM

All replies

  • Hi Maruthu,

    Can you tell me what’s the meaning of active user, do you mean that the user is enabled in Active Directory? If so, you can write following code to enumerate each user in SharePoint group and check whether the user is enabled.

    private DirectoryEntry FindUser(string id, DirectoryEntry domain)
            {
                if (!domain.Path.StartsWith("WinNT"))
                {
                    DirectorySearcher search = new DirectorySearcher(domain);
                    search.Filter =
                      string.Format("(&(objectClass=person)(objectSid={0}))", id);
                    SearchResult result = search.FindOne();
                    if (result != null)
                        return result.GetDirectoryEntry();
                }
                else
                {
                    foreach (DirectoryEntry de in domain.Children)
                    {
                        SecurityIdentifier si = new SecurityIdentifier(
                            (byte[])de.Properties["objectSid"][0], 0);
                        if (string.Compare(si.Value, id, true) == 0)
                            return de;
                    }
                }
                return null;
            }
    const long ADS_UF_ACCOUNTDISABLE = 0x0002;
    public void CheckAllUser()
            {
                SPSite site=new SPSite("http://lambda2007");
                using (SPWeb oWebsiteRoot = site.OpenWeb() )
                {
                    //Create a new Directory Entry Object
                    DirectoryEntry entry=new DirectoryEntry("LDAP://lambda.com");
    	//All Group Collection
                    SPGroupCollection collGroupsWebsite = oWebsiteRoot.SiteGroups;
                    foreach (SPGroup userGroup in collGroupsWebsite)
                    {
                        foreach (SPUser user in userGroup.Users)
                        {
                            try
                            {
    		//Get User From AD by Sid
                                DirectoryEntry resultUser = FindUser(user.Sid, entry);
                                //Check the disable flag
                                bool IsEnable = (Convert.ToInt32(resultUser.Properties["userAccountControl"].Value) & ADS_UF_ACCOUNTDISABLE) == 0;
                                Debug.WriteLine(user.Name + IsEnable);
                            }
                            catch (Exception ex)
                            {
                                //Handle the Exception
                            }
                        }
                       
                    }
                }
            }

    Please refer to these link for more information:

    http://www.codeproject.com/Articles/34299/Update-SharePoint-UserInfo-List-with-More-Active-D

    http://www.codeguru.com/csharp/csharp/cs_network/directoryservices/article.php/c6021/Accessing-Directory-Services.htm

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms696026.aspx

    Hope that helps.

    Thanks,


    Lambda Zhao

    TechNet Community Support


    Thursday, May 3, 2012 2:43 AM
  • Hi Lambda Zhao,

    Thank you very much for the reply!

    Here I have mentioned that the active user is one who is using the SharePoint site constantly. I want to remove the Inactive users which means:

    1. I have to delete the users who is removed from the active directory (as per the solution given by you, we can retrieve the active and non-active users - users enabled in the SP site)

    2. I also want to remove the users who are not using the sharepoint application for long time. I have tried exceuting the following SQL query against the SP content db which lists the active and Inactive users by a flag.

    Select tp_IsActive, tp_Title as UserName,tp_Login as NetworkID,tp_Email as EmailID from UserInfo

    In the above query, tp_IsActive is used to determine whether the user is active or not.

    Any suggestions would be greatly appreciated!


    Cheers! Maruthu | http://sharepoint-works.blogspot.com

    Thursday, May 3, 2012 6:59 AM
  • Is there any other simple way than this?

    Thanks, Sachin Jagtap

    Thursday, March 6, 2014 7:30 PM