I need to get the site last visited time of the user's, and need to update that time in userprofile properties. I am not sure any api or in build functionality available. Pls suggest me some idea to achieve this?

3 Answers 3


The simple answer - There is no simple or straightforward way to get this. However, you can try these if you have appropriate permissions here to use PowerShell for an immediate solution.

For a long run solution and a convenient solution like 'Analytics' you can try below:

  1. Write a custom API with SQL backed and deploy where you web can access it(Use Azure if budget permits and appropriate approval or develop in-house).
  2. Write a simple JS to call your API and place in your masterpage to capture the currentUrl, User... and use it.

This only effect after a short while but the output is solid to make better decisions over time for different scenarios.


You can do it by power shell as well as server side code.

Load the SharePoint cmdlets

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) 
    Write-Host "Loading SharePoint Powershell Snapin"    
    Add-PSSnapin "Microsoft.SharePoint.Powershell"  -EA SilentlyContinue

Import ActiveDirectory cmdlets

Import-Module ActiveDirectory

# Here's the function that will return the last logon date and time
function Get-ADUserLastLogon([string]$userName)
  $dcs = Get-ADDomainController -Filter {Name -like "*"}
  $time = 0
  foreach($dc in $dcs)
    $hostname = $dc.HostName
    $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
    if($user.LastLogon -gt $time) 
      $time = $user.LastLogon
  $dt = [DateTime]::FromFileTime($time)
  Write-Host $username "last logged on at:" $dt 

Get the user profiles

$site = Get-SPSite "https://intranet.company.com/"
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context) 
$profiles = $profileManager.GetEnumerator()

Iterate all profiles and grab the users last logon date time and write to

foreach($user in $profiles)
     Get-ADUserLastLogon -UserName $user["UserName"]

By Server Side

public DateTime GetLastLogonTime(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;

hope this will help you :)

  • When copy+pasting answers from the web, you are supposed to cite them. Otherwise your answer is a subject of plagiarizing.
    – moe
    Dec 22, 2017 at 7:18
  • i don't think so. we are here to help. not for showing our importance. m just helping people. one more thing above code is common whenever anyone is using they are writing a same code.
    – Negi Rox
    Dec 22, 2017 at 12:25
  • This isn't a matter of opinions. Sharing an available code is absolutely fine, but it still needs to be cited. You are actually doing the opposite of avoiding to show importance, when you don't credit the original author. Also you don't just use someone else's codes without citing them, but also the exact text phrases written by them.
    – moe
    Dec 22, 2017 at 12:43

We can get the data from the UserInfo table in SharePoint content database.

The SQL query:

SELECT top 1 [tp_SiteID]
FROM [WSS_Content_Test].[dbo].[UserInfo]
Where tp_Login='i:0#.w|domain\username'
order by [tp_ExternalTokenLastUpdated] desc

Then update that time in user profile properties.

Or as little_big_thoughts's reply, we can write JavaScript into master page to get the current user information and logon time, then update that time in user profile properties using REST API.

Reference: https://msdn.microsoft.com/en-us/library/office/dn790354.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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