SP.ClientContext.castTo method (sp.js)
Casts the specified client object to a derived type.
Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013
SP.ClientContext.castTo(obj, type)
Parameters
obj
SP.ClientObject
The client object to cast to the specified type.type
The type to cast the specified client object to.
Return value
An object of the specified type.
Remarks
The castTo method is inherited from the SP.ClientRuntimeContext object.
Example
The following code example shows how to cast an SP.Field client object to an SP.FieldNumber client object.
function addFieldToList(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
this.oField = oList.get_fields().addFieldAsXml(
'<Field DisplayName=\'MyField\' Type=\'Number\' />',
true,
SP.AddFieldOptions.defaultValue
);
var fieldNumber = clientContext.castTo(oField,SP.FieldNumber);
fieldNumber.set_maximumValue(100);
fieldNumber.set_minimumValue(35);
fieldNumber.update();
clientContext.load(oField);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded() {
var result = oField.get_title() + ' added.';
alert(result);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}