Stumbles and Travels


Programmatically Creating a Content Type and Site Column
August 18, 2007, 3:43 pm
Filed under: MOSS

In this post I show how you can create a content type programmatically and then create a new site column and add it to your new content type.

using(SPSite site = new SPSite(urlToSite))
{
   using(SPWeb web = site.OpenWeb())
   {
      SPContentType contentType =
         new SPContentType(web.AvailableContentTypes["Parent Name"],
         web.ContentTypes, “Name”);

      // use true below to make required field
      web.Fields.Add(“NameOfColumn”, SPFieldType.TypeOfField, true);
      SPFieldLink fieldLink = new SPFieldLink(web.Fields["NameOfColumn"]);
      contentType.FieldLinks.Add(fieldLink);
      contentType.Update();
   }
}


3 Comments so far
Leave a comment

i want to give you huge thanks for this effort

wael mohamed software developer from egypt

Comment by wael

This is very useful and rare post.

Comment by Abhishek Agrawal

Loyola says : I absolutely agree with this !

Comment by Loyola




Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>