Setting a lookup column’s default value in SharePoint Online

For a recent project I needed a lookup column that was hidden and defaulted to a specific list item. On premise this is simple to achieve:

  • Use SharePoint Manager 2013 to set the default value, format of the value needs to be ID;#Title e.g. 1;#Details
  • Set the lookup column to hidden within the site content type – This is a must, otherwise the UI will override the default value

For SharePoint Online I decided to use SharePoint’s REST API to set the lookup column’s default value. To save anyone else from spending time on something that should be simple I’ve created a simple tool for updating the default value of site columns:

Usage

  1. Copy and paste the code in this post into a script editor web part
  2. Enter the display name of the (site) lookup column into the Column Name field
  3. Enter the ID of the list item that the lookup column should default to within the Default Value field
  4. Click Update Column
  5. Check the result by modifying the following rest call to match your site and column, then paste it into the address bar – https://Your Site/_api/web/fields/getbytitle(‘Your Column‘)/defaultvalue

The tool will use Sharepoint’s  status bar to provide the outcome, as shown below:

DefaultValue

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
var statusId = '';
var notifyId = '';
function updateColumn(){
	SP.UI.Status.removeAllStatus(true);
	var colName = document.getElementById("siteColumn").value;
	var defaultValue = document.getElementById("defaultValue").value;
	var webUrl = _spPageContextInfo.webAbsoluteUrl
	if (colName.length == 0 || defaultValue.length == 0){
    	statusId = SP.UI.Status.addStatus("Ensure you have populated both the Column Name & Default values then try again");
    	SP.UI.Status.setStatusPriColor(statusId, 'red');
		}
	else{	
		$.ajax({
  		url: webUrl + "/_api/web/fields/getbytitle('" + colName +"')",
  		type: "POST",
  		data: JSON.stringify({
            "__metadata": { type: "SP.Field" },
            DefaultValue: defaultValue
        }),
          headers: { 
    		"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
    		"content-type": "application/json;odata=verbose",
    		"X-HTTP-Method": "MERGE"
  		},
  		success: successHandler,
  		error: errorHandler
		});
		function successHandler(){
		statusId = SP.UI.Status.addStatus("Yep we worked");
    	SP.UI.Status.setStatusPriColor(statusId, 'green');
		}
		function errorHandler(error){
		statusId = SP.UI.Status.addStatus("Failed - " + error.statusText);
    	SP.UI.Status.setStatusPriColor(statusId, 'red');
		}
	}
}
</script>
<table cellpadding="5">
<tr>
<th>Column Name (Display Name)</th><th>Default Value</th>
</tr>
<tr><td><input id="siteColumn" ></td><td><input id="defaultValue" ></td></tr>
<tr><td><input style="margin-left:0px;" id="goButton" type="button" value="Update Column" onclick="javascript:updateColumn();"></td><td></td></tr>
</table>