var DocumentlistName ="OceanContractsSourceDocs";
/* #Region - Function to Creating an item in library */
function uploadFile() {
// Define the folder path for this example.
// Get test values from the file input and text input page controls.
var fileInput = $('#inputFile');
var newName = $('#displayName').val();
fileCount = fileInput[0].files.length;
// Get the server URL.
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
var filesUploaded = 0;
for(var i = 0; i < fileCount; i++){
// Initiate method calls using jQuery promises.
// Get the local file as an array buffer.
var getFile = getFileBuffer(i);
getFile.done(function (arrayBuffer,i) {
// Add the file to the SharePoint folder.
var addFile = addFileToFolder(arrayBuffer,i);
addFile.done(function (file, status, xhr) {
//$("#msg").append("<div>File : "+file.d.Name+" ... uploaded sucessfully</div>");
filesUploaded++;
if(fileCount == filesUploaded){
alert("All files uploaded successfully");
//$("#msg").append("<div>All files uploaded successfully</div>");
GetListItems(getListItemssuccess,getListItemsfailure);
$("#inputFile").value = null;
filesUploaded = 0;
}
});
addFile.fail(onError);
});
getFile.fail(onError);
}
// Get the local file as an array buffer.
function getFileBuffer(i) {
var deferred = jQuery.Deferred();
var reader = new FileReader();
reader.onloadend = function (e) {
deferred.resolve(e.target.result,i);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(fileInput[0].files[i]);
return deferred.promise();
}
// Add the file to the file collection in the Shared Documents folder.
function addFileToFolder(arrayBuffer,i) {
var index = i;
// Get the file name from the file input control on the page.
var fileName = fileInput[0].files[index].name;
// Construct the endpoint.
var fileCollectionEndpoint = String.format(
"{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +
"/add(overwrite=true, url='{2}')",
serverUrl, DocumentlistName, fileName);
// Send the request and return the response.
// This call returns the SharePoint file.
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});
}
}
// Display error messages.
function onError(error) {
alert(error.responseText);
}
var lastitemId='';
function GetListItems(success,failure) {
$.ajax({
url: siteUrl + "/_api/web/lists/getbytitle('" +DocumentlistName+ "')/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
;
if(data!=null)
{
var newlength = data.d.results.length - fileCount;
for(var i= newlength;i<=data.d.results.length;i++)
{
success(data.d.results[i].ID);
}
}
},
error: function (data) {
failure(data);
}
});
}
var IDarray= new Array();
/*Getting uploaded document ID success*/
function getListItemssuccess(obj)
{
IDarray=obj
UpdateListItem(IDarray);
}
/*Getting uploaded document ID failure*/
function getListItemsfailure()
{
//$.hideLoading();
}
/*#ENDRegion - Function to get uploaded document ID */
/*Updating list item based on item ID */
function UpdateListItem(id) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle(DocumentlistName);
this.oListItem = oList.getItemById(id);
oListItem.set_item('Contract_x0020_ID', $("#txtContractID").val())
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.GBTonQuerySucceeded), Function.createDelegate(this, this.GBTonQueryFailed));
}
/*Updating list item based on item ID success*/
function GBTonQuerySucceeded() {
}
function GBTonQueryFailed() {
}