I tried creating a content type using code, and when I deploy I got this error, however when I go to the site, the content type is created,
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = null;
if (properties.Feature.Parent is SPSite)
{
SPSite sites = (SPSite)properties.Feature.Parent;
web = sites.RootWeb;
}
else
{
web = (SPWeb)properties.Feature.Parent;
}
if (web == null)
return;
/* CREATE SITE COLUMNS */
string columnGroup = "Financial Columns";
// Amount
string amountFieldName = web.Fields.Add("Amount", SPFieldType.Currency, false);
SPFieldCurrency amountField = (SPFieldCurrency)web.Fields.GetFieldByInternalName(amountFieldName);
amountField.Group = columnGroup;
amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
amountField.MinimumValue = 0;
amountField.Update();
// Client Name
string clientFieldName = web.Fields.Add("Client Name", SPFieldType.Text, false);
SPFieldText clientField = (SPFieldText)web.Fields.GetFieldByInternalName(clientFieldName);
clientField.Group = columnGroup;
clientField.Update();
// Date Opened
string dateOpenedFieldName = web.Fields.Add("Date Opened", SPFieldType.DateTime, false);
SPFieldDateTime dateOpenedField = (SPFieldDateTime)web.Fields.GetFieldByInternalName(dateOpenedFieldName);
dateOpenedField.Group = columnGroup;
dateOpenedField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
dateOpenedField.DefaultValue = "[today]";
dateOpenedField.Update();
// Cost Center Name
string costCenterFieldName = web.Fields.Add("Cost Center", SPFieldType.Choice, false);
SPFieldChoice costCenterField = (SPFieldChoice)web.Fields.GetFieldByInternalName(costCenterFieldName);
costCenterField.Choices.Add("Administration");
costCenterField.Choices.Add("Information Services");
costCenterField.Choices.Add("Facilities");
costCenterField.Choices.Add("Operations");
costCenterField.Choices.Add("Sales");
costCenterField.Choices.Add("Marketing");
costCenterField.Group = columnGroup;
costCenterField.Update();
/* CREATE SITE CONTENT TYPES */
string contentTypeGroup = "Financial Content Types";
// Get a content type to be the parent of a new Financial Document content type.
SPContentType documentCType = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];
// Create the Financial Document content type.
SPContentType financialDocumentCType = new SPContentType(documentCType, web.ContentTypes, "Financial Document");
// Note: A content type is not initialized until after it is added.
financialDocumentCType = web.ContentTypes.Add(financialDocumentCType);
financialDocumentCType.Group = contentTypeGroup;
// Add the Date Opened column. Child content types inherit the column.
SPFieldLink dateOpenedFieldRef = new SPFieldLink(dateOpenedField);
dateOpenedFieldRef.Required = true;
financialDocumentCType.FieldLinks.Add(dateOpenedFieldRef);
// Add the Amount column. Child content types inherit the column.
SPFieldLink amountFieldRef = new SPFieldLink(amountField);
financialDocumentCType.FieldLinks.Add(amountFieldRef);
// Commit changes.
financialDocumentCType.Update();
// Create the Invoice content type.
SPContentType invoiceCType = new SPContentType(financialDocumentCType, web.ContentTypes, "Invoice");
invoiceCType = web.ContentTypes.Add(invoiceCType);
invoiceCType.Group = contentTypeGroup;
// Modify the Title column inherited from the parent.
SPFieldLink serviceFieldRef = invoiceCType.FieldLinks[SPBuiltInFieldId.Title];
serviceFieldRef.DisplayName = "Service";
serviceFieldRef.Required = true;
// Add the Client column.
SPFieldLink clientFieldRef = new SPFieldLink(clientField);
clientFieldRef.Required = true;
invoiceCType.FieldLinks.Add(clientFieldRef);
// Specify a document template.
invoiceCType.DocumentTemplate = "Invoice.docx";
// Commit changes.
invoiceCType.Update();
// Create the Purchase Order content type.
SPContentType purchaseOrderCType = new SPContentType(financialDocumentCType, web.ContentTypes, "Purchase Order");
purchaseOrderCType = web.ContentTypes.Add(purchaseOrderCType);
purchaseOrderCType.Group = contentTypeGroup;
// Modify the Title column inherited from the parent.
SPFieldLink itemFieldRef = purchaseOrderCType.FieldLinks[SPBuiltInFieldId.Title];
itemFieldRef.DisplayName = "Item";
itemFieldRef.Required = true;
// Add the Department column.
SPFieldLink departmentFieldRef = new SPFieldLink(costCenterField);
departmentFieldRef.DisplayName = "Department";
departmentFieldRef.Required = true;
purchaseOrderCType.FieldLinks.Add(departmentFieldRef);
// Specify a document template.
purchaseOrderCType.DocumentTemplate = "PurchaseOrder.docx";
// Commit changes.
purchaseOrderCType.Update();
}
content-type,feature-activation. – Alex Angas Sep 19 '10 at 23:16