Skip to main content

Upload dokument jsom notes


Code I used to upload a document into a list based on search context in a displaytemplate

 upLoadDocument: function(itemId, produkt) {
               doSpAction(function(){
                  var inputFile = document.getElementById(itemId + "upload");
                  var btnUpLoad = $("#" + itemId + "btnupload");
                  var fileNameSpan = $("#" + itemId + "filename");
                  var uploadPnl = $("#" + itemId + "uploadPnl");
                  var doUpload = $("#" + itemId + "doUpload");
                 
                 
                  console.log(btnUpLoad);
                 
                  $(inputFile).change(function() {
                     console.log("inputFile changed: " + inputFile.value);
                     fileNameSpan.html(inputFile.value);
                     uploadPnl.addClass("visible");
                     doUpload.hide();
                  });
                 
                 
                  btnUpLoad.click(fileClicked);
                  function fileClicked() {

                  var fileData = inputFile.files[0];
                  var fileReader = new FileReader();
                 
                  fileReader.onload = upLoadFile;
                  fileReader.readAsArrayBuffer(fileData);
                 
                 
                  function upLoadFile(data) {
                     
                      var bytes = new Uint8Array(data.target.result);
                      var fileContent = "";
                      var l = bytes.length;
                      for(var i = 0; i < length; i++) {
                         fileContent += String.fromCharCode(bytes[i]);
                      };                
                 
                     var clientContext = get_clientContextCurrent();
                     var documentList = clientContext.get_web().get_lists().getByTitle("Documents");
                     var date = new Date();
                     var tick = date.getTime();
                   
                     console.log("File name:" + fileData.name);
                     var fileCreationInfo = new SP.FileCreationInformation();
                     fileCreationInfo.set_url(tick + "_"+ fileData.name);
                     fileCreationInfo.set_overwrite(true);
                     fileCreationInfo.set_content(fileContent);
                   
                     var uploadedFile = documentList.get_rootFolder().get_files().add(fileCreationInfo);
                   
                     var item = uploadedFile.get_listItemAllFields();
                   
                     var siteIdD = getSiteValues();
                   
                     var currentUserIdD = getCurrentUserId();
                   
                     currentUserIdD.done(loadUserDone);
                   
                     var currentUserId;
                   
                     function loadUserDone(userId) {
                        console.log("User id ");
                        console.log(userId);

                        currentUserId = userId;
                        siteIdD.done(loadingSiteIdDone);
                     }
                   
                    function loadingSiteIdDone(siteId) {
                      item.set_item("Foretak", siteId);
                      item.set_item("Produkt", produkt);
                      item.set_item("Mottaker", currentUserId);
                      item.update();
                   
                      clientContext.load(uploadedFile);
                      clientContext.executeQueryAsync(function()
                      {
                         console.log("File uploaded");
                         window.location = "url";                    
                      }, Function.createDelegate(this, listFailedToLoad));
                    };
                   
                   
                 };
                 
                 function listFailedToLoad() {
                   console.log(
                      "Request failed: " + arguments[1].get_message());
                 };
             
                }
                                 
               });
        },

Comments

Popular posts from this blog

CSOM System Update that does not update minor version number

I was in a scenario where I had to do a add some data to a document list in a web job. My issue was that I could not find any system update method in the CSOM API. After searching it i found a blog post showing that one could just override the system fields, like Modified and Editor(Modified My). That seemed to work but that did not stop it form updating the version number of the document. This example will only work for minor unpublished version. I have not found a way to do this without a published version. There it will get a new minor version. The solution combines the use of adding Editor, and Modified with the use of the ValidateUpdateListItem metod. I found that one can set the item values on the item and just set one value in the form value list, then it will update all the fields. If one then set the "bNewDocumentUpdate" it will not update the version number if it is a minor version. So what i do is just get the values from the item before an update, then sets...

User a service bus to connect to debug remove event receivers.

I was trying to follow Kirk Evans blog http://blogs.msdn.com/b/kaevans/archive/2014/02/26/attaching-remote-event-receivers-to-lists-in-the-host-web.aspx on how to create and debug a remote event receiver. I get to the point where one uses a service bus, I created the service bus got the connection string. But I got this error @"Error 1         CorrelationId: e47f76f0-ea2f-4e47-a663-11551dce10aa         ErrorDetail: The remote event receiver callout failed.         ErrorType: Transient         ErrorTypeName: Intermittent         ExceptionMessage: There was no endpoint listening at [ServiceBus++Url]/AppEventReceiver.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.       ...