Uplaod files to SharePoint document library using ECMA script (javascript client object model) :
UploadDocuments = function () {
if (!window.FileReader) {
alert("Browser does not support the HTML5 File APIs");
return;
}
var call = getDocument();
call.done(function (buffer, fileName) {
var call2 = uploadDocument(buffer, fileName);
call2.done(function (data, textStatus, jqXHR) {
var call3 = getItem(data.d);
call3.done(function (data, textStatus, jqXHR) {
var item = data.d;
var call4 = getCurrentUser();
call4.done(function (data, textStatus, jqXHR) {
var userId = data.d.CurrentUser.Id;
var call5 = updateItemFields(item, userId);
call5.done(function (data, textStatus, jqXHR) {
var div = jQuery("#msg");
div.text("Item added");
});
call5.fail(failHandler);
});
call4.fail(failHandler);
});
call3.fail(failHandler);
});
call2.fail(failHandler);
});
call.fail(function (errorMessage) {
alert(errorMessage);
});
function getDocument() {
var def = new jQuery.Deferred();
var element = document.getElementById("fileIdCard");
var file = element.files[0];
var parts = element.value.split("\\");
var fileName = parts[parts.length - 1];
var reader = new FileReader();
reader.onload = function (e) {
def.resolve(e.target.result, fileName);
}
reader.onerror = function (e) {
def.reject(e.target.error);
}
reader.readAsArrayBuffer(file);
return def.promise();
}
function uploadDocument(buffer, fileName) {
var url = String.format("{0}/_api/Web/Lists/getByTitle('ID Card Attachments')/RootFolder/Files/Add(url='{1}', overwrite=true)",
_spPageContextInfo.webAbsoluteUrl, fileName);
//alert(url);
var call = jQuery.ajax({
url: url,
type: "POST",
data: buffer,
processData: false,
headers: {
Accept: "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"Content-Length": buffer.byteLength
}
});
return call;
}
function getItem(file) {
var call = jQuery.ajax({
url: file.ListItemAllFields.__deferred.uri,
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
return call;
}
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
function getCurrentUser() {
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/?$select=CurrentUser/Id&$expand=CurrentUser/Id",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
return call;
}
function updateItemFields(item, userId) {
var itemType = GetItemTypeForListName('ID Card Attachments');
//alert(itemType);
var now = new Date();
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('ID Card Attachments')/Items(" +
item.Id + ")",
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.IdCardAttachmentsItem' },
'Employee Type': '' + "Permanant"
}),
headers: {
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"IF-MATCH": item.__metadata.etag,
"X-Http-Method": "MERGE"
}
});
return call;
}
}
failHandler = function (jqXHR, textStatus, errorThrown) {
var response = JSON.parse(jqXHR.responseText);
var message = response ? response.error.message.value : textStatus;
alert("Call failed. Error: " + message);
}
onQuerySuccess = function () {
alert("Success.!");
}
onQueryFail = function (sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
No comments:
Post a Comment