SharePoint 2016 Logo

I guess SPServices is the best solution for SharePoint. We still have SharePoint 2010 and prepate to migrate to SharePoint 2016 through SharePoint 2013. But for current tasks which must be made immediately, we still use SharePoint 2010, but only without server code. Rather often we have to make custom views for other users.

It's easy to get user login name with Javascript and SPServices. I don’t want to copy docs of SPServices, but I just want to have useful snippets to remember them fast.

In domain authentication SPServices return user login as "Domain\username". But usually you operate only with user account name.

 

//Connect SPServices
<script src="/_layouts/scripts/jquery/jquery.js"></script>
<script src="/_layouts/scripts/jquery.SPServices-2014.02/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>

//Main code of usage SPServices to get username
<script>
   var thisUserAccount ;          
    var login;
	$(document).ready(function() {
		thisUserAccount= $().SPServices.SPGetCurrentUser({
		fieldName: "Name",
		debug: false
		});

		if(thisUserAccount) {
			var pair = thisUserAccount.split("\\");
			login = pair[1];
		}
                alert(login);
	});
</script>