SlideShare a Scribd company logo
I had very strange experience when I had been interview yesterday, there three guys, I was asked to
write a code to upload multiple documents to document library neither laptop was given nor JS file
limit in sharepoint or REST API limit being asked, I was explaining code behind files usage, either
cross domain or single domain. Infact they are not aware of broader view sharepoint usage and
limitations. At this age of 40 people still ask to code on paper to clear interview. If they would have
given laptop and assignment that would have made sense to me.
Reference: Microsoft has already given reference model for this, all we need to do is use it. There are
many MS article for this. However Here is the one up to 2GB file size.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="/SiteAssets/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="FUdiv"></div>
<input type="button" id="btnUpload" value="Add File">
<script type="text/javascript">
$(document).ready(function(){
$("#btnUpload").on("click",function(){
var successFileUp = uploadFile("");
successFileUp.done(function(){
alert("File Uploaded!!!");});
});
});
jQuery(document).ready(function () {
$('#FUdiv').html('<input type="file" id="FileUpload" multiple/>');
if (!window.FileReader) {
alert('This browser does not support the FileReader API.');
}
});
function uploadFile(ID) {
var deferred = $.Deferred();
var serverRelativeUrlToFolder ='testdocs';
var fileInput = jQuery('#FileUpload');
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
(function Tasks(i,callback)
{
if(i<fileInput[0].files.length)
{
var success = ProcessFiles(i);
success.done(function(){Tasks((i+1),callback);});
}
else
{
callback();
}
})(0,function(){deferred.resolve();});
function ProcessFiles(ind)
{
var deferred = $.Deferred();
var getFile = getFileBuffer(ind);
getFile.done(function (arrayBuffer) {
var addFile = addFileToFolder(arrayBuffer,ind);
addFile.done(function (file, status, xhr) {
var getItem = getListItem(file.d.ListItemAllFields.__deferred.uri);
getItem.done(function (listItem, status, xhr) {
var changeItem = updateListItem(listItem.d.__metadata,ind);
changeItem.done(function (data, status, xhr) {
deferred.resolve();
});
changeItem.fail(onError);
});
getItem.fail(onError);
});
addFile.fail(onError);
});
getFile.fail(onError);
return deferred.promise();
}
function getFileBuffer(ind) {
var deferred = jQuery.Deferred();
var reader = new FileReader();
reader.onloadend = function (e) {
deferred.resolve(e.target.result);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(fileInput[0].files[ind]);
return deferred.promise();
}
function addFileToFolder(arrayBuffer,ind) {
var fileName = fileInput[0].files[ind].name;
var fileCollectionEndpoint = String.format("{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files"
+"/add(overwrite=true, url='{2}')",serverUrl, serverRelativeUrlToFolder, fileName);
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-type": "application/json;odata=verbose"
}
});
}
function getListItem(fileListItemUri) {
return jQuery.ajax({
url: fileListItemUri,
type: "GET",
headers: { "accept": "application/json;odata=verbose" }
});
}
function updateListItem(itemMetadata,ind) {
var newName = ID.toString()+'-'+fileInput[0].files[ind].name;
var body = String.format("{{'__metadata':
{{'type':'{0}'}},'FileLeafRef':'{1}','Title':'{2}','ParentID':'{3}'}}",
itemMetadata.type, newName, newName, ID);
return jQuery.ajax({
url: itemMetadata.uri,
type: "POST",
data: body,
headers: {
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-type": "application/json;odata=verbose",
"IF-MATCH": itemMetadata.etag,
"X-HTTP-Method": "MERGE"
}
});
}
return deferred.promise();
}
function onError(error) {
alert(error.responseText);
}
</script>
</body>
</html>
The same thing with SP hosted app.
RESTAPI_SPHOSTED_APP

More Related Content

PDF
Restap ito uploadfilessharepoint
TXT
Issues chmk (1)
PDF
Advanced JQuery Mobile tutorial with Phonegap
PPT
jQuery Mobile with HTML5
PDF
jQuery Mobile: Progressive Enhancement with HTML5
PDF
Robots and-sitemap - Version 1.0.1
PPTX
Canonical and robotos (2)
TXT
Etoy proyecto
Restap ito uploadfilessharepoint
Issues chmk (1)
Advanced JQuery Mobile tutorial with Phonegap
jQuery Mobile with HTML5
jQuery Mobile: Progressive Enhancement with HTML5
Robots and-sitemap - Version 1.0.1
Canonical and robotos (2)
Etoy proyecto

What's hot (8)

PDF
Javascript #2.2 : jQuery
PDF
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
PDF
6 Things You Didn't Know About Firebase Auth
PDF
null Bangalore meet - Php Security
PPT
External Data Access with jQuery
KEY
Molly Holzschlag - How HTML 5 is Going to Completely Change your Web App
PPTX
So You're the New SharePoint Administrator
Javascript #2.2 : jQuery
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
6 Things You Didn't Know About Firebase Auth
null Bangalore meet - Php Security
External Data Access with jQuery
Molly Holzschlag - How HTML 5 is Going to Completely Change your Web App
So You're the New SharePoint Administrator
Ad

Similar to RESTAPI_SPHOSTED_APP (20)

PDF
Summit2014 topic 0066 - 10 enhancements that require 10 lines of code
ODT
PDF
The Django Web Application Framework
PDF
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
PPTX
Node.js: The What, The How and The When
PDF
URLProtocol
PDF
Attractive HTML5~開発者の視点から~
PPTX
Javascript first-class citizenery
PDF
Mozilla Web Apps - Super-VanJS
KEY
BlackBerry DevCon 2011 - PhoneGap and WebWorks
ODP
CodeIgniter PHP MVC Framework
PDF
Softshake - Offline applications
PDF
node.js practical guide to serverside javascript
PPTX
End-to-end testing with geb
PDF
The Big Picture and How to Get Started
KEY
Approaches to mobile site development
PDF
Taking Web Apps Offline
PDF
Build web application by express
PPT
Real Time Communication using Node.js and Socket.io
PDF
php-mysql-tutorial-part-3
Summit2014 topic 0066 - 10 enhancements that require 10 lines of code
The Django Web Application Framework
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
Node.js: The What, The How and The When
URLProtocol
Attractive HTML5~開発者の視点から~
Javascript first-class citizenery
Mozilla Web Apps - Super-VanJS
BlackBerry DevCon 2011 - PhoneGap and WebWorks
CodeIgniter PHP MVC Framework
Softshake - Offline applications
node.js practical guide to serverside javascript
End-to-end testing with geb
The Big Picture and How to Get Started
Approaches to mobile site development
Taking Web Apps Offline
Build web application by express
Real Time Communication using Node.js and Socket.io
php-mysql-tutorial-part-3
Ad

Recently uploaded (20)

PPTX
430838499-Anaesthesiiiia-Equipmenooot.pptx
PDF
Beginner’s Guide to Digital Marketing.pdf
PDF
CV of Architect Professor A F M Mohiuddin Akhand.pdf
PPTX
STS CHAP 4 human development as reflected
PPTX
Prokaryotes v Eukaryotes PowerPoint.pptx
PPT
pwm ppt .pdf long description of pwm....
PPTX
GPAT Presentation PPT and details about imp topics.pptx
PPTX
1751884730-Visual Basic -Unitj CS B.pptx
PDF
ELA Parts of Speech Pronoun Educational Presentation in Green 3D Gradient Sty...
PPTX
DPT-MAY24.pptx for review and ucploading
PPT
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
PPTX
Slideham presentation for the students a
PDF
APNCET2025RESULT Result Result 2025 2025
PDF
Career Overview of John Munro of Hilton Head
PDF
iTop VPN Crack Latest Version 2025 Free Download With Keygen
PPTX
Core Characteristics and Abilities of an Effective Teacher_0.pptx
DOCX
PRACTICE-TEST-12 is specially designed for those
PPTX
Digital Education Presentation for students.
PPTX
The-Scope-of-Food-Quality-and-Safety.pptx managemement
PDF
Sheri Ann Lowe Compliance Strategist Resume
430838499-Anaesthesiiiia-Equipmenooot.pptx
Beginner’s Guide to Digital Marketing.pdf
CV of Architect Professor A F M Mohiuddin Akhand.pdf
STS CHAP 4 human development as reflected
Prokaryotes v Eukaryotes PowerPoint.pptx
pwm ppt .pdf long description of pwm....
GPAT Presentation PPT and details about imp topics.pptx
1751884730-Visual Basic -Unitj CS B.pptx
ELA Parts of Speech Pronoun Educational Presentation in Green 3D Gradient Sty...
DPT-MAY24.pptx for review and ucploading
NO000387 (1).pptsbsnsnsnsnsnsnsmsnnsnsnsjsnnsnsnsnnsnnansnwjwnshshshs
Slideham presentation for the students a
APNCET2025RESULT Result Result 2025 2025
Career Overview of John Munro of Hilton Head
iTop VPN Crack Latest Version 2025 Free Download With Keygen
Core Characteristics and Abilities of an Effective Teacher_0.pptx
PRACTICE-TEST-12 is specially designed for those
Digital Education Presentation for students.
The-Scope-of-Food-Quality-and-Safety.pptx managemement
Sheri Ann Lowe Compliance Strategist Resume

RESTAPI_SPHOSTED_APP

  • 1. I had very strange experience when I had been interview yesterday, there three guys, I was asked to write a code to upload multiple documents to document library neither laptop was given nor JS file limit in sharepoint or REST API limit being asked, I was explaining code behind files usage, either cross domain or single domain. Infact they are not aware of broader view sharepoint usage and limitations. At this age of 40 people still ask to code on paper to clear interview. If they would have given laptop and assignment that would have made sense to me.
  • 2. Reference: Microsoft has already given reference model for this, all we need to do is use it. There are many MS article for this. However Here is the one up to 2GB file size. <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="/SiteAssets/jquery-1.9.1.min.js"></script> </head> <body> <div id="FUdiv"></div> <input type="button" id="btnUpload" value="Add File"> <script type="text/javascript"> $(document).ready(function(){ $("#btnUpload").on("click",function(){ var successFileUp = uploadFile(""); successFileUp.done(function(){ alert("File Uploaded!!!");}); }); }); jQuery(document).ready(function () { $('#FUdiv').html('<input type="file" id="FileUpload" multiple/>'); if (!window.FileReader) { alert('This browser does not support the FileReader API.'); } }); function uploadFile(ID) { var deferred = $.Deferred(); var serverRelativeUrlToFolder ='testdocs'; var fileInput = jQuery('#FileUpload'); var serverUrl = _spPageContextInfo.webAbsoluteUrl;
  • 3. (function Tasks(i,callback) { if(i<fileInput[0].files.length) { var success = ProcessFiles(i); success.done(function(){Tasks((i+1),callback);}); } else { callback(); } })(0,function(){deferred.resolve();}); function ProcessFiles(ind) { var deferred = $.Deferred(); var getFile = getFileBuffer(ind); getFile.done(function (arrayBuffer) { var addFile = addFileToFolder(arrayBuffer,ind); addFile.done(function (file, status, xhr) { var getItem = getListItem(file.d.ListItemAllFields.__deferred.uri); getItem.done(function (listItem, status, xhr) { var changeItem = updateListItem(listItem.d.__metadata,ind); changeItem.done(function (data, status, xhr) { deferred.resolve(); }); changeItem.fail(onError); }); getItem.fail(onError); }); addFile.fail(onError); }); getFile.fail(onError); return deferred.promise(); } function getFileBuffer(ind) { var deferred = jQuery.Deferred(); var reader = new FileReader(); reader.onloadend = function (e) { deferred.resolve(e.target.result); } reader.onerror = function (e) {
  • 4. deferred.reject(e.target.error); } reader.readAsArrayBuffer(fileInput[0].files[ind]); return deferred.promise(); } function addFileToFolder(arrayBuffer,ind) { var fileName = fileInput[0].files[ind].name; var fileCollectionEndpoint = String.format("{0}/_api/web/getfolderbyserverrelativeurl('{1}')/files" +"/add(overwrite=true, url='{2}')",serverUrl, serverRelativeUrlToFolder, fileName); return jQuery.ajax({ url: fileCollectionEndpoint, type: "POST", data: arrayBuffer, processData: false, headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(), "content-type": "application/json;odata=verbose" } }); } function getListItem(fileListItemUri) { return jQuery.ajax({ url: fileListItemUri, type: "GET", headers: { "accept": "application/json;odata=verbose" } }); } function updateListItem(itemMetadata,ind) { var newName = ID.toString()+'-'+fileInput[0].files[ind].name; var body = String.format("{{'__metadata': {{'type':'{0}'}},'FileLeafRef':'{1}','Title':'{2}','ParentID':'{3}'}}", itemMetadata.type, newName, newName, ID); return jQuery.ajax({ url: itemMetadata.uri,
  • 5. type: "POST", data: body, headers: { "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(), "content-type": "application/json;odata=verbose", "IF-MATCH": itemMetadata.etag, "X-HTTP-Method": "MERGE" } }); } return deferred.promise(); } function onError(error) { alert(error.responseText); } </script> </body> </html> The same thing with SP hosted app.