Is it possible to create field with defined internal name using rest api in sharepoint 2013?
It does not seem possible to specify InternalName
when creating a field using SharePoint REST since InternalName
is ignored.
The workaround would be to:
- Create a field and specify for a
Title
anInternalName
value - Update a field by specifying actual
Title
In that case InternalName
will be preserved.
The following example demonstrates this approach:
function updateJson(endpointUri,payload, success, error)
{
var headers = {};
$.ajax({
url: endpointUri,
type: "POST",
data: JSON.stringify(payload),
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: success,
error: error
});
}
function createJson(endpointUri,payload, success, error)
{
var headers = {};
$.ajax({
url: endpointUri,
type: "POST",
data: JSON.stringify(payload),
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val()
},
success: success,
error: error
});
}
function createListField(webUrl,listTitle,fieldProperties, success,failure)
{
var fieldsUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/fields";
var fieldPayload = {
'__metadata': {'type': 'SP.Field'}
};
var replaceTitle = false;
for(var prop in fieldProperties){
if(prop == 'InternalName' && fieldProperties.hasOwnProperty('Title')) {
fieldPayload['Title'] = fieldProperties[prop];
replaceTitle = true;
}
else {
if(prop == 'Title' && replaceTitle) continue;
fieldPayload[prop] = fieldProperties[prop];
}
}
createJson(fieldsUri,fieldPayload,
function(data){
if(fieldProperties.hasOwnProperty('Title'))
{
var fieldUri = data.d['__metadata']['uri'];
fieldPayload = {
'__metadata': {'type': 'SP.Field'},
'Title': fieldProperties['Title']
};
updateJson(fieldUri,fieldPayload,success,failure);
}
else {
success(data.d);
}
},
failure);
}
Usage
var fieldProperties = {'FieldTypeKind':31,'InternalName':'OfficeLocation','Title':'Office Location'};
createListField(_spPageContextInfo.webAbsoluteUrl,'Documents',fieldProperties,printInfo,logError);
function printInfo()
{
console.log('Field has been created');
}
function logError(error){
console.log(JSON.stringify(error));
}
Not entirely sure what you mean by 'defined internal', gonna take a leap here and assume you mean using a schema definition. Yes you can create a field using a schema in the REST api:
$.ajax({
url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741')/fields",
type: "POST",
data: { '__metadata': { 'type': 'SP.Field' }, 'FieldTypeKind': 20, 'Title':
'Stakeholders', 'SchemaXml': '<Field Type=\"UserMulti\" Required=\"TRUE\"
UserSelectionMode=\"PeopleAndGroups\" UserSelectionScope=\"0\" Mult=\"TRUE\" />'
},
headers: {
"X-RequestDigest": <form digest value>,
"accept": "application/json; odata=verbose",
"content-type": "application/json;odata=verbose",
"content-length": <length of body data>
},
success: successHandler,
error: errorHandler
});
Normal way to define a field in REST:
$.ajax({
url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741')/fields",
type: "POST",
data: "{ '__metadata': { 'type': 'SP.Field' }, 'Title': 'Comments', 'FieldTypeKind': 3
}",
headers: {
"X-RequestDigest": <form digest value>,
"accept": "application/json; odata=verbose",
"content-type": "application/json;odata=verbose",
"content-length": <length of body data>
},
success: successHandler,
error: errorHandler
});
Here is more on the SharePoint fields REST API. Here is a list of the FieldTypeKind enumeration.