Change the value of a SharePoint Choice field on a custom page
- Posted by: Hayden Sinclair
You can change the value of a SharePoint Choice field in a customised SharePoint page (i.e. a new form page created in SharePoint Designer).
This post describes how to change the form status from Awaiting Approval to Approved on a customised ApproveForm.aspx page and how to set the Approver to the current user at the same time.
Instructions
Copy the script below and paste it into Notepad. This script will swap out your drop down choice and change the value of a people picker field.
Swap out the highlighted fields. The blue highlighted field is for your choice field. The orange highlighted field is for your People Picker field.
Be sure to swap out the options on the green highlighted field with your status options, and change the selected option for the value you want to default to.
<script type="text/javascript">
var context = null;
var web = null;
var currentUser = null;
ExecuteOrDelayUntilScriptLoaded(GetUserLoginName, "sp.js");
function GetUserLoginName() {
context = new SP.ClientContext.get_current();
web = context.get_web();
this._currentUser = web.get_currentUser();
_currentStatus ='<option selected="Selected" value="Approved">Approved</option><option value="Rejected">Rejected</option><option value="Withdrawn">Withdrawn</option>';
context.load(this._currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
Function.createDelegate(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args) {
document.getElementById('ctl00_ctl35_g_5c17d614_9a6a_475f_9ba4_a84c4b475818_ff91_ctl00_DropDownChoice').innerHTML=_currentStatus;
document.getElementById('ctl00_ctl35_g_5c17d614_9a6a_475f_9ba4_a84c4b475818_ff81_ctl00_ctl00_UserField_upLevelDiv').innerHTML=this._currentUser.get_title();
}
function onFaiureMethod(sender, args) {
alert('request failed' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
Go to the ApproveForm.aspx page in Internet Explorer
Click Cog > Edit page
Click Add a Web Part
Choose Media and Content > Script Editor
Click Add
Click Edit Snippet, paste in your code from Notepad, and click Insert
Select the Page Ribbon, then click Stop Editing