Tuesday, March 20, 2012

How to Use "Add To All Content Types" When Creating List Columns with Code

The "Add To All Content Types" functionality can be achieved using the "AddFieldAsXml" method with the "AddToAllContentTypes" option, see below

   SPList currentList = currentWeb.Lists["DocLib"];
   SPField newField = currentList.Fields.CreateNewField(SPFieldType.Text.ToString(), "New Field2");
   currentList.Fields.AddFieldAsXml(newField.SchemaXml, true, SPAddFieldOptions.AddToAllContentTypes);
   currentList.Update();

I found this code and an excellent explanation on this blog http://blogs.microsoft.co.il/blogs/davidbi/archive/2009/01/22/sharepoint-add-a-spfield-to-all-the-content-types-in-a-splist.aspx

No comments:

Post a Comment