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

Getting the script folder in PowerShell

Today I needed to call another script file in the same folder as the main script. After a little searching on the web I found "$MyInvocation.MyCommand.Path". "$MyInvocation.MyCommand.Path" returns the path to the current script, With that I was able to get the current folder using Get-Item. This is my test scripts: ToRun.ps1: Write-Host $MyInvocation.MyCommand.Path $scriptDirectory  = (Get-Item $MyInvocation.MyCommand.Path).DirectoryName $scriptName = "HelloWorld.ps1" Write-Host calling $scriptName in $scriptDirectory & ($scriptDirectory + '\' + $scriptName) HelloWorld.ps1: Write-Host "Hello word" After running the script the output was: calling HelloWorld.ps1 in C:\scriptFun Hello word Simple but useful.

Using azure event gird with a function app

Introduction In this tutorial we will set up a simple azure event grid topic, with an function app as a subscriber to the topic. The function app will just log the messages, so we can se that it picked up the event. Setting things up First step is to create a resource group where we can add all the azure services we need az group create --name  myresourcegroup   --location norwayeast We need to set up a storage account that can be used bye the function app. az storage account create --name mystorageaccount --resource-group myresourcegroup --location eastus --sku Standard_LRS Now that we have a storage account we can make the function app. Here we will set it up to be able to run .Net 6 functions az functionapp create --name myFunctionApp --resource-group  myresourcegroup  --consumption-plan-location eastus --runtime dotnet --runtime-version 6.0 --functions-version 4 --storage-account  mystorageaccount...