Setting default value at library using REST

30 Apr

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 ‘Column default value settings’. 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.

 

When a default value is set there are two things that happen under the SharePoint hood:

  1. A file called client_LocationBasedDefaults.html is created in the Forms folder of the library
  2. The event receiver ‘LocationBasedMetadataDefaultsReceiver ItemAdded’ is attached to the library

The file contains xml which stores the default value set at the field level. An example of the file contents:

<MetadataDefaults><a href=”/teams/testteamsite/TestDefaultValue/DocLib”><DefaultValue FieldName=”MyColumn”>123</DefaultValue></a></MetadataDefaults>

Now I want to accomplish the same thing using REST.
First the file with xml is created and uploaded to the appropriate location using the following piece of code.

var fileCollectionEndpoint = String.format(   "{0}/_api/web/getfolderbyserverrelativeurl('{1}/{2}/forms')/files"
+ "/add(url='{3}',overwrite=true)",
_spPageContextInfo.webServerRelativeUrl,
_spPageContextInfo.webServerRelativeUrl,
list, "client_LocationBasedDefaults.html");

$http({
method: "POST",
url: fileCollectionEndpoint,
data: "<MetadataDefaults><a href=\"" + _spPageContextInfo.webServerRelativeUrl
+ "/" + list + "\"><DefaultValue FieldName=\"MyColumn\">MyValue</DefaultValue></a></MetadataDefaults>",
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $('#__REQUESTDIGEST').val(),
}
} 

In the UI the result is visible immediately and the default value of ‘MyColumn’ is set to ‘MyValue’.

DefaultValue - code

So it seems to do the trick, but since the event receiver is missing, the default value won’t be set when adding a document to the library…

Unfortunately I wasn’t able to add an event receiver using REST…

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’s not possible to set a default value at folder level, but for now an acceptable solution.
DefaultValue - field default value

This can be accomplished by REST:


$http({
    method: "POST",
    url: rootUrl + "/" + url +
"/_api/web/lists/getbytitle('" + listTitle +
"')/fields/getbytitle('" + columnTitle + "')",
    data: "{ '__metadata': { 'type':
'SP.Field' }, 'DefaultValue': '" + itemValue.toString() + "' }",
    headers: {
        "Accept":"application/json;odata=verbose",
        "X-HTTP-Method":"MERGE",
        "content-type":"application/json;odata=verbose",
        "X-RequestDigest":$('#__REQUESTDIGEST').val(),
        "If-Match": "*",
    }
}

Summary

Setting a default value at library level isn’t that easy using REST, because it’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.

One Reply to “Setting default value at library using REST”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.