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
- To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
- On the File menu, point to New, and then click Project.
- In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
- SelectEmpty SharePoint Project from the project items.
- In theName box, type CreateContentType and then click OK.
- In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as http://localhost/SampleWebSite).
- For the trust level, select Deploy as a farm solution and then click Finish.
Learn SharePoint by Tekslate - Fastest growing sector in the industry. Explore Online "SharePoint Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into a SharePoint Expert.
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
- Right-click the Features folder in Solution Explorer and then click Add Feature.
- Right-clickFeature1 and then click Add Event Receiver. Visual Studio adds a feature event receiver to Feature1.
- Right-clickEventReceiver.cs and then click View Code.
- Uncomment the FeatureActivated method in the Feature1EventReceiver
- Insert the following code in the FeatureActivated
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(); }
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 type Announcement 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 titled Company, to the content type.
- Uncomment the FeatureDeactivating
- Insert the following code in the FeatureDeactivating
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 field Team Project.
- 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
- Start Internet Explorer and browse to the Web site that you specified previously.
- At the upper-left section of the screen, click Site Actions, and then click Site Settings.
- UnderGalleries, click Site Columns.
- In theShow Group options, click Custom Columns.
You should see the new field Team Project. Figure 1. Team Project field
- Click Site Actions and then click Site Settings.
- UnderGalleries, click Site content types.
- From the Show Group options, select Custom Content Types.
You should see the new content type New Announcements. Figure 2. New Announcements content type
For an in-depth understanding of SharePoint click on