{"id":2131,"date":"2016-03-31T21:55:15","date_gmt":"2016-03-31T19:55:15","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=2131"},"modified":"2016-04-01T16:02:18","modified_gmt":"2016-04-01T14:02:18","slug":"create-a-document-set-using-jsom-and-rest","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/create-a-document-set-using-jsom-and-rest\/","title":{"rendered":"Create a document set using JSOM and REST"},"content":{"rendered":"<p>To create a document set in a document library its necessary to add a Document Set content type to the library. After doing this a document set can be created using various techniques. In this post I&#8217;ll show you how to do so using JSOM or REST.<\/p>\n<h4>JSOM<\/h4>\n<p>To use JSOM the appropriate references have to be added to the HTML, but the most important one is SP.DocumentManagement.js. Of course a nice UI can be set up to pass in values for necessary input parameters, but for demo purposes filled in variables are used as shown below.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar url = &quot;&quot;;\r\nvar listTitle = &quot;&quot;;\r\nvar docSetContentTypeID = &quot;&quot;;\r\nvar docSetName = &quot;&quot;;\r\n\r\nvar context = new SP.ClientContext(url);\r\nvar web = context.get_web();\r\nvar list = web.get_lists().getByTitle(listTitle);\r\ncontext.load(list);\r\n\r\nvar parentFolder = list.get_rootFolder();\r\ncontext.load(parentFolder);\r\n\r\nvar docSetContentType = context.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);\r\ncontext.load(docSetContentType);\r\n\r\ncontext.executeQueryAsync(\r\n  function () {\r\n    SP.DocumentSet.DocumentSet.create(context, parentFolder, docSetName, docSetContentType.get_id());\r\n    context.executeQueryAsync(\r\n    function () {\r\n      logtoconsole(&quot;document set created&quot;);\r\n    },\r\n    function (sender, args) {\r\n      logtoconsole(&quot;document set error&quot;);\r\n    }\r\n    );\r\n  }\r\n);\r\n\r\n<\/pre>\n<p>That&#8217;s all there is!<\/p>\n<h4>REST<\/h4>\n<p>I found it more of a challenge using REST to add a document set to a library, because I couldn&#8217;t find any appropriate SharePoint 2013 REST endpoint to use.<\/p>\n<p>I tried for example a POST to &#8216;\/_api\/web\/folders&#8217; with { &#8216;__metadata&#8217;: { &#8216;type&#8217;: &#8216;SP.Folder&#8217; }, &#8216;ServerRelativeUrl&#8217;: &#8216;Documents\/test1&#8217; , &#8216;ContentTypeId&#8217;: &#8216;0x0120D520009403DDAFA2D9F54E885F81B4DA488BA00101&#8217;}<br \/>\nThe following message was returned as result:<\/p>\n<blockquote><p>&#8216;The property &#8216;ContentTypeId&#8217; does not exist on type &#8216;SP.Folder&#8217;. Make sure to only use property names that are defined by the type.&#8217;<\/p><\/blockquote>\n<p>So I wasn&#8217;t able to specify a content type id when adding a folder, assuming a document set is a special type of folder.<\/p>\n<p>Another option could be trying to add an item using a POST method to &#8216;\/_api\/web\/lists\/getbytitle(&#8216;&#8221; + listTitle + &#8220;&#8216;)\/items with\u00a0 { &#8216;__metadata&#8217;: { &#8216;type&#8217;: &#8216;SP.Data.Gedeelde_x0020__x0020_documentenItem&#8217; } , &#8216;ContentTypeId&#8217;: &#8216;0x0120D520009403DDAFA2D9F54E885F81B4DA488BA00101&#8217; };<br \/>\nThe result was:<\/p>\n<blockquote><p>&#8216;To add an item to a document library, use SPFileCollection.Add()&#8217;<\/p><\/blockquote>\n<p>The only way I could find was to use the good old (SP2010) listdata service\u2026<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$http({\r\n\u00a0 method: &quot;POST&quot;,\r\n\u00a0 url: url + &quot;\/_vti_bin\/listdata.svc\/&quot; + listTitle,\r\n\u00a0 data: JSON.stringify(docSetOptions),\r\n\u00a0 headers: {\r\n\u00a0 \u00a0 &quot;Accept&quot;: &quot;application\/json;odata=verbose&quot;,\r\n\u00a0 \u00a0 &quot;content-type&quot;: &quot;application\/json;odata=verbose&quot;,\r\n\u00a0 \u00a0 &quot;X-RequestDigest&quot;: $('#__REQUESTDIGEST').val(),\r\n\u00a0 \u00a0 &quot;Slug&quot;: _spPageContextInfo.siteServerRelativeUrl + &quot;\/&quot; + url + docSetOptions.Path + &quot;\/&quot; + docSetOptions.Title + &quot;|&quot; + docSetOptions.ContentTypeId,\r\n\u00a0 }\r\n}).success(function (data, status, headers, config) {\r\n\u00a0 logtoconsole(&quot;document set created&quot;);\r\n}).error(function (data, status, headers, config) {\r\n\u00a0 logtoconsole(&quot;document set error&quot;);\r\n});\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>With docsetOptions declared like:<br \/>\nvar docSetOptions = {<br \/>\n&#8216;Title&#8217;: myTitle,<br \/>\n&#8216;Path&#8217;: &#8216;\/Documents&#8217;,<br \/>\n&#8216;ContentTypeId&#8217;: contentTypeId,<br \/>\n&#8216;ContentType&#8217;: contentType};<\/p>\n<h4>Summary<\/h4>\n<p>Creating a document set using CSOM or REST can be done, sometimes with a nice challenge!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a document set in a document library its necessary to add a Document Set content type to the library. After doing this a document set can be created using various techniques. In this post I&#8217;ll show you how &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/create-a-document-set-using-jsom-and-rest\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,35],"tags":[37,44],"class_list":["post-2131","post","type-post","status-publish","format-standard","hentry","category-office-365","category-sharepoint-2013","tag-office365","tag-sharepoint-2013"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=2131"}],"version-history":[{"count":9,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2131\/revisions"}],"predecessor-version":[{"id":2140,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2131\/revisions\/2140"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}