Starter files and solutions files can be found here.
In this lab we will define content types and create views by code.
We will have to create a list of students, but before doing that we must define what a student is; a student content type will be defined in the CourseSiteFeatures project.
Let’s create a new Folder named ContentTypes in our project.
In this folder, add a SharePoint project item from the Content Types template and name it StudentCT : the base content type must be Item.
Change the Name, the Group and the Description as following:
Let’s define the Site Columns : first create a new empty folder called SiteColumns; in this folder add a new SharePoint Project Item based on the Empty Element template; name it StudentSC.
Copy and paste into Elements.xml the text from snippet1.txt. 4 site columns are defined in this snippet:
Reference the site columns from the content type definition. Copy the code provided in snippet2.txt into the Content Type FieldRefs tag:
You will notice that we add an out of the box Site Column (Company) and that we remove (RemoveFieldRef) the default and Out of the Box Title column.
Very important: switch the Inherits attribute to FALSE otherwise the RemoveFieldRef tag won’t work.
Create a feature called StudentCT and make sure your items StudentSC and StudentCT are part of this feature (and only there, remove them if they are part of other features).
The feature must be hidden and part of the CourseSitefeature dependency.
Deploy your package, check to make sure the content type has been deployed with the appropriate site columns:
Create a custom list based on this new content type ; when you add a new item based this new content type, you should have the following form (you will notice that the Title column is now hidden) :
Now we will create a custom field type that will allows us to define an E-mail address.
In your Visual Studio 2010 project, create a new Folder called CustomFieldTypes; add the files StudentEmailFieldType.Field.cs and StudentEmailFieldType.FieldControl.cs files into this folder; analyze the code : the key function is the GetValidatedString() function where the validation takes place.
An xml file describing this custom field type must be deployed to 14\TEMPLATE\XML : add a mapped folder to this location; in this XML mapped folder, add an xml file named fldtypes_validatedEmail.xml (the requirement is the filename must start with “fldtypes”); in this file add the code provided in snippet3.txt.
Now let’s create a new site column to our StudentSC item (copy snippet4.txt) :
Reference this new site column from our Student Content Type :
Deploy you package; delete and recreate the Student list with the new version of the Student Content Type; add a new item and check that the email validation works as expected :
We want the Student list to be present in every course site; we will therefore modify the page CreateCoursIte.aspx.
In CreateCourseSite.aspx code behind, just before the redirection, create the Students list :
Add the content type to the list and create a view named “All Students”; set it as the default view.
Build and deploy the package; create a course site and make sure the Students list and the new default view are created: