{"id":1891,"date":"2018-04-19T19:13:30","date_gmt":"2018-04-19T17:13:30","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1891"},"modified":"2018-04-19T19:13:30","modified_gmt":"2018-04-19T17:13:30","slug":"remove-custom-properties-metadata-for-an-office-document-javascript","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2018\/04\/19\/remove-custom-properties-metadata-for-an-office-document-javascript\/","title":{"rendered":"Remove custom properties\/metadata for an Office document [javascript]"},"content":{"rendered":"<p>I have this document library on Sharepoint where I have one custom column called <em>Metadata<\/em> and that is a lookup to another list on my Sharepoint.<\/p>\n<p>When the users download an Office document from this library and then re-upload it we could have the below error message:<\/p>\n<blockquote><p>There is at least one lookup column that enforces a relationship behavior and contains values that reference one or more non-existent items in the target list.<\/p><\/blockquote>\n<p>It&#8217;s because the Office documents keep the custom columns from the document library from where they have been downloaded&#8230; In that case the file tries to reassign the <em>Metadata<\/em> with an ID that doesn&#8217;t exist anymore&#8230; causing this issue!<\/p>\n<p>Because I&#8217;m using a homemade interface to upload documents, I&#8217;ve been able to pass some code to delete the file&#8217;s properties on-the-fly before pushing it into the document library.<\/p>\n<p>To do so, you need <a href=\"https:\/\/stuk.github.io\/jszip\/\">JSZip<\/a> that will permit to unzip the Office document in order to retrieve the file <b>docProps\/custom.xml<\/b> and to change the properties we want before the final upload.<\/p>\n<p>Let&#8217;s imagine my page contains an <code>&lt;input type=\"file\"&gt;<\/code>, and that I have already loaded JSZip. Then I use <a href=\"https:\/\/developer.mozilla.org\/fr\/docs\/Web\/API\/FileReader\/readAsArrayBuffer\">FileReader<\/a>:<\/p>\n<pre class=\"brush:javascript\">\r\nvar fileReader = new FileReader();\r\nfileReader.onloadend = function(e) {\r\n  \/\/ content is the \"arraybuffer\" that represents my file\r\n  var content = e.target.result;\r\n  \/\/ check the file's extension (here \"docx\", \"xlsx\", and \"pptx\", but we could add more extensions\r\n  var ext=file.name.split(\".\").slice(-1)[0];\r\n  switch (ext) {\r\n    case \"docx\":\r\n    case \"xlsx\":\r\n    case \"pptx\": {\r\n      \/\/ load content in JSZip\r\n      JSZip.loadAsync(content)\r\n      .then(function(zip) {\r\n        \/\/ unzip the file that contains metadata\/properties\r\n        return zip.file(\"docProps\/custom.xml\").async(\"text\")\r\n        .then(function(txt) {\r\n          \/\/ replace the faulty column\r\n          txt = txt.replace(\/name=\"Metadata\">&lt;vt:lpwstr>\\d+&lt;\\\/vt:lpwstr>\/,'name=\"Metadata\">&lt;vt:lpwstr>&lt;\\\/vt:lpwstr>');\r\n          \/\/ reinject the new file\r\n          zip.file(\"docProps\/custom.xml\", txt);\r\n          return zip.generateAsync({type:\"arraybuffer\"})\r\n        })\r\n      })\r\n      .then(function(content) {\r\n        \/\/ do something with your content\r\n        \/\/ for example (https:\/\/aymkdn.github.io\/SharepointPlus\/): $SP().list(\"my_list\").createFile({content:content, filename:file.name})\r\n      })\r\n      break;\r\n    }\r\n    default: {\r\n      \/\/ for the other files, treat them normally\r\n      \/\/ for example (https:\/\/aymkdn.github.io\/SharepointPlus\/): $SP().list(\"my_list\").createFile({content:content, filename:file.name})\r\n    }\r\n  }\r\n}\r\nfileReader.onerror = function(e) {\r\n  console.error(e.target.error);\r\n}\r\nfileReader.readAsArrayBuffer(file);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have this document library on Sharepoint where I have one custom column called Metadata and that is a lookup to another list on my Sharepoint. When the users download an Office document from this library and then re-upload it we could have the below error message: There is at least one lookup column that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[170,20,33],"tags":[123,24,155,158],"class_list":["post-1891","post","type-post","status-publish","format-standard","hentry","category-english","category-niveau-expert","category-programmation","tag-english","tag-javascript","tag-niveau-expert","tag-programmation"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1891"}],"version-history":[{"count":3,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1891\/revisions"}],"predecessor-version":[{"id":1894,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1891\/revisions\/1894"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}