4

I'm trying to figure out when a user has last visited.

So let's say I logged out of my SharePoint intranet last night at 11PM, and this morning I logged back in at 9AM, I'd like to see a message saying "you last logged in yesterday at 11PM" (and as far as I care, a console message would do just fine for now).

So. How can I see when the current user last logged in?

Preferably a solution in JavaScript, since I can't use C# for this (not allowed to do server-side coding). Also, I'm not sure if cookies are a great solution, what is your opinion on using cookies?

Thanks in advance for any help or suggestions, I really do appreciate your response.

EDIT
My plan is to eventually use this to see if there are any new items that were added to our SharePoint intranet after the user has last visited, and then show those items to the user the next time he/she logs in. I know how to do the latter, I'm just stuck at finding out when a user last visited. :/

1

needs to be done through active directory:

powershell

http://admin-sharepoint.blogspot.co.uk/2013/08/get-users-last-logon-time-and-date.html

in your case it would be through c# to get to active directory and query lastLogon:

    public DateTime Get(string attr, string UserName)
    {

        DomainConfiguration domainConfig = new DomainConfiguration();
        using (new SPMonitoredScope("AD Properties"))
        {
            using (DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainConfig.DomainName, domainConfig.UserName, domainConfig.Password))
            {
                //DirectorySearcher searcher = new DirectorySearcher(domain, "(|(objectClass=organizationalUnit)(objectClass=container)(objectClass=builtinDomain)(objectClass=domainDNS))");
                DirectorySearcher searcher = new DirectorySearcher(domain);
                searcher.PageSize = 1000;
                searcher.Filter = "(SAMAccountName='" + UserName + "')";
                //searcher.Filter = "(|(objectCategory=group)(objectCategory=person))";
                searcher.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
                var user = searcher.FindOne();
                DateTime LastLogon = DateTime.FromFileTime((Int64)user.Properties["lastLogon"].Value);
                return LastLogon;
            }
        }
    }

https://stackoverflow.com/questions/13796547/how-to-get-last-login-details-time-for-all-users

EDIT

i could be wrong but i dont think its possible todo that client side using the javascript client object model!

you would need to create a list, store the username, last login time and retrieve them that way! could be done through JS but be a pain! as you need to update whenever someone logs off, and get the info when they login!

  • Thanks! Although I currently cannot use C# due to not yet allowed to be doing server-sided coding, I'll keep your answer in mind and use it as soon as I begin with C#. – Magali Mar 6 '14 at 15:28
  • shame! iv updated my answer! – Ali Jafer Mar 6 '14 at 16:12
  • Regarding your updated answer, it would indeed be a pain to do so in JavaScript. I didn't figure out yet how to detect whether a user is logged out (closing the browser also counts as logging out) or back logged in. So I'll just wait until I get to the C#, which should be somewhere in the next two weeks. :) – Magali Mar 18 '14 at 12:41
  • Wouldn't this return the DateTime, of the last login of any system, that uses the AD? How do you know, that this was SharePoint? – Hinek Jun 27 '17 at 13:06
  • Yes your right it would. Only way to properly do it is to track via a list on SharePoint that is updated on a timer service (show user logged in or logged out). the timer service checks against server IIS logs and eventViewer logs. It should also have a check every 30 min (timer job runs every 30min, could run every 5min but need keepalive check for 30). If no logs from user reported after that time the user is considered logged off otherwise if logs keep coming in with the users account then the user is active so list is updated every time timer is run with a list of active users ;). – Ali Jafer Jun 29 '17 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.