Attachment failure

IndianRich 21 Reputation points
2022-10-28T21:10:32.257+00:00

Hi @Michael Williams ,
I am having similar issues. -
Request failed with error message - File Not Found. .Stack Trace -undefined.

I made the changes you suggested but still says the same thing. Since this is a new list item I am trying to attach to, the ID isn't available as yet. I submitted the item then instead of closing the form I queried the item to get the ID. I am getting the correct ID for the attachment folder. Here's how I handled the attachmentFolder:
var attachmentFolder = oWeb.getFolderByServerRelativeUrl('Lists/Issue-Tracker-List/Attachments/' +ID1);
ID1 being the ID of the line item. I am not sure if when I attach the file and then submitting is causing the break even though I am passing though the functions from the end.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,792 questions
{count} votes

2 answers

Sort by: Most helpful
  1. IndianRich 21 Reputation points
    2022-10-28T21:51:40.933+00:00

    @Michael Williams
    OK I stepped further down the rabbit hole. Now I am getting
    NewMEPDRIntakeTest.aspx:968

       Uncaught TypeError: attachmentFolder.get_files is not a function  
    

    Your thoughts?

    0 comments No comments

  2. IndianRich 21 Reputation points
    2022-10-31T16:24:46.62+00:00

    The first thing that happens is the form inputs all of the front data and all of that works separate from the attachment. Adding the attachment is at the end of the form and the app recognizes the input file. Onclick the app goes to the OKAttach() function.
    function OkAttach(){
    att3 = document.getElementById('attachment-file-name');
    var att2 = att3.files[0];
    var att4 = att3.value.split("\");
    att1 = att4[att4.length - 1];
    var reader = new FileReader();
    //reader.onload = function(e){
    console.log('Attachment: ' +att1);
    document.getElementById('idSpace').innerHTML=att1;
    $(document).ready(function(){
    fileInput = $("#attachment-file-name");
    });
    }
    After the data input is saved the onQuerySucceededNew() happens and that's how I get the ID for the attachment folder. If there is an attachment set then it goes to getList30() and this program finds the last ID set.
    function onQuerySucceededNew() {
    if(att1 !== ""){
    console.log('add lineitem: ' +att1);
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getList30);
    //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', registerClick);
    }else if(att1 === ""){
    document.getElementById('main12').style.display="none";
    document.getElementById('main22').style.display="block";
    setTimeout(CloseDlg, 2000);
    }
    }
    function getList30() {
    try {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext .get_web();
    var list = web.get_lists().getByTitle('Issue-Tracker-List');
    var query = '<View Scope=\'RecursiveAll\'>'+
    '<Query>'+
    '<Where>'+
    '<IsNotNull>'+
    '<FieldRef Name=\'LinkTitle\'/>' +
    // '<Value Type=\'Text\'>'+ SDP1 +'</Value>'+
    '</IsNotNull>'+
    '</Where>'+
    '<OrderBy>'+
    '<FieldRef Name=\'ID\' Ascending=\'False\'/>'+
    '</OrderBy>'+
    '</Query>'+
    '</View>';
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(query);
    this.listItems = list.getItems(camlQuery);
    clientContext.load(listItems, 'Include(ID,LinkTitle)');
    clientContext.executeQueryAsync(Function.createDelegate(this, this.querysuccess30), Function.createDelegate(this, this.onQueryFailedAT));
    }
    catch (e) {
    alert(e);
    }
    }
    function querysuccess30(){
    var listItemInfo = '';
    var itemLink = "";
    var listItemEnumerator1 = listItems.getEnumerator();
    var divHTML ='';
    listItemEnumerator1.moveNext();
    var oListItem = listItemEnumerator1.get_current();
    var Intake = oListItem.get_item('ID');
    ID1 = Intake;
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', readFile);
    // });
    }

        function readFile() {  
            //Get File Input Control and read the file name    
            var element = att3;    
            var file = element.files[0];    
            var parts = element.value.split("\\");    
            var fileName = parts[parts.length - 1];    
            //Read File contents using file reader    
            var reader = new FileReader();    
            reader.onload = function(e) {    
                uploadFile(e.target.result, fileName);    
            }    
            reader.onerror = function(e)     
            {    
                alert(e.target.error);    
            }    
            reader.readAsArrayBuffer(file);    
        }    
        function uploadFile(arrayBuffer, att1)     
        {    
            console.log('arrayBuffer: ' +arrayBuffer);  
            console.log('filename: ' +att1);  
            //Get Client Context and Web object.    
            clientContext = new SP.ClientContext();    
            var oWeb = clientContext.get_web();    
    
            //Get list and Attachment folder where the attachment of a particular list item is stored.    
            var oList = oWeb.get_lists().getByTitle('Issue-Tracker-List');    
            var attachmentFolder = 'https://sp-cloud.kp.org/sites/MedicaidRepository/Lists/Issue-Tracker-List/Attachments/' +ID1;  
            var bytes = new Uint8Array(arrayBuffer);    
            var i, length, out = '';    
            for (i = 0, length = bytes.length; i < length; i += 1) {    
                out += String.fromCharCode(bytes[i]);    
            }    
            var base64 = btoa(out);    
            //Create FileCreationInformation object using the read file data    
            createInfo = new SP.FileCreationInformation();    
            createInfo.set_content(base64);    
            createInfo.set_url(att1);   
    
            //Add the file to the list item    
            attachmentFiles = attachmentFolder.get_files().add(createInfo);    
    
            //Load client context and execute the batch    
            clientContext.load(oList);    
            clientContext.load(attachmentFiles);    
            clientContext.executeQueryAsync(QuerySuccessAT, QueryFailureAT);    
        }    
    
        function QuerySuccessAT()     
        {    
            document.getElementById('main12').style.display="none";  
            document.getElementById('main22').style.display="block";  
            console.log("Attachment added successfuly ");   
            setTimeout(CloseDlg, 2000);   
        }    
        function QueryFailureAT(sender, args)     
        {    
            console.log('Request failed with error message - ' + args.get_message() + ' . Stack Trace - ' + args.get_stackTrace());    
        }
    
    0 comments No comments