C H A P T E R  14

images

Pages and Pages of Fun!
Creating Custom Editing, Management, and Presentation Pages

Custom page development is important to the user experience. After all, everything the user sees is a page, right? So, if we are able to customize the pages, we have maximum control over the user interface.

This chapter will work both with the EmpireTimes solution and with separate and stand-alone solutions. I'll show a range of options for creating pages, including pages that are not tied to any particular solution. So, without further ado, let's get started.

Mission Statement

In this chapter, we'll explore page construction in SharePoint. We will create two types of pages: an application page and a site page. We will also use two different methods for adding code to our pages, both using inline code and using a code-behind assembly. The application page will be a content type hierarchical list, while the site page will be a list of the categories we have put in our Categories list.

Finally, we will use a custom action to add a link to the Site Settings page so that we get a link to the content type hierarchy application page.

Sound good? Let's go!

Basics of Page Authoring in SharePoint

SharePoint is nothing but a relatively standard ASP.NET solution. So, we should be able to create custom pages as much as we like.

We need to consider a few things, however. First, SharePoint uses a common _layouts virtual directory that is mapped to the [12]TEMPLATELAYOUTS folder. This virtual directory is shared between all sites in the entire farm, so it is a very useful place to put pages that are going to be used in all or most sites.

If you want your page to be accessible only to a certain site or site collection, it makes sense to package the pages as a module and make that module part of a feature. That way, you can deploy or retract the page or pages on a site-by-site basis.

Let's start with the latter and create a basic page for our Empire News web site.

Exercise 14-1. Creating a Site Page

The custom page you just created can serve as a template for creating other site pages. From this point on, we will add page-specific content, so I thought it would be wise to just pause here for a moment so you can admire your creation.

Note that most of the content in our page is still housed in the default master page. To maintain a consistent user experience, this makes sense. I am not saying you should keep the standard SharePoint master page; after all, we are here to learn how to customize the user experience, not re-create the existing experience. However, if you stick to using the ~masterurl/ default.master token, then updating the user experience for all pages is a lot easier.

Also note that we are inheriting the Microsoft.SharePoint.WebPartPages.WebPartPage class. In a later exercise, we will create our own class to serve as the code-behind class, but for now we will make do with the basic functionality that WebPartPage provides.

I think we should move on and start adding some interesting stuff to our page.

Exercise 14-2. Adding Some Content to the Page, Take 1

images Note We are adding inline code to our site page, which must present a security problem. After all, if you can just upload an .aspx page and have code run inside, anyone could upload code to a page library and run the page. The answer to this mystery is that we are not running our page from a library at all. If you try the same trick with a page stored in a library or even if you customize the page we have created here using SharePoint Designer, you get an error message stating that you are not allowed to run inline code in this page.

imagesTip If you need to specify paths where you will allow inline code to run, you need to modify the web.config file and set the PageParserPaths element inside the SafeMode element of the SharePoint element in web.config. MSDN has an article describing this issue at http://www.understandingsharepoint.com/url/10035.

When you deployed your updated categories.aspx page in the previous exercise, you did not need to reactivate the feature. The reason for this is that you are updating the source of the module created in the first exercise. SharePoint maintains the link to the source file in a module as long as we do not customize the page further using tools such as SharePoint Designer. As such, any edits made to the source file will immediately affect any page that has been provisioned from that source file.

Here comes a cool thing about WSPBuilder: in your Solution Explorer, you copy a file directly to [12] using a context menu shortcut. Developing this is extremely handy because you do not need to rebuild the WSP file, deploy, wait for solution retraction, and deploy for every minor change you do to your page. All you do is right-click the file, hit “Copy to 12 hive,” and refresh the page. Figure 14-3 shows where this option is located.

images

Figure 14-3. “Copy to 12 hive” option in WSPBuilder

OK, it is time to fix that horrible iteration code from the previous exercise.

Exercise 14-3. Improving the Site Page Code

By using the SPQuery object, we get a lot more control over what is displayed. For example, you might want to add RowLimit to your SPQuery object to just get the first five items within each category:

query.RowLimit = 5;

Or, knowing that the default sort is by ID, you might want to sort the items in a different order. You could sort the news by the modified date to bring the latest news first by adding a few elements to the query:

<OrderBy>
<FieldRef Ascending="False" Name="Modified"/>
</OrderBy>

Look, this exercise is about creating pages, so I don't want to get too deep into the list querying stuff. In any case, doing this using a custom view is likely to be much more efficient and will also add the benefit of being able to modify the view using the web interface rather than updating compiled code.

As such, let's move on.

Adding a Global Page and a Code-Behind

Sometimes you want to improve or expand the management experience, and this is a situation where placing files in the [12]TEMPLATELAYOUTS folder would be a wise choice. As mentioned before, files placed in this folder are available to all sites in all site collections.

For this example, I have chosen to do a simple content type hierarchy page. For the sake of brevity, the example will be extremely simple and just add the content types to a TreeView control. I will leave any expansion beyond that, such as adding fancy icons for sealed or read-only content types or linking to other custom or out-of-the-box management pages, for your exploration.

For this exercise I want you to create a new solution. This solution will be globally available and not dependant on the EmpireTimes solution.

imagesNote There are probably way smarter methods of creating the tree. This is not an exercise in tree building but in global page development.

Exercise 14-4. Adding a Global Page

What we have created up to this point is the outline of a custom layouts page, often referred to as an application page. You basically have the stem cell of a SharePoint layouts page; from here on, the page can be turned into virtually any page you need. Note that System.Web.UI.WebControls is not strictly necessary, but you will be using this so often I thought it made sense to include it in this example.

We are also using the ~/layouts/application.master master page. As you learned in Chapter 5, this page is global to the farm, and modifications to the application.master page are not supported by Microsoft. You may want to create your own master page, and if so, a good starting point is to copy the existing application.master from the [12]TEMPLATE LAYOUTS folder and place it into your solution. However, the default application.master contains a lot of nonvalidating code and certain…features…that will break the Visual Studio rendering engine and display errors. As such, you might want to just start from scratch, especially if you are developing a master page for your solution anyway.

I will leave custom application master page development as an exercise for you to do on your own.

imagesNote Starting down the path of custom application.master development should not be done without considering the consequences. Maintaining a separate application.master includes both operations and development maintenance, because you need to update your application.master as part of your regular update cycle.

We should get back to our regularly scheduled page development.

Exercise 14-5. Adding Content to the Page

What we did in our previous example was to add some content in the form of an asp: Content control and a standard TreeView control to display our content type hierarchy. Then we added some code in our code-behind file to fill the tree with the content types. It wasn't anything fancy; it was just to show you the outline of how you can create a custom page.

imagesNote There are many more content placeholders you may want to utilize to make a better-looking page. I highly recommend reading through the application.master page to learn about the different placeholders.

Next we want to add our page to the site settings. After all, we don't want to type the _layouts/ContentTypeHierarchy.aspx string every time we want to see our page. For this we need to use a CustomAction feature.

Exercise 14-6. Adding Links to the Custom Page

You may ask yourself how I knew about the GroupId and Sequence values. Go ahead, ask yourself. No answer? I'll provide one for you.

Look in the [12]TEMPLATEFEATURESSiteSettings folder, specifically in the SiteSettings.xml file. Inside you will find all the links on the Site Settings page. Just search for the CustomAction tag with Id="ManageCType", and you have your answer.

If you need a refresher, check out Chapter 5 for the details on CustomAction.

Last Page

Well, at least it is the last page of this chapter. And, since this chapter was about pages…well, you get the pun.

Anyway, we've explored a few different methods for page creation in SharePoint. You should consider this an introduction, however, because the topic is a massive one, and your possibilities are endless. Needless to say (but I'll say it anyway), custom page creation is a very powerful user experience feature. I'd encourage you to experiment and keep exploring, but I may have mentioned this a few times already.

We still have one more chapter to go, in which we will wrap up everything into a single site definition. We will create a site definition, dabble with the site navigation, and explore methods to improve our site creation. Oh, and the killer won't be revealed on this last page. There will be another.

I love these little teasers….

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

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