- var user = [];
-
- function GetUserId() {
- if (validate()) {
- $("div[class*='sp-peoplepicker-topLevel']").each(function() {
- if ($(this).find("span[class='ms-entity-resolved']").text().length > 0) {
- var endPointURLToGetUseID = _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers?$select=Title,ID&$filter=Title eq '" + $(this).find("span[class='ms-entity-resolved']").text() + "'";
- $.ajax({
- url: endPointURLToGetUseID,
- type: "GET",
- dataType: "json",
- async: false,
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: callSuccessMethod,
- error: callFailureMethod
- });
- }
- });
-
- function callSuccessMethod(data, request) {
- $.each(data.d.results, function(key, val) {
- var GetId = val.Id;
- user.push([GetId]);
- });
- }
-
- function callFailureMethod(data, errCode, errMessage) {
- alert("Error: " + errMessage);
- }
- CreateListItem();
- }
- }
-
- function CreateListItem() {
- var clientContext = new SP.ClientContext('https://your site Url');
- var List = clientContext.get_web().get_lists().getByTitle('dynamiclist');
- var itemCreateInfo = new SP.ListItemCreationInformation();
- this.ListItem = List.addItem(itemCreateInfo);
- var Ename = document.getElementById("empname").value;
- var EmpID = document.getElementById("empid").value;
- var user1;
- ListItem.set_item('Employeename', Ename);
- ListItem.set_item('Empid', EmpID);
- if (user.length == 4) {
- ListItem.set_item('Approver1', user[1][0]);
- ListItem.set_item('Approver2', user[1][0]);
- ListItem.set_item('Approver3', user[2][0]);
- ListItem.set_item('Approver4', user[3][0]);
- ListItem.update();
- } else if (user.length == 3) {
- ListItem.set_item('Approver1', user[0][0]);
- ListItem.set_item('Approver2', user[1][0]);
- ListItem.set_item('Approver3', user[2][0]);
- ListItem.set_item('Approver4', user1)
- ListItem.update();
- } else if (user.length == 2) {
- ListItem.set_item('Approver1', user[0][0]);
- ListItem.set_item('Approver2', user[1][0]);
- ListItem.set_item('Approver3', user1);
- ListItem.set_item('Approver4', user1)
- ListItem.update();
- } else if (user.length == 1) {
- ListItem.set_item('Approver1', user[0][0]);
- ListItem.set_item('Approver2', user1);
- ListItem.set_item('Approver3', user1);
- ListItem.set_item('Approver4', user1)
- ListItem.update();
- } else if (user.length == 0) {
- ListItem.set_item('Approver1', user1);
- ListItem.set_item('Approver2', user1);
- ListItem.set_item('Approver3', user1);
- ListItem.set_item('Approver4', user1)
- ListItem.update();
- }
- clientContext.load(ListItem);
- clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
- }
-
- function onQuerySucceeded() {
- alert('Leave Applied Suceessfully');
- }
-
- function onQueryFailed(sender, args) {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
-
- function validate() {
- var returnval = true;
- if (document.getElementById("empname").value == "") {
- alert("Please enter the Employee ID");
- returnval = false;
- } else if (document.getElementById("empid").value == "") {
- alert("Please enter the Department.");
- returnval = false;
- } else if (document.getElementById("txtlev").value == 0) {
- alert("Please Specify level of approval");
- returnval = false;
- } else if (document.getElementById("peoplePickerDiv1_TopSpan_HiddenInput").value == "" || document.getElementById("peoplePickerDiv1_TopSpan_HiddenInput").value == "[]") {
- alert("Please Specify Approver1");
- returnval = false;
- } else if (document.getElementById("txtlev").value == 2 && document.getElementById("peoplePickerDiv2_TopSpan_HiddenInput").value == "" || document.getElementById("peoplePickerDiv2_TopSpan_HiddenInput").value == "[]") {
- alert("Please Specify Approver2");
- returnval = false;
- } else if (document.getElementById("txtlev").value == 3 && document.getElementById("peoplePickerDiv3_TopSpan_HiddenInput").value == "" || document.getElementById("peoplePickerDiv3_TopSpan_HiddenInput").value == "[]") {
- alert("Please Specify Approver3");
- returnval = false;
- } else if (document.getElementById("txtlev").value == 4 && document.getElementById("peoplePickerDiv4_TopSpan_HiddenInput").value == "" || document.getElementById("peoplePickerDiv4_TopSpan_HiddenInput").value == "[]") {
- alert("Please Specify Approver4");
- returnval = false;
- }
- return returnval;
- }
Html File
Here, I am using Dynamic list as My list.
Employeename, Empid, Approver1, Approver2, Approver3, Approver4 are the field columns of that List.
Step 1
In HTML file.
Include jquery-1.8.3.js,
ui/1.10.0/jquery-ui.js,
http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js ,
moment.js ,
sppeoplepicker.min.js ,
sppeoplepicker.js
And also, store the above JS file as .js format and include in HTML.
Step 2
In Document.ready function, intialise all the people pickers. For example -
$("#peoplePickerDiv1").spPeoplePicker();
Step 3
Based on the option values, I am hiding and showing textbox
- if(document.getElementById("txtlev").value==0)
- $('tr[id="approver1"]').hide();
- $('tr[id="approver1"]').show();
Create necessary Div tag and buttons to append people picker as shown in the above HTML file.
Step 4
In JS part
For Loop for all Divs
- $("div[class*='sp-peoplepicker-topLevel']").each(function()
It checks whether the Div box is empty or not..
- if($(this).find("span[class='ms-entity-resolved']").text().length>0
Step 5
Based on the div value, the below code retrieves the Id of Users(people picker) Eg : SpTestuser1, the id is 10
- var endPointURLToGetUseID =_spPageContextInfo.webAbsoluteUrl+"/_api/web/siteusers?$select=Title,ID&$filter=Title eq '"+$(this).find("span[class='ms-entity-resolved']").text()+"'";
Step 6
- function callSuccessMethod(data, request) {
- $.each(data.d.results, function(key, val) {
- var GetId = val.Id;
- user.push([GetId]);
- });
Based on results of Val.id, we push the values of GetId into some variable user array I created (Push Function acts by using moment.js)
Step 7
Update the values in the List using below code.
- ListItem.set_item('Employeename', Ename);
- ListItem.set_item('Empid', EmpID);
- if (user.length == 4) {
- ListItem.set_item('Approver1', user[1][0]);
- ListItem.set_item('Approver2', user[1][0]);
- ListItem.set_item('Approver3', user[2][0]);
- ListItem.set_item('Approver4', user[3][0]);
- ListItem.update();
- }
Step 8
The below code is used for validation...
- if (document.getElementById("txtlev").value == 2 && document.getElementById("peoplePickerDiv2_TopSpan_HiddenInput").value == "" || document.getElementById("peoplePickerDiv2_TopSpan_HiddenInput").value == "[]") {
- alert("Please Specify Approver2");
- returnval = false;
- }