SPSite site = new SPSite("http://sp2010:21016");
SPWeb web = site.RootWeb;
//SPWeb web = site.OpenWeb();
//foreach (SPContentType ct in web.ContentTypes)
//{
// Console.WriteLine(ct.Name+"\t-->"+ct.Group);
//}
//return;
SPContentTypeId contentTypeID = SPBuiltInContentTypeId.Item;
SPContentType cType = new SPContentType(web.ContentTypes[contentTypeID],
web.ContentTypes, "SalesCT1");
cType.Description = "Sales data content type";
cType.Group = "Company-Sales-Data";
cType.Sealed = true;
cType.Hidden = false;
web.ContentTypes.Add(cType);
//adding 4 fields 1.sales man 2.yr 2005,2006,2007
string fieldname=web.Fields.Add("Sales Man", SPFieldType.Text, false);
//first add to web field collection then link to content type.
SPField field= web.Fields.GetField(fieldname);
SPFieldLink fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
fieldname = web.Fields.Add("2005", SPFieldType.Number, false);
field = web.Fields.GetField(fieldname);
fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
fieldname = web.Fields.Add("2006", SPFieldType.Number, false);
field = web.Fields.GetField(fieldname);
fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
fieldname = web.Fields.Add("2007", SPFieldType.Number, false);
field = web.Fields.GetField(fieldname);
fieldLink = new SPFieldLink(field);
cType.FieldLinks.Add(fieldLink);
cType.Update();
No comments:
Post a Comment