<script type="text/javascript">
$(document).ready(function() {
BindDepartment();
$("#AddListItem").click(function (event) {
createListItem();
window.location.href = window.location.href;
//event.preventDefault();
});
});
function createListItem() {
var listName = "EmployeeInfo";
var url = _spPageContextInfo.webAbsoluteUrl;
var itemType = getItemTypeForListName(listName);
var item = {
"__metadata": { "type": itemType },
"Title": $('#txtTitle').val(),
"Department": $('#ddlDepartment option:selected').val(),
"EmpName": $('#txtEmpName').val(),
"EmpID": $('#txtEmpId').val(),
"Manager": $('#txtManager').val()
};
$.ajax({
url: url + "/_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) {
alert("Item updated, refreshing avilable items");
success(data);
},
error: function (data) {
alert("Ooops, an error occured. Please try again");
failure(data);
}
});
alert(url);
}
function getItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
function BindDepartment()
{
var url = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: url + "/_api/web/lists/getbytitle('Department')/items",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function(data){
$.each(data.d.results, function(index, item){
var listNames = "<option>" + item.Title + "</option>";
//alert(listNames);
$('#ddlDepartment').append(listNames);
});
},
error: function(error){
alert(JSON.stringify(error));
}
});
}
</script>