Friday, August 28, 2015

User Profile Service in SharePoint 2013

User Profile Service in SharePoint 2013

Introduction

Welcome to a new tip of User profile Service in SharePoint 2013. Today, I will be showing you a beautiful way to get Current logged in User’s details on a page using JavaScript.
So let’s do it:
  • Create a new Page
  • Add a Content Editor web part
  • Place the below code in it.

Code

-----The following HTML code is used to display the Current logged in User name on a Content Editor Web Part.
Name- <span id="username"></span><br/>

---- The following code calls the sp service and append the HTML code replacing by the value.

<script type="text/javascript"> 
  var thisUserAccount ;         
    $(document).ready(function() {
    thisUserAccount= $().SPServices.SPGetCurrentUser({
    fieldNames: ["Title","Department","Email"],
    debug: false
});
 
var nametag="<span id=\"username\">"+thisUserAccount.Title+"</span>";
 
$('#username').replaceWith(nametag);
 
}   
    ); </script>
This is the result on the Home page:
As you can see, the name comes up on the page load of the site. In a similar way, you can have the following attributes like DepartmentEmail, etc. to your page.
FIELD NAMEINTERNAL NAME
AccountName
NameTitle
Work e-mailEmail
About meNotes
PicturePicture
DepartmentDepartment
Job TitleJobTitle
SIP AddressSipAddress
First nameFirstName
Last NameLastName
Work phoneWorkPhone
OfficeOffice
User nameUserName
Web siteWebSite
ResponsibilitiesSPResponsibility
You just have to make a change like:
  • thisUserAccount.Title
  • thisUserAccount.Email
  • thisUserAccount.Department
in the code and hence the purpose will be served.
Till then, keep learning.
Cheers!

No comments:

Post a Comment