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.
Feature1.EventReceiver.cs
. Uncomment FeatureActivated
and FeatureDeactivating
methods. 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(); }
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; }
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.
3.141.8.247