Search This Blog

Friday, November 27, 2015

Pre Populate User data onto sharepoint form using spservices jquery

SPServices is a jQuery library which abstracts SharePoint’s Web Services and makes them easier to use. It also includes functions which use the various Web Service operations to provide more useful (and cool) capabilities. It works entirely client side and requires no server install.

We can achieve it in two ways.
Add these two files in your page
<script src="../SiteAssets/jquery.min.js" type="text/javascript"></script>
<script src="../SiteAssets/jquery.SPServices.min.js" type="text/javascript"></script>
(or)
<SharePoint:ScriptLink language="javascript" name="~sitecollection/Styl\e Library/jquery.min.js" 
Defer="true" runat="server"/>
<SharePoint:ScriptLink language="javascript" Defer="true" runat="server" 
name="~sitecollection/Style Library/jquery.SPServices.min.js" />
The above are two ways of including js files in your master page or any application page that where you want to get current login user details in client side.

First one)
<script src="../SiteAssets/jquery.min.js" type="text/javascript"></script>
<script src="../SiteAssets/jquery.SPServices.min.js" type="text/javascript"></script>
$().SPServices({
operation: "GetUserInfo",
async: false,
userLoginName: $().SPServices.SPGetCurrentUser(),
completefunc: function (xData, Status) {
$(xData.responseXML).find("User").each(function() {
curUserId = $(this).attr("ID");
curUserName = $(this).attr("Name");
curFullUserName = $(this).attr("ID")+";#"+$(this).attr("Name");
});
}
});
The above will get the  current  login user id and the user name, this we  implement in the master page of shaerpoint designer 2013  and you can use this in 2010 also.  
Second One)
<script language="javascript" type="text/javascript">
$(document).ready(function() {
// Set the 'Title' input field to the current user's "Title" property
var userName = $().SPServices.SPGetCurrentUser({
fieldName: "Title"
});
$("input[Title='Title']").val(userName);
// Set the 'User Name' input field to the current user's "UserName" property
var userName = $().SPServices.SPGetCurrentUser({
fieldName: "UserName"
});
$("input[Title='User Name']").val(userName);
// Set the 'Last Name' input field to the current user's "LastName" property
var lastName = $().SPServices.SPGetCurrentUser({
fieldName: "LastName"
});
$("input[Title='Last Name']").val(lastName);
// Set the 'First Name' input field to the current user's "FirstName" property
var firstName = $().SPServices.SPGetCurrentUser({
fieldName: "FirstName"
});
$("input[Title='First Name']").val(firstName);
// Set the 'Work Phone' input field to the current user's "WorkPhone" property
var userPhone = $().SPServices.SPGetCurrentUser({
fieldName: "WorkPhone"
});
$("input[Title='Work Phone']").val(userPhone);
// Set the 'Department' input field to the current user's "Department" property
var userDepartment = $().SPServices.SPGetCurrentUser({
fieldName: "Department"
});
$("input[Title='Department']").val(userDepartment);
});
</script>

No comments:

Post a Comment