Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();

    }
share|improve this question
1  
There's a lot of code here and the error could be anywhere... Have you tried using a debugger to pinpoint the problem? Please try that and edit your question with more information. Also remember to add good tags so ppl can find your question, e.g. content-type, feature-activation. – Alex Angas Sep 19 '10 at 23:16
the error is deploy, if i compile it compiles ok, but when I press f5 to deploy I got that error,it does not even attach the debugger! – Luis Valencia Sep 21 '10 at 2:55

closed as too localized by Alex Angas Jun 6 '11 at 2:42

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.