{"id":2142,"date":"2016-04-30T22:02:58","date_gmt":"2016-04-30T20:02:58","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=2142"},"modified":"2016-05-01T17:20:19","modified_gmt":"2016-05-01T15:20:19","slug":"setting-default-value-at-library-using-rest","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/setting-default-value-at-library-using-rest\/","title":{"rendered":"Setting default value at library using REST"},"content":{"rendered":"<p>A nice feature available at document library level is to set a default value on a field.<br \/>\nThis can be done in the UI by navigating to the document library settings and select &#8216;Column default value settings&#8217;. A default value can be set at a specific folder in the library or at the root folder of the document library as can be seen in the picture below.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-manual.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2146 size-full\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-manual.png\" alt=\"\" width=\"833\" height=\"168\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-manual.png 833w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-manual-300x60.png 300w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-manual-600x121.png 600w\" sizes=\"auto, (max-width: 833px) 100vw, 833px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>When a default value is set there are two things that happen under the SharePoint hood:<\/p>\n<ol>\n<li>A file called client_LocationBasedDefaults.html is created in the Forms folder of the library<\/li>\n<li>The event receiver &#8216;LocationBasedMetadataDefaultsReceiver ItemAdded&#8217; is attached to the library<\/li>\n<\/ol>\n<p>The file contains xml which stores the default value set at the field level. An example of the file contents:<\/p>\n<blockquote><p>&lt;MetadataDefaults&gt;&lt;a href=&#8221;\/teams\/testteamsite\/TestDefaultValue\/DocLib&#8221;&gt;&lt;DefaultValue FieldName=&#8221;MyColumn&#8221;&gt;123&lt;\/DefaultValue&gt;&lt;\/a&gt;&lt;\/MetadataDefaults&gt;<\/p><\/blockquote>\n<p>Now I want to accomplish the same thing using REST.<br \/>\nFirst the file with xml is created and uploaded to the appropriate location using the following piece of code.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar\u00a0fileCollectionEndpoint = String.format( \u00a0 &quot;{0}\/_api\/web\/getfolderbyserverrelativeurl('{1}\/{2}\/forms')\/files&quot;\r\n+\u00a0&quot;\/add(url='{3}',overwrite=true)&quot;,\r\n_spPageContextInfo.webServerRelativeUrl,\r\n_spPageContextInfo.webServerRelativeUrl,\r\nlist,\u00a0&quot;client_LocationBasedDefaults.html&quot;);\r\n\r\n$http({\r\nmethod: &quot;POST&quot;,\r\nurl: fileCollectionEndpoint,\r\ndata: &quot;&lt;MetadataDefaults&gt;&lt;a href=\\&quot;&quot; + _spPageContextInfo.webServerRelativeUrl\r\n+ &quot;\/&quot; + list + &quot;\\&quot;&gt;&lt;DefaultValue FieldName=\\&quot;MyColumn\\&quot;&gt;MyValue&lt;\/DefaultValue&gt;&lt;\/a&gt;&lt;\/MetadataDefaults&gt;&quot;,\r\nheaders: {\r\n&quot;Accept&quot;: &quot;application\/json;odata=verbose&quot;,\r\n&quot;content-type&quot;: &quot;application\/json;odata=verbose&quot;,\r\n&quot;X-RequestDigest&quot;: $('#__REQUESTDIGEST').val(),\r\n}\r\n} <\/pre>\n<p>In the UI the result is visible immediately and the default value of &#8216;MyColumn&#8217; is set to &#8216;MyValue&#8217;.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2145\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-code.png\" alt=\"DefaultValue - code\" width=\"835\" height=\"168\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-code.png 835w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-code-300x60.png 300w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-code-600x120.png 600w\" sizes=\"auto, (max-width: 835px) 100vw, 835px\" \/><\/a><\/p>\n<p>So it seems to do the trick, but since the event receiver is missing, the default value won&#8217;t be set when adding a document to the library\u2026<\/p>\n<p>Unfortunately I wasn&#8217;t able to add an event receiver using REST\u2026<\/p>\n<p>Besides setting a default value by using the option above, there is another option: set a default value at field level. Probably less fancy, because it&#8217;s not possible to set a default value at folder level, but for now an acceptable solution.<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-field-default-value.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2147\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-field-default-value.png\" alt=\"DefaultValue - field default value\" width=\"485\" height=\"638\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-field-default-value.png 485w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2016\/05\/DefaultValue-field-default-value-228x300.png 228w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><\/a><\/p>\n<p>This can be accomplished by REST:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n$http({\r\n\u00a0\u00a0\u00a0 method: &quot;POST&quot;,\r\n\u00a0\u00a0\u00a0 url: rootUrl + &quot;\/&quot; + url +\r\n&quot;\/_api\/web\/lists\/getbytitle('&quot; + listTitle +\r\n&quot;')\/fields\/getbytitle('&quot; + columnTitle + &quot;')&quot;,\r\n\u00a0\u00a0\u00a0 data: &quot;{ '__metadata': { 'type':\r\n'SP.Field' }, 'DefaultValue': '&quot; + itemValue.toString() + &quot;' }&quot;,\r\n\u00a0\u00a0\u00a0 headers: {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;Accept&quot;:&quot;application\/json;odata=verbose&quot;,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;X-HTTP-Method&quot;:&quot;MERGE&quot;,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;content-type&quot;:&quot;application\/json;odata=verbose&quot;,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;X-RequestDigest&quot;:$('#__REQUESTDIGEST').val(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;If-Match&quot;: &quot;*&quot;,\r\n\u00a0\u00a0\u00a0 }\r\n}\r\n<\/pre>\n<h4>Summary<\/h4>\n<p>Setting a default value at library level isn&#8217;t that easy using REST, because it&#8217;s impossible to add an event receiver to the list. At this particular scenario another option is viable: set a default value at field level. This is an easy step to accomplish using REST.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A nice feature available at document library level is to set a default value on a field. This can be done in the UI by navigating to the document library settings and select &#8216;Column default value settings&#8217;. A default value &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/setting-default-value-at-library-using-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,31],"tags":[37,44],"class_list":["post-2142","post","type-post","status-publish","format-standard","hentry","category-office-365","category-sharepoint-2013","category-visual-studio","tag-office365","tag-sharepoint-2013"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2142","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=2142"}],"version-history":[{"count":18,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2142\/revisions"}],"predecessor-version":[{"id":2163,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2142\/revisions\/2163"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=2142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=2142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=2142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}