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



using (SPWeb spWeb = properties.Feature.Parent as SPWeb)


SPContentType newAnnouncement = spWeb



.FirstOrDefault(c => c.Name == "New Announcements");

if (newAnnouncement != null)




SPField newField = spWeb.Fields


.FirstOrDefault(f => f.StaticName == "Team Project");

if (newField != null)




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"]);


SPFieldLink companyFieldLink = new SPFieldLink(spWeb.Fields["Company"]);





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



using (SPWeb spWeb = properties.Feature.Parent as SPWeb)


SPContentType myContentType = spWeb.ContentTypes["New Announcements"];


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


  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


For Indepth understanding of SharePoint click on