{"id":808,"date":"2011-12-21T17:33:19","date_gmt":"2011-12-21T16:33:19","guid":{"rendered":"http:\/\/blog.kodono.info\/wordpress\/?p=808"},"modified":"2013-04-05T10:39:41","modified_gmt":"2013-04-05T08:39:41","slug":"create-a-file-into-sharepoint-document-librairies-with-the-copy-asmx-web-service-javascript","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2011\/12\/21\/create-a-file-into-sharepoint-document-librairies-with-the-copy-asmx-web-service-javascript\/","title":{"rendered":"Create a file into Sharepoint document librairies with the Copy.asmx web service [Javascript]"},"content":{"rendered":"<div lang=\"en\"><strong><em>This is the English version of my previous article<\/em><\/strong><\/p>\n<p><strong>EDIT:<\/strong> I&#8217;ve created a JavaScript API for Sharepoint that handles the file creation. Just look at <a href=\"http:\/\/aymkdn.github.com\/SharepointPlus\/symbols\/%24SP%28%29.html#.createFile\">http:\/\/aymkdn.github.com\/SharepointPlus\/symbols\/%24SP%28%29.html#.createFile<\/a><\/p>\n<p>Did you know it&#8217;s possible to create a file from scratch and to add it into a shared documents library of Sharepoint, and only with Javascript ?<\/p>\n<p>To do it we&#8217;ll use the &#8220;copy.asmx&#8221; web service with the &#8220;CopyIntoItems&#8221; function.<br \/>\nIf you check <strong>http:\/\/your_sharepoint\/_vti_bin\/copy.asmx?op=CopyIntoItems<\/strong> you&#8217;ll see that we have several details about the &#8220;CopyIntoItems&#8221; function. Unfortunately it&#8217;s very difficult to find any information regarding this on the Web, and specially for Javascript&#8230;<\/p>\n<p>So, here is the solution to create a file, e.g. an Excel file (.xls) with a regular HTML table, and we&#8217;re going to save it to this library: &#8220;http:\/\/your_sharepoint\/Shared Documents\/&#8221; (Note: I&#8217;m using jQuery for the AJAX requests).<\/p>\n<pre class=\"brush: javascript\">\r\n\/\/ The file content must be encoded into Base64. To do it I use the function available on my blog (http:\/\/blog.kodono.info\/wordpress\/2011\/07\/27\/midi-code-encoder-decoder-en-base64-pour-javascript-programmation\/)\r\nfunction encode_b64(a,b,c,d,e,f){b=\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/\";c='=';for(d=f='';e&#038;=3,a.charAt(d++)||(b=';=',e);f+=b.charAt(63&#038;c>>++e*2))c=c&lt;&lt;8|a.charCodeAt(d-=!e);return f}\r\n\r\n\/\/ \"Upload\" is the name of our function to do the job\r\n\/\/ txtContent is a plain text, the content of our file\r\n\/\/ destinationUrl is the full path URL to the document library (with the filename included)\r\nfunction Upload(txtContent, destinationUrl) {\r\n    var jsStream = encode_b64(txtContent);\r\n    var soapEnv  = &quot;&lt;?xml version=\\&quot;1.0\\&quot; encoding=\\&quot;utf-8\\&quot;?>&quot;\r\n                  +&quot;&lt;soap:Envelope xmlns:xsi=\\&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance\\&quot; xmlns:xsd=\\&quot;http:\/\/www.w3.org\/2001\/XMLSchema\\&quot; xmlns:soap=\\&quot;http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\\&quot;>&quot;\r\n                  +&quot;&lt;soap:Body>&quot;\r\n                  +&quot;&lt;CopyIntoItems xmlns=\\&quot;http:\/\/schemas.microsoft.com\/sharepoint\/soap\/\\&quot;>&quot;\r\n                  +&quot;&lt;SourceUrl>http:\/\/null&lt;\/SourceUrl>&quot;\r\n                  +&quot;&lt;DestinationUrls>&lt;string>&quot;+destinationUrl+&quot;&lt;\/string>&lt;\/DestinationUrls>&quot;\r\n                  +&quot;&lt;Fields>&lt;FieldInformation Type=&apos;File&apos; \/>&lt;\/Fields>&quot;\r\n                  +&quot;&lt;Stream>&quot;+jsStream+&quot;&lt;\/Stream>&quot;\r\n                  +&quot;&lt;\/CopyIntoItems>&quot;\r\n                  +&quot;&lt;\/soap:Body>&quot;\r\n                  +&quot;&lt;\/soap:Envelope>&quot;;\r\n    jQuery.ajax({\r\n        url: &quot;http:\/\/your_sharepoint\/_vti_bin\/copy.asmx&quot;,\r\n        type: &quot;POST&quot;,\r\n        dataType: &quot;xml&quot;,\r\n        data: soapEnv,\r\n        beforeSend: function(xhr) { xhr.setRequestHeader(&apos;SOAPAction&apos;, &apos;http:\/\/schemas.microsoft.com\/sharepoint\/soap\/CopyIntoItems&apos;); },\r\n        contentType: &quot;text\/xml; charset=\\&quot;utf-8\\&quot;&quot;\r\n    });\r\n}\r\n\r\nUpload(\"&lt;html>&lt;table>&lt;tr>&lt;th>Colonne 1&lt;\/th>&lt;th>Colonne B&lt;\/th>&lt;\/tr>&lt;tr>&lt;td>Total:&lt;\/td>&lt;td>1500&lt;\/td>&lt;\/tr>&lt;\/table>&lt;\/html>\",\"http:\/\/your_sharepoint\/Shared Documents\/test.xls\");\r\n<\/pre>\n<p>Then, when you&#8217;ll click on the new &#8220;test.xls&#8221; file, your web browser will want to open it with Excel. MS Excel should show you a warning message, but just click YES and you&#8217;ll see your table inside the sheet !<\/p>\n<p>Of course you can create any type of files (.txt, .jpg, .doc, &#8230;) and with any content.<\/p>\n<p>Related links:<\/p>\n<ul>\n<li><a href=\"http:\/\/blog.stuartwhiteford.com\/?p=86\">http:\/\/blog.stuartwhiteford.com\/?p=86<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/7976329\/sharepoint-copyintoitems-soap-message-in-objective-c\">http:\/\/stackoverflow.com\/questions\/7976329\/sharepoint-copyintoitems-soap-message-in-objective-c<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/copy.copy.copyintoitems%28v=office.12%29.aspx\">http:\/\/msdn.microsoft.com\/en-us\/library\/copy.copy.copyintoitems%28v=office.12%29.aspx<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is the English version of my previous article EDIT: I&#8217;ve created a JavaScript API for Sharepoint that handles the file creation. Just look at http:\/\/aymkdn.github.com\/SharepointPlus\/symbols\/%24SP%28%29.html#.createFile Did you know it&#8217;s possible to create a file from scratch and to add it into a shared documents library of Sharepoint, and only with Javascript ? To do [&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":[13,33],"tags":[123,24,124,117],"class_list":["post-808","post","type-post","status-publish","format-standard","hentry","category-niveau-intermediaire","category-programmation","tag-english","tag-javascript","tag-programming","tag-sharepoint"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/808","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=808"}],"version-history":[{"count":6,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":1283,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/808\/revisions\/1283"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}