Creating custom content type using an object model

In the previous recipe, we extended a content type using an XML schema. This can get pretty difficult as Visual Studio does not provide ways to debug the XML schemas. Instead of using the xml schema method, you can always use object model APIs to create the content type. In this recipe we will do just that.

For this recipe, we will create a new content type called Project Proposal that has four custom columns Amount, Department, Project Start Date, and Project End Date. The Department field is a choice type where you can select the department to which the project belongs. The Amount field is of currency type and the Project Start Date and the Project End Date are of DateTime type. The content type will be inherited from the document content type.

Getting ready

You should have completed the previous recipes successfully.

How to do it...

  1. Launch your Visual Studio 2010 IDE as an administrator (right-click the shortcut and select Run as administrator).
  2. Select File | New | Project. The new project wizard dialog box will be displayed (make sure to select .NET Framework 3.5 in the top drop-down box).
  3. Select Empty SharePoint Project under Visual C# | SharePoint | 2010 node from the Installed Templates section on the left-hand side.
  4. Name the project ProjectProposal and provide a directory location where you want to save the project and click OK to proceed to the next step in the wizard.
  5. By default, Visual Studio selects the SharePoint site available on the machine. Select Deploy as Farm Solution and click on Next to proceed to the next step in the wizard.
  6. This should create an empty SharePoint project. To this project, add a feature by right-clicking on the Feature folder.
  7. Add an event receiver to the new feature added by right-clicking on the feature and selecting Add Event Receiver.
  8. This should add a code file named Feature1.EventReceiver.cs. Uncomment FeatureActivated and FeatureDeactivating methods.
  9. Add the code to create a Project Proposal content type in the FeatureActivated method. The code is as follows:
    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;
    string columnGroup = "Chapter3 Project Proposal Site Column Group";
    // Project Amount
    string sAmountFieldName = web.Fields.Add("Chapter3 Project Amount", SPFieldType.Currency, false);
    obect modelused, for creating custom content typeSPFieldCurrency fldAmount = (SPFieldCurrency) web.Fields.GetFieldByInternalName(sAmountFieldName);
    fldAmount.Group = columnGroup;
    fldAmount.DisplayFormat = SPNumberFormatTypes. TwoDecimals;
    fldAmount.MinimumValue = 0;
    fldAmount.Update();
    Guid fldGuid = fldAmount.Id;
    // Project Start Date
    string sProjectDateFieldName = web.Fields.Add("Chapter3 Project Start Date", SPFieldType.DateTime, false);
    SPFieldDateTime fldProjectStartDate = (SPFieldDateTime)web.Fields.GetFieldByInternalName(sProjectDateFieldName);
    fldProjectStartDate.Group = columnGroup;
    fldProjectStartDate.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
    fldProjectStartDate.DefaultValue = "[today]";
    fldProjectStartDate.Update();
    // Project End Date
    string sProjectEndDateFieldName = web.Fields.Add("Chapter3 Project End Date", SPFieldType.DateTime, false);
    SPFieldDateTime fldProjectEndDate = (SPFieldDateTime)web.Fields.GetFieldByInternalName(sProjectEndDateFieldName);
    fldProjectEndDate.Group = columnGroup;
    fldProjectEndDate.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
    fldProjectEndDate.DefaultValue = "[today]";
    fldProjectEndDate.Update();
    // Department
    string sDepartmentFieldName = web.Fields.Add("Chapter3 Department", SPFieldType.Choice, false);
    SPFieldChoice fldDepartment = (SPFieldChoice)web. Fields.GetFieldByInternalName(sDepartmentFieldName);
    fldDepartment.Choices.Add("Human Resources");
    fldDepartment.Choices.Add("Information Technology");
    fldDepartment.Choices.Add("Finance");
    fldDepartment.Choices.Add("Research and Development");
    fldDepartment.Choices.Add("Sales");
    fldDepartment.Choices.Add("Marketing");
    fldDepartment.Group = columnGroup;
    fldDepartment.Update();
    string contentTypeGroup = "Chapter3 Project Proposal Content Type Group";
    obect modelused, for creating custom content type// We will use Document Content type as the parent
    SPContentType documentCType = web.AvailableContentType s[SPBuiltInContentTypeId.Document];
    // Create the Budget Proposal Content type.
    SPContentType ctProjectProposal = new SPContentType (documentCType, web.ContentTypes, "Chapter3 Project Proposal");
    ctProjectProposal = web.ContentTypes. Add(ctProjectProposal);
    ctProjectProposal.Group = contentTypeGroup;
    // Add columns created earlier
    SPFieldLink projectStartDateFieldRef = new SPFieldLink (fldProjectStartDate);
    projectStartDateFieldRef.Required = true;
    ctProjectProposal.FieldLinks.Add(projectStartDateField Ref);
    // Add columns created earlier
    SPFieldLink projectEndDateFieldRef = new SPFieldLink (fldProjectEndDate);
    projectEndDateFieldRef.Required = true;
    ctProjectProposal.FieldLinks.Add (projectEndDateFieldRef);
    SPFieldLink frAmount = new SPFieldLink(fldAmount);
    ctProjectProposal.FieldLinks.Add(frAmount);
    SPFieldLink frDepartment = new SPFieldLink (fldDepartment);
    ctProjectProposal.FieldLinks.Add(frDepartment);
    // Commit changes.
    ctProjectProposal.Update();
    }
    
  10. Add the code to delete the content type and columns in the FeatureDeactivating method. The code should be as follows:
    public override void FeatureDeactivating(SPFeatureReceiver Properties 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;
    web.AllowUnsafeUpdates = true;
    web.ContentTypes["Chapter3 Project Proposal"]. Delete();
    web.Fields.GetFieldByInternalName("Chapter3_x0020_ Project_x0020_Amount").Delete();
    web.Fields.GetFieldByInternalName("Chapter3_x0020_ Department").Delete();
    web.Fields.GetFieldByInternalName("Chapter3_x0020_ Project_x0020_End_x0020_Date").Delete();
    web.Fields.GetFieldByInternalName("Chapter3_x0020_ Project_x0020_Start_x0020_Date").Delete();
    web.Update();
    web.AllowUnsafeUpdates = false;
    }
    
  11. Build and run the project by pressing F5. Visual studio will launch the browser with the site you provided during the project creation. Navigate to Site Actions | Site Settings | Galleries | Site Columns. You should see the new site columns created under a new group Chapter3 Project Proposal Site Column Group. Similarly, navigate to Site Actions | Site Settings | Galleries | Site Content Types to see the Project Proposal content type as shown in the following screenshot:
How to do it...

How it works...

Instead of the schema, we made use of the object model APIs to create the site columns. This was explained in our first recipe. The addition to this recipe is to create the content type and reference the site columns that were created previously.

For this, we used the SPContentType constructor and passed in the parent content type and the name of the content type. We added this to our web content type collection.

To add a reference to the fields, we created an instance of SPFieldLink and added to the content type. The content type was updated with the reference to the fields when we called the update method on the content type.

See also

  • Associating a document template with the content type recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.8.247