Sachchin Annam

Sachchin Annam

  • NA
  • 549
  • 50.7k

Redirect to list view after SPServices Update

Apr 1 2018 10:34 AM

Hello Techies,
I created a custom form to submit list item and attach multiple attachments.

And would need to redirect user to default list view after all the attachments are attached to a list item.
I tried adding window.location.href='default List url' but it redirects while the attachments are getting added.
It works fine for small file(s) but for large file(s), only 1 attachment gets added.
Here is the snippet:
HTML:


  1.   
  2. <table align="left" border="1" cellpadding="0" cellspacing="0" >  
  3. <tbody>  
  4. <tr>  
  5. <td  valign="top">  
  6. <h3> Nameh3>  
  7. td>    
  8. <td valign="top" style="padding:9px;">    
  9. <input type="text" value="" maxlength="255" id="Title" title="Name" style="width: 96%;" ms-spellcheck-true">    
  10. td>  
  11. tr>  
  12.                                   
  13. <tr >  
  14. <td >  
  15. <span style="font-family: " segoe ui" ,sans-serif; color: #444444">  
  16. Click here to attach file  
  17. span>   
  18. <div id="attachFilesHolder ">    
  19.  <input type="file" id="attachment" name="FileUpload" multiple/>  
  20. div>    
  21. td>  
  22. <td>  
  23. td>  
  24. tr>  
  25. table>  
  26. <div>  
  27. <input name="SaveItem" style=" height: 40px; font-size: 15px;" id="NewSaveItem" accesskey="O" onclick="" type="button" value="Click here to submit " target="_self">  
  28. div>  
And here is my js:
  1. var ListTitle = "SubTask";  
  2.   
  3. $( document ).ready(function() {  
  4. //$('#attachment').multifile();  
  5.     $("#NewSaveItem").click(function() {  
  6.     CreateNewItem();  
  7.            
  8. });  
  9.       
  10.       
  11. });  
  12.   
  13. function CreateNewItem() {  
  14.     var data = {  
  15.         __metadata: { 'type'"SP.Data.SubTaskListItem" },  
  16.         Title: $('#Title').val()  
  17.     };  
  18.   
  19.     $.ajax({  
  20.         //_spPageContextInfo.webAbsoluteUrl get current SharePoint site url  
  21.         url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('" + ListTitle + "')/Items",  
  22.         type: "POST",  
  23.         headers: {  
  24.             "accept""application/json;odata=verbose",  
  25.             "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  26.             "content-Type""application/json;odata=verbose"  
  27.         },  
  28.         // to make sync calls.  
  29.         async: false,  
  30.         data: JSON.stringify(data),  
  31.         success: function (data) {  
  32.             if (data.d.ID != undefined && data.d.ID > 0){  
  33.                 //Uploads files to SharePoint List item  
  34.                 UploadFileToListItem(data.d);  
  35.                   
  36.                 }  
  37.             else  
  38.             {  
  39.                 console.log('Item added successfully');  
  40.                 }  
  41.         },  
  42.         error: function (error) {  
  43.             console.log('Problem saving data');  
  44.         }  
  45.     });  
  46.    // window.location.href = 'https://connect.sharepoint.com/sites/SpApps/Lists/SubTask/';  
  47.   
  48. }  
  49.   
  50.   
  51.   
  52. //Once the item is created now let’s try to upload documents to SharePoint list item.  
  53.   
  54. function UploadFileToListItem(data) {  
  55.     var element = document.getElementById("attachment");  
  56.     lastFileName = element.files[element.files.length - 1].name;  
  57.     for (var i = 0; i < element.files.length; i++) {  
  58.         var file = element.files[i];  
  59.         uploadFile(data, file);  
  60.     }  
  61.     alert('Done');  
  62.     window.location.href = 'https://connect.sharepoint.com/sites/SpApps/Lists/SubTask/';  
  63. }  
  64.   
  65. //uploads single file at a time.  
  66. function uploadFile(data, file) {  
  67.     var getFileBuffer = function (file) {  
  68.         var deferred = $.Deferred();  
  69.         var reader = new FileReader();  
  70.   
  71.         reader.onload = function (e) {  
  72.             deferred.resolve(e.target.result);  
  73.         }  
  74.   
  75.         reader.onerror = function (e) {  
  76.             deferred.reject(e.target.error);  
  77.         }  
  78.   
  79.         reader.readAsArrayBuffer(file);  
  80.   
  81.         return deferred.promise();  
  82.     };  
  83.       
  84.     getFileBuffer(file).then(function (buffer) {  
  85.         var binary = "";  
  86.         var bytes = new Uint8Array(buffer);  
  87.         var i = bytes.byteLength;  
  88.         while (i--) {  
  89.             binary = String.fromCharCode(bytes[i]) + binary;  
  90.         }  
  91.         var fileName = file.name;  
  92.         var error = ''  
  93.         $().SPServices({  
  94.             operation: "AddAttachment",  
  95.             async: false,  
  96.             listName: ListTitle,  
  97.             listItemID: data.Id,  
  98.             fileName: fileName,  
  99.             attachment: btoa(binary),  
  100.             completefunc: function (xData, Status) {  
  101.                 console.log(file.name + " uploaded");  
  102.             }  
  103.         });  
  104.     });  
  105.       
  106.       
  107. }  


Answers (1)