SharePoint 2010 General Lab for Developers (Part 4/5)

sergefacebook_thumb1_thumb

 

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:

clip_image002

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:

clip_image004

Reference the site columns from the content type definition. Copy the code provided in snippet2.txt into the Content Type FieldRefs tag:

clip_image006

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:

clip_image008

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) :

clip_image010

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.

clip_image012

Now let’s create a new site column to our StudentSC item (copy snippet4.txt) :

clip_image014

Reference this new site column from our Student Content Type :

clip_image016

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 :

clip_image018

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 :

clip_image020

Add the content type to the list and create a view named “All Students”; set it as the default view.

clip_image022

Build and deploy the package; create a course site and make sure the Students list and the new default view are created:

clip_image024

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s