I am using the below code
Web rootWeb = clientContext.Site.RootWeb;
rootWeb.Fields.AddFieldAsXml("<Field DisplayName='Session Name' Name='SessionName' ID='"+new GUID()+"' Group='SharePoint Saturday 2014 Columns' Type='Text' />", false, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();
Field session = rootWeb.Fields.GetByInternalNameOrTitle("SessionName");
ContentType sessionContentType = rootWeb.ContentTypes.GetById("0x0100BDD5E43587AF469CA722FD068065DF5D");
sessionContentType.FieldLinks.Add(new FieldLinkCreationInformation
{
Field = session
});
sessionContentType.Update(true);
clientContext.ExecuteQuery();
This creates a site column and adds it to the group, but when I try to attach it to a content type it throws
duplicate "SessionName" found
I also get the below error sometimes
The field specified with name "SessionName" and ID{some-id} is not accessible or does not exist
After creating site column using this code, I am not able to add site column to content type manually!!
It throws the same error