1

I am using SharePoint 2013 on premises. I have to find the first login details of the user.

Please suggest me the solution. Is it possible with the out-of-the-box feature?

1

Create a custom list which will store logged in user information.

Add a code snippet using JSOM on the master page. This will insert current logged in user data to the list.

You can enhance the logic by using cookies etc to eliminate duplicate record insertion.

Now you can create custom list view and sort the data as you desired

0

Create a custom list (I named it as FirstUserLogData) with two fields,

  1. User (People or Group)
  2. Date (Date Time field) - Set the Default Value of the Fied to Today during the column creation itself and it should be a Date Only Field

Insert the below code in your master page

$(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', LoadCurrentUser);
});

//Loads the current user who has logged in the page
function LoadCurrentUser() {
    var context = SP.ClientContext.get_current();
    var siteColl = context.get_site();
    var web = siteColl.get_rootWeb();
    this._currentUser = web.get_currentUser();
    context.load(this._currentUser);
    context.executeQueryAsync(Function.createDelegate(this, UpdateLoggedinUser), Function.createDelegate(this, Failure));
}

function Failure() {
    alert('failed');
}

//This method logs the first logged in user in the list created
function UpdateLoggedinUser() {
    var listName = "FirstUserLogData";
    var userid = this._currentUser.get_id();
    $.when(CheckForDuplicate(listName)).done(function (count) {
        if (count == 0) {
            var itemType = GetItemTypeForListName(listName);
            var item = {
                "__metadata": { "type": itemType },
                "UserId": userid
            }
            CreateListItem(listName, item);
        }
    });
}
//This method gets the item type for the required list
function GetItemTypeForListName(name) {
    var itemType;
    $.ajax({
        async: false,
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + name + "')?$select=ListItemEntityTypeFullName",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            itemType = data.d.ListItemEntityTypeFullName;
        },
        error: function (data) {
            failure(data);
        }
    });
    return itemType;
}

//This Method creates the list item
function CreateListItem(listName, item) {
    var createdItemId;
    $.ajax({
        async: false,
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            createdItemId = data.d.ID;
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
    return createdItemId;
}

//This method checks if the first loggin has been already made or not by querying the list data
function CheckForDuplicate(listName) {
    var count=0;
    var dfd = $.Deferred(function () {
        var clientContext = new SP.ClientContext.get_current();
        var web = clientContext.get_web();
        var list = clientContext.get_web().get_lists().getByTitle(listName);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Date'/><Value IncludeTimeValue='FALSE' Type='DateTime'><Today /></Value></Eq></Where></Query></View>");
        var collListItem = list.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(function () {
            count = collListItem.get_count();
            dfd.resolve(count);
        }, Function.createDelegate(this, Failure));
    });
    return dfd.promise();
}

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.