C H A P T E R  12

images

Creating Your First Content Factory
Using Content Types As Part of Our User Experience

With our first building blocks in place, we should now start looking at what content we should include in our newspaper. No, I'm not talking about which articles would provide the best readership but rather how the content is built.

Structuring content is a lot easier with content types, but we also get a lot of other benefits, as you saw in Chapter 8. Now it is time to see how you can harness the power and coolness of content types to create a great user experience.

Mission Objective

In this chapter, we will practice creating content types and create a simple content type hierarchy for use in our Empire Times web site. We will create three different content types to hold a root NewsArticle type, and we'll have two child types, a “Series article” and “Stand-alone article,” derive from that root type. We will utilize the site columns we created in the previous chapter.

We will also discover a few gotchas for content type deployment. Finally, we will add an event receiver to our content type to make sure editors do not delete or modify the news in any way we do not approve.

Content Type Startup

We established in Chapter 8 that content types are exceptionally cool. Or rather, I told you, and you are of course free to disagree, but I will assume that you at least want to explore how to build content types so you can make up your mind.

So, let's get down to business.

Exercise 12-1. Creating Your First Content Type

What we just did was add the base of a content type. In itself this is quite worthless, but I wanted to show you a few basic things first, before we dive into the heavier stuff.

The most challenging aspect so far is to create the content type ID. As you may remember from Chapter 8, we basically have two options for crafting the ID value. The first method is to use the parent content type, 0x01 for Item in our example, and then add a two-digit number for our content type. Any two-digit number goes, except for 00, which is used in the second method.

The second method is to use the parent content type, still 0x01 for Item in our example, and then add 00 followed by a GUID value where the hyphens and brackets have been removed. This method yields a guaranteed unique ID for our content type but also a rather long ID.

You may likely want to combine the two methods when you are developing a content type strategy. One of the cool features of content types is inheritance support, and you may want to take advantage of this feature in your strategy. In this case, you may want to use the second method to create your root content type and then use the first method to create child content types.

Let's take a look at how this might work.

Exercise 12-2. Creating a Content Type Hierarchy

As of now, we have a semiworking content type hierarchy for our solution, but we still have a long way to go. Next we want to add our previously created site columns to our content types and discover some gotchas for content type deployment.

imagesTip At this point, you may want to set the Hidden="True" attribute on the root NewsArticle content type. We will not be using that content type directly but rather focusing on the child content types. This is completely optional.

Content Type Columns

Now, by the sheer power of logic, we should be able to add some fields and some content to our parent types and then have the child content types inherit those properties. Let's give that a try, shall we?

Exercise 12-3. Adding Content Type Content

Our base article content type has gotten the site columns we wanted. If you did everything correctly in the previous chapter, you should also find that the columns reference the right list and field and that everything is peachy.

Now let's check to see what has happened to the child content types. Excited? Don't be, there is disappointment ahead. You see, with the code we have now, there is very little inheritance, at least for the sake of site columns.

Figure 12-4 may throw some cold water in your blood. Don't worry; there is an easy fix—our first gotcha for SharePoint content type deployment.

images

Figure 12-4. What? Where are the “inherited” site columns? I want my money back!

As you can see, we didn't get any of the columns that the parent content type NewsArticle defines, even if we set up the correct content type ID. Let's take a look and see whether we cannot fix this.

Exercise 12-4. Fixing Broken Inheritance

Time to Get Serious: Content Type Forms

We need to take a trip down memory lane. You should recall from Chapters 6 and 8 that the data editing, entry, and display experience is closely tied to content types. Each content type is connected to a set of form templates that define how data should be presented to users. The default form templates are inherited by the Item content type, and all point to the ListForm rendering template.

However, the default display of data doesn't really suit us. After all, we want to create a newspaper, not a data sheet web site. The default form templates do not resemble any newspaper I have seen, so let's get medieval on the default interface.

Exercise 12-5. Changing the Default Form Templates

imagesNote We haven't modified or created a custom NewForm or EditForm. These forms follow the same principles as the DisplayForm. I will leave modifying them as an optional exercise for you.

We now actually have a working content type that uses our new forms, but we haven't actually added the content type anywhere. You could of course trust me, but then you wouldn't heed the warning I gave you about not trusting strange authors. So, to verify, do the following optional exercise.

Exercise 12-6 (Optional). Testing the List Custom Display Form

More Advanced Concepts

You may wonder why I asked you to create a feature with a receiver earlier, rather than just a blank feature. Rest assured that your time of wondering is now over; you are about to be enlightened. What we are going to do here is explore some advanced concepts in order to learn more about the coolness of content types.

Preventing News Deletion and Category Change

The editor of Empire Times has decided that news should not be deleted. In addition, news articles should not change categories. I mean, if you have a weather report, chances are it will not be a sports update any time soon.

To accomplish this, we are going to utilize an item event receiver. Event receivers are important tools for making sure that the user experience feels right and is secure. In short, an event receiver is a piece of code that fires whenever a certain event happens in SharePoint.

imagesNote A thorough investigation of event receivers is a bit outside the scope of this book. Luckily for you, I have a Business Process Management series on my blog that explains event receivers and workflows. Check it out at http://www.understandingsharepoint.com/url/10001.

Exercise 12-7. Creating Event Receivers in Content Types

What we just did was bind a feature receiver to a content type using an XML document. The cool thing about this is that no matter where you now add the content type, the feature receiver will follow.

We can still do more. Content types can be tied to other functionality as well. For example, you might want to connect special user actions only to documents or items of a particular type.

One method of doing so is with a custom action, which we discussed briefly in Chapter 5. Custom actions are the method by which links and items are added to menus and lists in SharePoint. We will explore this further in Chapter 13 as well, but I wanted to show you how to connect a custom action to a specific content type.

Exercise 12-8. Connecting a Custom Action to a Content Type

Binding the Pieces Together

One more important task remains. What happens if we activate our content types feature without having already activated the site columns feature? Disaster I tell you, disaster!

Fortunately, SharePoint gives us an opportunity to prevent such accidents through the FeatureActivationDependency element. This element, when placed in a feature.xml file, will prevent features from being activated if the dependent features are not activated already.

Note that FeatureActivationDependency does not activate the dependent features for you. You must still activate these, so consider this feature as a reminder, not as any form of automation. We will look at a better solution in Chapter 14.

To enable feature activation dependency, add the following to your feature.xml file in the TimesContentTypes feature, right inside the Feature element:

<ActivationDependencies>
  <ActivationDependency FeatureId="[FEATURE ID OF SITE COLUMNS FEATURE]"/>
</ActivationDependencies>

Substitute the FeatureId attribute with the feature ID of your TimesSiteColumns feature. You will find this value in the feature.xml file.

Stop Being Cool

I hope these small exercises have inspired you to start developing your own content types from scratch. You may even have picked up a tip or two on a few pitfalls that lurk, waiting for the next haphazard developer who assumes that everything works just like they hope.

You can now remove your sunglasses, take off your leather jacket, and step off the Harley. Just remember to bring it all back when you start doing content type development in your production environment. You'll be the star of any show.

Unless they bring a straitjacket, that is. Either way, you should have a great time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.107.210