Is it possible to add an existing site column to a SharePoint list with REST? The MSDN documentation shows that a field can be added using /fields/addField
but all the examples I have seen just create a new field, which I don't want to do. I find it very hard to believe that it is possible to create a new field but not add an existing one.
-
Take a look at this answer– Jonathan HolveyCommented May 16, 2016 at 0:05
Add a comment
|
1 Answer
Sorry for bumping old post. I came across this and I was looking for a more complete answer.
Jonathan is right, you create a 'child' field on the list using the site field as a base.
I did it like this (don't do a blind copy please, headers are not included here):
pro.createListColumnFromXmlSchema = function(currSite, listName, schemaXml){
// Set the option to addFieldInternalNameHint - 8 to ensure the internal name in the schema xml is not altered.
return $.ajax({
url: currSite+"_api/web/lists/GetByTitle('" + listName + "')/fields/createfieldasxml",
type: "POST",
data: JSON.stringify({
"parameters": {
"__metadata": { "type": "SP.XmlSchemaFieldCreationInformation" },
"Options": 8,
"SchemaXml": schemaXml
}
})
});
}
To first get the schemaXml I do this:
pro.addColumnToList = function(currSite,listName,dispName){
var deferred = $.Deferred();
$.get(currSite + "_api/site/RootWeb/Fields/getByTitle('"+dispName+"')/schemaxml").done(function(data){
var schemaXml = data.d.SchemaXml;
pro.createListColumnFromXmlSchema(currSite, listName, schemaXml).done(function(data){
deferred.resolve(data);
}).fail( function(arguments){ deferred.reject(arguments); } );
}).fail( function(arguments){ deferred.reject(arguments); } );
return deferred.promise();
};