C H A P T E R  15

images

Our Empire United
Wrapping All Our Hard Work into a Single SharePoint Solution

Now we are nearing the end of our empire building, but there are still important tasks to accomplish. We want to wrap all our hard work into a complete SharePoint solution that can be used to set up a complete SharePoint site, including all the custom fields, list templates, lists, content types, and pages we have developed.

Unlike certain MMORPGs I play, however, the end game in this book is far from lacking. We still have more tasks to accomplish, such as adding our own custom navigation and ensuring that everything is set up as expected. Oh, and our story takes a strange turn later, so read on.

Mission Statement

In this chapter, we will create our final pieces of our puzzle and put those pieces together to form a new site. We will create a basic site definition and then add the pieces we have created. We will then add navigation and explore how we can create navigation options that fit our needs.

Creating the Site Definition

After reading Chapter 3, you may know that I am biased toward using features for most of our functionality deployment. Let's take that notion to the extreme by removing any superfluous functionality from our site definition.

imageNote There are two empty elements here, DocumentTemplates and NavBars. The NavBars element is there for validation only; it is required by the wss.xsd schema. The DocumentTemplates element must exist, but may be empty, for the default Document Library template to work. If the DocumentTemplates element is not present, attempting to create a document library will cause an exception to be thrown.

Oh, and you might want to call me a liar in a little while.

Our site so far really has no content. No default.aspx page, no lists or even list templates, no data, no nothing. It's not very useful, is it?

What we can do, however, is to go to the Site Settings page and activate all the glorious features we have created. Go ahead—do that now.

Not very easy, you say? How do you get to the Site Settings page without the Site Actions menu? Oh, yeah, if you manage to wrangle that _layouts/settings.aspx URL from memory, now all you have to do is remember in which order to activate the different features. Remember that if you do this wrong, your site may be permanently damaged and even at best will simply not work as expected.

There must be a better way. And, knowing me as you do after reading this entire book, you also know that an answer will be presented shortly.

In the previous exercise, we made a few improvements so that most of our setup is now done semi-automatically. First, we made sure that when a new site is created from within the site collection, we are sent to the Site Settings page to complete the setup of features and other…features.

Second, we set the WelcomePage property to a custom page. You can, of course, set this to any page, but note that the address is relative to the current site.

With a complex feature dependency scheme like the one we have in our solution, the built-in activation dependencies will not work. One important reason why activation depend- encies will not help is that one of the site-scoped features, the fields feature, depends on a web-scoped feature, specifically, the “Article category” list feature. Site-scoped features cannot depend on web-scoped features.

And even if activation dependencies did work, you will only be warned that you need to activate other features first. The “dependee” features will not get activated when a depending feature is activated. We need to either activate manually or write code as we have done here.

Still, our solution is not elegant or flexible. If you add new features, you need to build and deploy a new assembly. If you change the order of activation, you need to build and deploy the new assembly. These issues, however, can be overcome, for example, by reading the order of features from a config file. I will leave that as an exercise for you, because we have more pressing matters that require our attention.

At this point, we have a site that resembles Figure 15-4 after we have created a new site collection and hit the Activate button on the TimesSetup feature.

Image

Figure 15-4. Our site so far

Although I know that this is massively impressive and your mind can barely grasp the power you now possess, we still need to do more. For example, we have no navigation yet. Let's fix that.

Can't reactivate the feature? Getting errors about already existing features? I know, I know, we have already activated the feature that in turn activated all the other required features of our solution. Not just that, but if you deactivate the features, you still get an error since one of the features, TimesArticleCategoryList, will try to create a list that already exists.

There is a simple fix. Just modify your feature activate code, and check to see whether the feature is already activated:

if (web.Features[g] == null) web.Features.Add(g);

Similarly, for site-scoped features, you can use this:

if (web.Site.Features[g] == null) web.Site.Features.Add(g);

You may wonder why I did not tell you about this right away. Well, it is easier to remember a fix for a problem you experience than just an explanation that something will go wrong if you don't do something.

Now you can try to reactivate your setup feature and see what happens. Back on your front page, you should now see something resembling Figure 15-5.

Image

Figure 15-5. QuickLaunch in place

We should move on. Let's see whether you can guess what's up next.

Come on, you knew I was kidding, right? We're not done yet. There are still a great many things we should do, but for now, let's fix up the problem we just discovered.

The problem in our case is simple; there is no TopNavigationBar defined in our site. Remember back to our site definition? We left the NavBars element empty. However, in order to add items to the menu, the menu must exist. Now you know why you might want to call me a liar for saying that the empty NavBars element was there for validation only. I feel very ashamed if that makes you feel better.

So, our solution seems simple enough; just modify your site definition NavBars element in the onet file as such:

<NavBars>
  <NavBar ID="1002"/>
</NavBars>

And you would think that everything is in order, right? Wrong. If you modify your site definition, your supportability takes a nosedive that would bring a blush to the face of Stuka pilots.

This is important. Because your site definition in effect is set in stone once you create your first site off that definition, you need to think very carefully what you put into that definition. If you forget a vital element, such as the TopNavigationBar element, you are basically out of luck and must re-create all your sites.

However, for our development environment, we don't care. And, because of our massive developer genius, we know that we can simply delete our site completely, redeploy our fixed onet.xml, create a new site collection, and hit our magic Setup button to get the whole show running again.

But let's really explore brilliance. While in the onet file, add the following element to your single configuration element:

<WebFeatures>
  <Feature ID="[FEATURE ID OF TIMESSETUP FEATURE]"></Feature>
</WebFeatures>

See where this is going?

OK, there are a few details left. And I promised I would explain why I added the categories.aspx URL to the News navigation node.

First, though, some more glory. Figure 15-7 shows what your TopNavigationBar looks like with the child menu open.

Image

Figure 15-7. TopNavigationBar open…and another detail

Notice anything in particular? At least two things might spring to mind. The News tab is highlighted. That is because we are on the page to which the News tab links. If you add nodes directly to the TopNavigationBar navigation node collection, you will, as a free bonus, get tab highlighting when you are on the page to which a tab links.

You can try this by modifying your node creation code in TimesSetup.cs as such:

SPNavigationNode newsNode =
 new SPNavigationNode("News", "Lists/Categories/AllItems.aspx");
topNavigationBar.AddAsFirst(newsNode);
SPNavigationNode localNews = image
 new SPNavigationNode("Local news", "categories.aspx");
topNavigationBar.Add(localNews, newsNode);
SPNavigationNode weather = new SPNavigationNode("Weather", "/");
newsNode.Children.AddAsFirst(weather);
SPNavigationNode sports = new SPNavigationNode("Sports", "/");
newsNode.Children.Add(sports, weather);

I have put the modified lines in bold. The changes simply remove the link to categories.aspx from the News node and place it on the “Local news” node instead. Now, if you go to the front page, you will get Figure 15-8 as your result. Needless to say, going to the All Items view of the Categories list will highlight the News node.

Image

Figure 15-8. Highlighting of different tab

Figure 15-8 also reveals another feature. Note that in the latest modification we added two nodes directly to the TopNavigationBar node collection, but we added two child elements to the News item and even some grandchild elements to spice up the navigation. Notice that the nodes now are ordered differently than Figure 15-7, for example. That is because the root nodes, News and “Local news,” in our example, get all their child elements added before the next root node. If we add child elements to the “Local news” node, SharePoint will add these to the end of our menu.

To be perfectly honest, and I think I should be after that NavBars prank I pulled, the number of element levels displayed horizontally depends on the StaticDisplayLevels property of the AspMenu control that is added by the default.master page. By default this is set to 2, meaning the first two levels of elements are added as horizontal tabs.

The number of fly-out levels is determined by the MaximumDynamicDisplayLevels property of the same control. So, changing these values to StaticDisplayLevels="1" and MaximumDynamicDisplayLevels="2", we get a result resembling Figure 15-9.

Image

Figure 15-9. Less static, more dynamic

OK, there is one last thing I want to reveal before we end. The highlighting issue is a bit more complex. You see, the highlighting is actually done on the root node if you are on a page from which any of the descendant elements of that node links. For example, if you were to modify the Soccer node to the following code:

SPNavigationNode soccer = new SPNavigationNode("Soccer",image
"/Lists/Categories/NewForm.aspx");

then the News node would light up if you were on the NewForm of the Categories page. This is because the Soccer node is a child, or rather a grandchild, of the News node. I have done this; Figure 15-10 shows how this works.

Image

Figure 15-10. Highlighting root node

Of course, you might already know this if you worked extensively with the ASPMenu object before. Yeah, those menus are little more than regular ASP menus.

Enough talk.

Your Empire Is Complete

You're done! Yeah, really. Done. No more. You have created, from scratch, a complete site while learning how to manipulate the user experience in a ton of different ways.

You have explored deep forests. You have traversed mountains and glaciers of ice so cold that any other mortal would succumb. You have fought dragons and monsters in dark, damp caves. You have investigated massive fields of technical challenges down to details so small that quarks look like planets. You have built an empire from scratch, from virtually nothing, until a massive and majestic site emerged. Your empire will stand the test of time.

You want to know what that empire really is? It is your knowledge. The site will be gone when you reinstall your computer. Your knowledge of how you built the site will remain forever.

Congratulations. You are done. Now it is time to get started.

Oh, and by the way, in case you're reading only the last page of this book: there is only one fictional character here (except the dragon, of course). Remember that annoying editor who wanted to prevent you from deleting news articles? Well, since no other character is part of our story, it is logical to assume that the killer is the editor.

Now, the real mystery is to figure out who was killed. Perhaps I need to write a new book….

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

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