web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to note user last login time and date in crm?

(0) ShareShare
ReportReport
Posted on by

Last login time and date of user.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rajkumar Rajaraman Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    Viswanadh,

    Enable auditing you can access audit summary report, refer this thread:

    community.dynamics.com/.../156708

    Hope this helps.

    Regards,

    R.Rajkumar

    "Please mark my answer as verified if you found it helpful"

  • Suggested answer
    Surmeet Singh Profile Picture
    3,075 on at
    RE: How to note user last login time and date in crm?

    Hi Viswanadh,

    crmbook.powerobjects.com/.../auditing-in-microsoft-dynamics-crm

  • Community Member Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    RajKumar,

    User login in 90 days back. get the last login time and date?

    Regards,

    Viswanadh

  • Rajkumar Rajaraman Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    are you using online or onpremise?

    If it's onpremise, refer this: abedhaniyah.blogspot.in/.../crm-201315-on-prem-last-access-date.html

  • Community Member Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    I am using Online.

  • Rajkumar Rajaraman Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    User login in 90 days back, if you enabling auditing before these days you can able to view the information in audit history otherwise you cannot find it.

  • Community Member Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    How To create a new workflow named [User]  that would check the current date in user’s Last Logon Date field and automatically disable the user if older than 90 days.

  • Community Member Profile Picture
    on at
    RE: How to note user last login time and date in crm?

    Hello Viswanadh,

    You can use XRMToolBox with the User Audit Viewer plugin for that:

    https://www.xrmtoolbox.com/releases/xrmtoolbox-v1.2018.6.24/

    https://www.xrmtoolbox.com/plugins/BDK.XrmToolBox.UserAuditViewer/

    Then you will be able to export inactive users from 1/3/6/12 last months to Excel.

    4544.Untitled.png

    Hope it helps,

    William

  • Teach Me Profile Picture
    85 on at
    RE: How to note user last login time and date in crm?

    Is it just me or the User Audit Viewer plugin has been removed from XRM Toolbox? It comes up with a big red cross on the plugin list:

    https://www.xrmtoolbox.com/plugins/

  • Faisal Fiaz Profile Picture
    75 on at
    RE: How to note user last login time and date in crm?

     class Program
        {
            static IOrganizationService service;
            static void Main(string[] args)
            {
                Console.WriteLine("Started");
                Console.WriteLine(DateTime.Now.ToLocalTime());
                Program app = new Program();
                app.Run();
                Console.WriteLine(DateTime.Now.ToLocalTime());
                Console.WriteLine("Completed");
                Console.ReadKey();
            }
            public void Run()
            {

                try
                {
                    string OrgServiceUri = ConfigurationManager.AppSettings["OrgServiceUri"];
                    string UserName = ConfigurationManager.AppSettings["UserName"];
                    string Password = ConfigurationManager.AppSettings["Password"];
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    ClientCredentials credentials = new ClientCredentials();
                    credentials.UserName.UserName = UserName;
                    credentials.UserName.Password = Password;
                    Uri serviceUri = new Uri(OrgServiceUri);
                    OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                    proxy.EnableProxyTypes();
                    service = (IOrganizationService)proxy;

                    QueryExpression queryUser = new QueryExpression { EntityName = "systemuser" };
                    queryUser.Criteria.AddCondition("isdisabled", ConditionOperator.Equal, false);
                    queryUser.ColumnSet = new ColumnSet("systemuserid", "windowsliveid", "isdisabled", "fullname", "createdon", "modifiedon", "createdby", "domainname", "internalemailaddress", "caltype");
                    EntityCollection userCollection = service.RetrieveMultiple(queryUser);
                    Console.WriteLine("Total Enabled Users: {0}", userCollection.Entities.Count);
                    CreateLog("CreatedOn , ModifiedOn , CreatedBy , DomainName , Email , LicenseType , FullName , UserName , LastLogin");
                    foreach (var e in userCollection.Entities)
                    {
                        // Console.WriteLine("Processing: {0}", e["windowsliveid"]);
                        string Email = string.Empty;
                        string DomainName = string.Empty;
                        string FullName = string.Empty;
                        string userName = string.Empty;
                        DateTime Create = DateTime.MinValue;
                        string CreatedOn = string.Empty;
                        DateTime Modified = DateTime.MinValue;
                        string ModifiedOn = string.Empty;
                        EntityReference Created = null;
                        string CreatedBy = string.Empty;
                        OptionSetValue License = null;
                        string LicenseType = string.Empty;
                        if (e.Attributes.Contains("fullname"))
                        {
                            FullName = (string)e.Attributes["fullname"];
                        }
                        if (e.Attributes.Contains("windowsliveid"))
                        {
                            userName = (string)e.Attributes["windowsliveid"];
                        }
                        if (e.Attributes.Contains("createdon"))
                        {
                            Create = (DateTime)e.Attributes["createdon"];
                            CreatedOn = Create.ToString("dd/MM/yyyy");
                        }
                        if (e.Attributes.Contains("modifiedon"))
                        {
                            Modified = (DateTime)e.Attributes["modifiedon"];
                            ModifiedOn = Modified.ToString("dd/MM/yyyy");
                        }
                        if (e.Attributes.Contains("createdby"))
                        {
                            Created = (EntityReference)e.Attributes["createdby"];
                            CreatedBy = Created.Name;
                        }
                        if (e.Attributes.Contains("domainname"))
                        {
                            DomainName = (string)e.Attributes["domainname"];
                        }
                        if (e.Attributes.Contains("internalemailaddress"))
                        {
                            Email = (string)e.Attributes["internalemailaddress"];
                        }
                        if (e.Attributes.Contains("caltype"))
                        {
                            License = e.Attributes["caltype"] as OptionSetValue;
                            LicenseType = GetoptionsetText("systemuser", "caltype", License.Value, service);
                        }

                        string fetchXml = string.Format(@"<fetch distinct='true' no-lock='false' mapping='logical' count='1'>
                                                            <entity name='audit'>
                                                               <attribute name='createdon' />
                                                               <attribute name='userid' />
                                                                <attribute name='action' />
                                                                <order attribute='createdon' descending='true' />
                                                            <filter type='and'>
                                                               <condition attribute='action' operator='eq' value='64'/>
                                                               </filter>
                                                            <link-entity name='systemuser' from='systemuserid' to='objectid' link-type='inner' alias='SystemUser'>
                                                                <attribute name='systemuserid'/>                                                          
                                                                <filter type='and'>
                                                                  <condition attribute='systemuserid' operator='eq' uitype='systemuser' value='{0}' />
                                                                </filter>
                                                            </link-entity>
                                                            </entity>
                                                            </fetch>", e.Id.ToString());

                        // Excute the fetch query and get the xml result.
                        RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
                        {
                            Query = new FetchExpression(fetchXml)
                        };

                        EntityCollection returnCollection = ((RetrieveMultipleResponse)service.Execute(fetchRequest1)).EntityCollection;
                        //  System.Console.WriteLine(returnCollection.Entities.Count());
                        if (returnCollection.Entities.Count > 0)
                        {
                            foreach (var c in returnCollection.Entities)
                            {
                                DateTime Logon = DateTime.MinValue;
                                string Login = string.Empty;

                                if (c.Attributes.Contains("createdon"))
                                {
                                    Logon = (DateTime)c.Attributes["createdon"];
                                    Login = Logon.ToString("dd/MM/yyyy");
                                }
                                CreateLog(CreatedOn + "," + ModifiedOn + "," + CreatedBy + "," + DomainName + "," + Email + "," + LicenseType + "," + FullName + "," + userName + "," + Login);
                                Console.WriteLine(FullName + "," + userName + "," + Login);
                            }
                        }
                        else
                        {
                            CreateLog(CreatedOn + "," + ModifiedOn + "," + CreatedBy + "," + DomainName + "," + Email + "," + LicenseType + "," + FullName + "," + userName + ", Never Accessed");
                            Console.WriteLine(userName + ", Never Accessed");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR2," + ex.Message + ex.InnerException);
                    CreateLog(ex.Message);
                }
            }


            //This method will write log file for operations
            public static void CreateLog(string logText)
            {
                StreamWriter log;
                string date1 = DateTime.Now.ToShortDateString();
                string today = date1.Replace("/", "_") + ".txt";
                if (!System.IO.File.Exists(@"C:\Temp\UserAccess" + today))
                {
                    log = new StreamWriter(@"C:\Temp\UserAccess" + today);
                }
                else
                {
                    log = System.IO.File.AppendText(@"C:\Temp\UserAccess" + today);
                }
                log.WriteLine(logText);
                log.Close();
            }
            public static string GetoptionsetText(string entityName, string attributeName, int optionSetValue, IOrganizationService service)
            {
                string AttributeName = attributeName;
                string EntityLogicalName = entityName;
                RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = EntityLogicalName
                };
                RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
                Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
                Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
                Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
                //IList<OptionMetadata> OptionsList = (from o in options.Options
                //                                     where o.Value.Value == optionSetValue
                //                                     select o).ToList();
                //string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
                string optionsetLabel = (from o in options.Options
                                         where o.Value.Value == optionSetValue
                                         select o).First().Label.UserLocalizedLabel.Label;
                return optionsetLabel;
            }
        }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans