Gowtham

Gowtham

  • NA
  • 516
  • 38.1k

How to site column to content type using js

Mar 2 2017 9:23 AM
So for I have tried.The code which is referenced form other website.
 
function AddContentType()
{
var ctx = new SP.ClientContext.get_current();
var filename="companybranch";
var field = ctx.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle("Company_x0020_Branches");
var ctype = ctx.get_site().get_rootWeb().get_contentTypes().getById("0x01005B20AF5B57EE9045BBF0C148A600E681");
ctx.load(ctype);
ctx.load(field);
var createInfo = new SP.FieldLinkCreationInformation();
createInfo.set_field(field);
var fieldLink = ctype.get_fieldLinks().add(createInfo);
ctype.update (true);
ctx.load(fieldLink);
ctx.executeQueryAsync(success, failure);
}
function success()
{
alert('success');
}
function failure()
{
alert('failure');
}
Over all code is woking good.
But this line.
var field = ctx.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle("Company");
 
-- column name was only adding to content type which was already presented in share point site.if we are passing anonymous name it shows failure.
Please let me know how to set the column and fields to add to content type in share point site using java script.