C H A P T E R  13

images

Accounting Gone Haywire
List, Forms, Reports, Views…Ah, the Bean Counters Will Go Mad!

Lists: either you love them or you need to reconsider SharePoint as your platform of choice.

Everything that is SharePoint revolves around lists of some sort. As good developers, we must thus learn how to work with and develop these lists. After reading this chapter, you will have covered the basics of both list template creation and list instantiation using features.

Mission Objective

In this chapter, we will create a list template from scratch. Actually, we will skip the view construction and just borrow code from the Custom List template.

Next, we will attach our previous content types to that list template and discover some caveats to content type deployment. Finally, we will instantiate two new lists, the “News article” list and the “Article category” list, before we add some logic to make sure our list infrastructure gets created the right way.

List Templates

Our first step is to create the list templates we will use for the news articles. So, without further delay, here is the first exercise.

Exercise 13-1. Creating the First List Template Feature

Exercise 13-2. Creating Your Basic Schema.xml File

CONTENT TYPE MYSTERY

You may have noticed that we did not add any content types to our list. If you remember our previous bet regarding whether or not you were using content types, you may recall that you always get Item and Folder content types added to your list if you create a default custom list. Now that we didn't use the custom list, the Item and Folder content types are not present.

Before you run over to Amazon to retract your book review or cancel the $5 payment to my PayPal account, check out your list in SharePoint Manager 2007. Specifically, check out the ContentTypes node and see that you still get a default content type, named after your list.

images

So, yes, you still owe me that review.

Oh, and in case you are wondering where the Title, Created By, and Modified By originate, these come from the BaseType 0 list, defined in the global site definition. The default content type added to a list without any specific content type gets columns added from the BaseType from which the list is generated.

Adding Content Types to Our List

We haven't added any content types to our list yet, and we want to do that, so let's try the most obvious thing first, the ContentTypes element of our list template.

Exercise 13-3. Adding Content Types, Take 1

The problem here is that the child content types do not inherit the parent type's proper- ties correctly. If you had added the NewsArticle root type instead, all the columns would be added as expected, but of course this is not what we want, so we need to look for alternative solutions.

You have a few options for making content types work as expected using the ContentTypes element in the list template:

  • Update the content type manually through the web interface. Just go into any column on the content type and hit OK without making changes.
  • Add the content type manually to the list. Remember to enable content type management from the advanced settings of the List Settings page.
  • Add all the content type columns to the Fields element in your list template.

These options aren't any good, though. The first two options rely on manual labor after creating a list. Imagine the overhead of instructing users to go through the hoops. The last option is a terrible approach because it relies on adding your columns in two places, first in the content type and then in any list using those content types.

No, we need a better approach. Read on….

imagesNote I am going to annoy you a bit. Now that you have created the list, try deleting it.

Not successful? I'll show you why later and provide you with a fix.

Exercise 13-4. Adding Content Types, Take 2

This approach does solve a few of our problems. First, we do get our content types added. Second, we also automatically enable content type management. Third, the columns from the content types are added correctly to the list.

The downside, however, is that we need to know the list URL prior to creating the feature. Since we have no way of modifying the feature after it has been deployed, this method is useful only when we also deploy the list instance using features and thus know the URL prior to creating the content type bindings feature.

Still, this may be useful, especially if you create lists as part of site creation. Next, let's see whether we can find an even better approach.

Exercise 13-5. Adding Content Types, Take 3

Although this method will allow us to add child content types and make them behave as we want, it still has issues. First and foremost, we do not enable management of content types using this method, and thus we will always be given the default content type when adding a new item through the web interface. Second, since management of content types is not enabled, there is no way to change a content type for an existing item.

The benefit of this approach is that we are fixing the inheritance issue at the source. Because of this, we do not need to know the URL of any lists that will use the content type in advance.

Sadly, there is currently no “best-of-both-worlds” solution to the issue of deploying child content types, so you need to take the approach that best fits your needs.

We should move on; there's plenty of work still to be done.

List Forms

Previously we borrowed the Forms section from the default Custom List template. This may or may not be what you need. If it is what you need, feel free to use that method, but if not, here's how to add your own list forms to customize the editing and displaying of list data.

Remember that a list form is only one part of the content type/user experience picture.

Exercise 13-6. Adding Custom List Forms

imagesNote When creating your own custom list form pages, remember to match any changes in the WebPartZoneId of the Form element to the web part zone in your page.

List Instances

Although it is extremely simple, we now have a working list template from which we can create our new list. We need to create two lists actually, both the article list and the category list to which the Article Category lookup column will be linked.

Let's start with the article list, because the category list will cause us a bit of a problem.

Exercise 13-7. Adding a List Instance

Adding the Categories List

The Categories list is where we will store news categories. The site column Article Category, which we created in Chapter 11, is linked to this list. We can create this list from the Custom List template, so we don't need to create a new list template for the Categories list.

It would seem that adding the category list would be really easy now. It is, but there is also a caveat. Let's take a look at what does not work.

Exercise 13-8. Adding the Categories List, Take 1

It All Depends on This...

So, why can we not deploy the solution now? At this point, we have three main components in our solution. First, there are the site columns, one of which is a lookup column to a list, and then there's the Categories list, which we are about to create. So, to activate the site columns feature, we must first make sure we activate the lists feature.

Now, the lists feature adds a content type, or two actually, to the list. So, it makes sense to have the list dependent on the content type being activated. You may see where this is going.

The content type is utilizing the site columns we added, including the lookup column. So, now the content type depends on the site columns feature, which depends on the list feature, which depends on the content type feature. Circular references and madness ahead. But we are smarter than that, aren't we?

Exercise 13-9. Adding the Categories List, Take 2

Fixing the List Deletion Problem

You may have been surprised by a small issue with the News list. If you try to delete the list, regardless of whether you have items in the list, you will get a message saying “Cannot delete news. If it happened, it happened.” Of course, you remember that we set up an event handler to give us this message in the previous chapter.

This may be surprising because there are no items in the list and the event handler that causes this error is connected to the ItemDeleting event. Even if you have no items in the list, the ItemDeleting event handler prevents you from deleting the list. Why?

The answer becomes clear after a bit of SharePoint Manager investigation. Open your list in SPM, and look at the items there (see Figure 13-7).

images

Figure 13-7. Mystery solved.

When we add a content type to a list, SharePoint creates two folders to hold content type resources for us. And, as we remember from Chapter 8, folders are nothing but normal items with a specific content type inheritance. Thus, the ItemDeleting event is fired for these two folder items, preventing us from deleting the list.

Let's see whether we can fix this annoyance.

Exercise 13-10. Fixing the ItemDeleted Event Handler

Accountants, Go Home!

We're done with lists for now. No more. The accountants may have had a field day, but we are going home. Or, at least, we are going to go to the next chapter.

Our empire is growing steadily. Using the current code, we are able to get our news web site up and running. But we want more….

In the next two chapters, we will expand our solution to add some custom pages to our site and additionally wrap up everything into a single site definition. After that, we will have a complete SharePoint site ready to deploy at the click of a button.

You know that's just a saying, right?

See you on the next page.

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

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