• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Create Custom Content Types in SharePoint 2010

Create a SharePoint 2010 Project

In this task, you create an empty SharePoint 2010 project in Microsoft Visual Studio 2010.

To create the SharePoint project

  1. To start Visual Studio 2010, click theStart Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
  2. On theFile menu, point to New, and then click Project.
  3. In theNew Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
  4. SelectEmpty SharePoint Project from the project items.
  5. In theName box, type CreateContentType and then click OK.
  6. In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such ashttp://localhost/SampleWebSite).
  7. For the trust level, selectDeploy as a farm solution and then click Finish.

Create a Content Type

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

To create a content type

  1. Right-click theFeatures folder in Solution Explorer and then click Add Feature.
  2. Right-clickFeature1 and then click Add Event Receiver. Visual Studio adds a feature event receiver to Feature1.
  3. Right-clickEventReceiver.cs and then click View Code.
  4. Uncomment theFeatureActivated method in the Feature1EventReceiver
  5. Insert the following code in theFeatureActivated

 

C#

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

Desired to gain proficiency on SharePoint? 
Explore the blog post on SharePoint training course to become a pro in SharePoint.

The FeatureActivated method is run when Feature1 is started. This code does the following:

  • Deletes the content typeNew Announcements and the field Team Project, if they exist.
  • Creates a parent content typeAnnouncement based on the New Announcements content type.
  • Creates a text field, which is titled Team Project, and then adds it to the content type.
  • Adds an existing field, which is titledCompany, to the content type.
  1. Uncomment theFeatureDeactivating
  2. Insert the following code in theFeatureDeactivating

 

C#

using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
{
SPContentType myContentType = spWeb.ContentTypes[“New Announcements”];
spWeb.ContentTypes.Delete(myContentType.Id);
spWeb.Fields[“Team Project”].Delete();
}

 

The FeatureDeactivating method is run when Feature1 is deactivated. This code does the following:

  • Deletes the content typeNew Announcements.
  • Deletes the text fieldTeam Project.
  1. In Solution Explorer, right-clickCreateContentType and then click Deploy.

 

Verify that the Project Works Correctly

In this task, you verify the presence of the content type and the two fields.

To test the project

  1. Start Internet Explorer and browse to the Web site that you specified previously.
  2. At the upper-left section of the screen, clickSite Actions, and then click Site Settings.
  3. UnderGalleries, click Site Columns.
  4. In theShow Group options, click Custom Columns.

You should see the new field Team Project.

Figure 1. Team Project field

83

  1. ClickSite Actions and then click Site Settings.
  2. UnderGalleries, click Site content types.
  3. From theShow Group options, select Custom Content Types.

You should see the new content type New Announcements.

 

Figure 2. New Announcements content type

84

For Indepth understanding of SharePoint click on

Summary
Review Date
Reviewed Item
Create Custom Content Types in SharePoint 2010
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Create Custom Content Types in SharePoint 2010"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.