Search This Blog

Friday, January 31, 2020

Get user last logon date, Group,permission -SharePoint


using Microsoft.SharePoint.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Threading.Tasks;

namespace SampleConsoleApp
{


    class Program
    {


        public static void Main(string[] args)
        {
          
            DataTable dtAllUsr = geSiteUserInfoListDetails("http://sp2016:1234/sites/test");
            Console.ReadLine();
        }
        public static DateTime? GetLastLogin(string userName)
        {
            try
            {
                //domain.com eg: sp2016\\jaisona -> LDAP://sp2016.com
                DirectoryEntry de = new DirectoryEntry("LDAP://sp2016.com");
                DirectorySearcher ds = new DirectorySearcher(de);
                ds.Filter = string.Format("(&(objectCategory=user)(objectClass=user)({0}={1}))", "samAccountName", userName);
                ds.PropertiesToLoad.AddRange(new string[] { "samAccountName", "lastLogon" });
                SearchResult sr = ds.FindOne();
                long lastLogon = (long)sr.Properties["lastLogon"][0];
                DateTime dtLastLogon = DateTime.FromFileTime(lastLogon);
                return dtLastLogon;
            }
            catch { return null; }
        }
        public static DataTable geSiteUserInfoListDetails(string siteUrl)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Sno", typeof(string));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("FullLoginName", typeof(string));
            dt.Columns.Add("EMail", typeof(string));
            dt.Columns.Add("UserPermission", typeof(string));
            dt.Columns.Add("UserGroup", typeof(string));
            dt.Columns.Add("GroupPermission", typeof(string));
            dt.Columns.Add("LastLogon", typeof(string));
          
            DataRow dr;
            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;
            int count = 0; string usrgroups = ""; string usrgroupPermission = ""; string usrPermission = "";
            clientContext.Load(site, w => w.Title, w => w.Description, w => w.SiteUsers, w => w.SiteGroups);
            var siteUsers = site.SiteUsers;
            clientContext.ExecuteQuery();
            foreach (var user in siteUsers)
            {
                usrgroups = usrgroupPermission = usrPermission = "";
                count++;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Checking user Permission : " + count.ToString() + " User :" + user.Title);
                //RoleAssignmentCollection usrroleAssignments = site.RoleAssignments;
                //clientContext.Load(clientContext.Web, w => w.RoleAssignments.Where(ra => ra.Member.LoginName == user.LoginName));
                //clientContext.ExecuteQuery();

                //foreach (var ra in usrroleAssignments)
                //{
                //    Console.WriteLine("Checking user permission: " + user.Title);
                //    clientContext.Load(ra.Member);
                //    clientContext.Load(ra.RoleDefinitionBindings);
                //    clientContext.ExecuteQuery();
                //    foreach (var definition in ra.RoleDefinitionBindings)
                //    {
                //        clientContext.Load(definition);
                //        clientContext.ExecuteQuery();
                //        usrPermission += user.LoginName + ": " + definition.Name;
                //    }
                //}
                dr = dt.NewRow();
                dr["Sno"] = count.ToString();
                dr["Title"] = user.Title;
                dr["FullLoginName"] = user.LoginName;
                dr["EMail"] = user.Email;
                dr["UserPermission"] = usrPermission;
                GroupCollection grpCol = user.Groups;
                clientContext.Load(grpCol);
                clientContext.ExecuteQuery();
                if (grpCol.Count > 0)
                {
                    foreach (Group grp in grpCol)
                    {
                        RoleAssignmentCollection roleAssignments = site.RoleAssignments;
                        clientContext.Load(clientContext.Web, w => w.RoleAssignments.Where(ra => ra.Member.LoginName == grp.LoginName));
                        clientContext.ExecuteQuery();
                        foreach (var ra in roleAssignments)
                        {

                            clientContext.Load(ra.Member);
                            clientContext.Load(ra.RoleDefinitionBindings);
                            clientContext.ExecuteQuery();
                            foreach (var definition in ra.RoleDefinitionBindings)
                            {
                                clientContext.Load(definition);
                                clientContext.ExecuteQuery();
                                usrgroupPermission += grp.LoginName + ": " + definition.Name;
                            }
                        }
                        usrgroups += ";" + grp.Title;
                    }
                    dr["UserGroup"] = usrgroups;
                    dr["GroupPermission"] = usrgroupPermission;
                }
                if (user.LoginName.Contains("\\") && user.LoginName.Contains("sp2016"))
                {
                    dr["LastLogon"] = Convert.ToString(GetLastLogin(user.LoginName.Split('\\')[1]));
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }

}


No comments:

Post a Comment