Search This Blog

Friday, January 9, 2015

SharePoint 2010: Create Custom Content Types

The first thing you will need to do is open up Visual Studio 2010.

Create Custom Content Types

  1. To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
  4. Select Empty SharePoint Project from the project items.
  5. In the Name box, type CreateContentType and then click OK.new custom content type
  6. In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (for me it was http://neronguyen/sites/SharePointDevelopment).debug custom content types
  7. For the trust level, select Deploy as a farm solution and then click Finish.

Create New Custom Content Types

In this task, you create the custom content types as a feature and add an event receiver.

To create a content type

  1. Right-click the Features folder in Solution Explorer and then click Add Feature.3-add-feature
  2. Right-click Feature1 and then click Add Event Receiver. Visual Studio adds a feature event receiver to Feature1.
  3. Right-click Feature1.EventReceiver.cs and then click View Code if it doesn’t pop up automatically.
  4. Uncomment the FeatureActivated method in the Feature1EventReceiver class.
  5. Insert the following code in the FeatureActivated method:
using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
{
    SPContentType newAnnouncement = spWeb
        .ContentTypes
        .Cast<SPContentType>()
        .FirstOrDefault(c => c.Name == "New Announcements");
    if (newAnnouncement != null)
    {
        newAnnouncement.Delete();
    }
    SPField newField = spWeb.Fields
        .Cast<SPField>()
        .FirstOrDefault(f => f.StaticName == "Team Project");
    if (newField != null)
    {
        newField.Delete();
    }
    SPContentType myContentType =
        new SPContentType(spWeb.ContentTypes["Announcement"],
            spWeb.ContentTypes, "New Announcements");
    myContentType.Group = "Custom Content Types";
    spWeb.Fields.Add("Team Project", SPFieldType.Text, true);
    SPFieldLink projFeldLink = new SPFieldLink(spWeb.Fields["Team Project"]);
    myContentType.FieldLinks.Add(projFeldLink);
    SPFieldLink companyFieldLink = new SPFieldLink(spWeb.Fields["Company"]);
    myContentType.FieldLinks.Add(companyFieldLink);
    spWeb.ContentTypes.Add(myContentType);
    myContentType.Update();
}
4-feature-activated
The FeatureActivated method is run when Feature1 is started. This code does the following:
  • Deletes the content type New Announcements and the field Team Project, if they exist.
  • Creates a parent content type Announcement based on the New Announcementscontent type.
  • Creates a text field, which is titled Team Project, and then adds it to the content type.
  • Adds an existing field, which is titled Company, to the content type.
6. Uncomment the FeatureDeactivating method.
7. Insert the following code in the FeatureDeactivating method:
using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
{
    SPContentType myContentType = spWeb.ContentTypes["New Announcements"];
    spWeb.ContentTypes.Delete(myContentType.Id);
    spWeb.Fields["Team Project"].Delete();
}
5-feature-receiver
8. In Solution Explorer, right-click CreateContentType and then click Deploy.
The FeatureDeactivating method is run when Feature1 is deactivated. This code does the following:
  • Deletes the content type New Announcements.
  • Deletes the text field Team Project.

Verify that the Project Works Correctly

In this task, after creating custom types are completed, you verify the presence of the content type and the two fields.

Verifying the Team Project Custom Column

  1. Start Internet Explorer and browse to your Web site.
  2. At the upper-left section of the screen, click Site Actions, and then click Site Settings.
  3. Under Galleries, click Site Columns.
  4. In the Show Group options, click Custom Columns.You should see the new field Team Project.
6-custom-columns

Verifying the New Announcement Custom Content Type

  1. Click Site Actions and then click Site Settings.
  2. Under Galleries, click Site content types.
  3. From the Show Group options, select Custom Content Types.You should see the new content type New Announcements.
7-site-content-types
Errors: When creating custom content types, I received an error saying:
Error 1 ‘Microsoft.SharePoint.SPContentTypeCollection’ does not contain a definition for ‘Cast’ and no extension method ‘Cast’ accepting a first argument of type ‘Microsoft.SharePoint.SPContentTypeCollection’ could be found (are you missing a using directive or an assembly reference?) c:usersadministratordocumentsvisual studio 2010ProjectsCreateContentTypeCreateContentTypeFeaturesFeature1Feature1.EventReceiver.cs 25 68 CreateContentType
Resolution: Add the line: using System.Linq; at the top of the file.

No comments:

Post a Comment