In this task, you create an empty SharePoint 2010 project in Microsoft Visual Studio 2010.
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.
In this task, you create the content type as a feature and add an event receiver.
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:
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:
In this task, you verify the presence of the content type and the two fields.
You should see the new field Team Project. Figure 1. Team Project field
You should see the new content type New Announcements. Figure 2. New Announcements content type
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/3
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox