The code sample in this chapter assumes that managing CMS assets (Channels, Postings, Templates, and so on) in a Web Property is understood and a good portion of the CMS PAPI is instinctive. Therefore, screen shots will only be shown for results and directives specific to this chapter. Please refer to other chapters in this book for CMS non Web Service details. Also, the simple error handling in the code throughout the code sample isn't meant to be the model for how to handle CMS exceptions; there is an application code block from Microsoft on how to do exception handling in .NET. But before we can begin coding our Web Services, we need to do a little setup work. The BOTS Consulting Web site already has a channel structure and navigation that will accommodate what we will call Job Offerings. However, there are currently no general, summary, or detail templates or postings in that section of the BOTS Web site. So we will need to create them. Not to worry we'll keep it very simple.
NOTE: You don't need to have BOTS to create the code in this chapter. Just create or use channels, templates, and postings in your own site and change the names accordingly. The steps we will go through to set up the BOTS Web site follow, first in brief and then in detail. It should take no more than about ten minutes to set up this environment.
Set Up Channel HierarchyUsing the Site Manager, ensure that we have, at a minimum, the channel hierarchy characterized in Table 33-2. The jobs channel was called "growth" for Growth Opportunities in other parts of the book. But "jobs" for Job Offerings seemed a better descriptor for this code sample. When you are done, it should look something like Figure 33-2. Figure 33-2. Channel hierarchy for BOTS Job Offerings
Set Up TemplatesUsing the VS.NET MCMS Template Explorer, create the logical templates characterized in Table 33-3 (take the default values for all unspecified properties). We added the two circled templates in Figure 33-3. Note that they are not yet associated with a template file, so the icon looks broken and there is a red check mark because they are still checked out. Figure 33-3. Templates for BOTS Job OfferingsSet Up Template Files and Associate with TemplatesUsing the VS.NET Solution Explorer, create two physical template files in the Templates directory by copying the existing generic.aspx template file. Name them jobsummary.aspx and jobdetail.aspx, and save them to disk.
NOTE: If you aren't using the BOTS code, the generic.aspx template file is just a stub that includes the header, footer, and left navigation used on the Web site. It also has the default console and a Web Form literal called BodyTitle in the content portion of the file. You could easily use a standard MCMS template file and add your own custom interface. Using the VS.NET MCMS Template Explorer, associate the Template File property of the JobSummary template with the jobsummary.aspx file, and the TemplateFile property of the JobDetail template with the jobdetail.aspx file. The icon for each template should become unbroken, and if you check them, the red check mark should go away. Back in the VS.NET Solution Explorer, edit the jobsummary.aspx file. Add a Web Form placeholder control named SummaryOfPostings just below the existing literal control in the content portion of the file. It should look something like Figure 33-4. Figure 33-4. jobsummary.aspxEdit the jobdetail.aspx file. Add an HtmlPlaceholderControl named JobDescription just below the existing literal in the content portion of the file. Also, choose JobDescription from the drop-down list for the PlaceholderToBind property. The result should look something like Figure 33-5. Figure 33-5. jobdetail.aspxCode the Template FilesReplace the Page_Load function in the jobsummary.aspx template file with the following code: private void Page_Load(object sender, System.EventArgs e) { HtmlAnchor cmsPostingLink; Literal cmsPostingDate; CmsHttpContext cmsContext = CmsHttpContext.Current; //Show the Posting DisplayName at the top of the content BodyTitle.Text = "<b>" + HttpUtility.HtmlEncode( CmsHttpContext.Current.Posting.DisplayName) + "</b>"; //Go through all the Postings in this Channel and Add details to //the SummaryOfPostings Web Form Placeholder for each Posting //sorted by StartDate //Do not include the Posting named default PostingCollection cmsChannelPostings = cmsContext.Channel.Postings; if (cmsChannelPostings != null) { cmsChannelPostings.SortByStartDate(false); foreach (Posting cmsPosting in cmsChannelPostings) { if (cmsPosting.Name != "default") { //Format Hyperlink cmsPostingLink = new HtmlAnchor(); cmsPostingLink.HRef = cmsPosting.Url; cmsPostingLink.InnerText = cmsPosting.DisplayName; cmsPostingLink.Attributes.Add("style","COLOR: gray"); SummaryOfPostings.Controls.Add(new LiteralControl("<p>")); SummaryOfPostings.Controls.Add(cmsPostingLink); SummaryOfPostings.Controls.Add(new LiteralControl( "<br />Posted: " + cmsPosting.StartDate.ToLongDateString())); SummaryOfPostings.Controls.Add(new LiteralControl("</p>")); } } } } The code basically shows a URL and StartDate for each posting in the channel except the default posting. Ensure that the namespace and public class (initially called "generic" if copied from generic.aspx) near the top of the file are unique in your project. You may want to provide a more detailed comment. namespace botsconsulting.Templates { /// <summary> /// Summary description for jobsummary. /// </summary> public class jobsummary : System.Web.UI.Page This is the only custom code in this template file. Replace the Page_Load function in the jobdetail.aspx template file with the following code: private void Page_Load(object sender, System.EventArgs e) { //Show the Posting DisplayName at the top of the content BodyTitle.Text = "<b>" + HttpUtility.HtmlEncode( CmsHttpContext.Current.Posting.DisplayName) + "</b>"; } Ensure that the namespace and public class (initially called "generic" if copied from generic.aspx) near the top of the file are unique in your project. You may want to provide a more detailed comment. namespace botsconsulting.Templates { /// <summary> /// Summary description for jobdetail. /// </summary> public class jobdetail : System.Web.UI.Page This is the only custom code in this template file. Build the Solution in VS.NETThis is an important step don't miss it. You can use Ctrl-Shift-B or choose Build Solution from the Build menu. Clearly, but hopefully unnecessary, you must deal with any errors before continuing. Create PostingsOnce the build is successful, browse to http://localhost/botsconsulting/careers/jobs in Internet Explorer.
NOTE: For the top navigation to work properly, you may want to create a posting named "default" in the careers channel. This posting can be based upon any template that allows navigation to the channel's children. However, it isn't required for this code sample to function. You could even create the default posting in the careers channel using the JobSummary template if you like. Give it a name of "default" and a display name something like "Check out our Job Offerings." Follow these steps to create the default posting based upon the Job Summary template in the jobs channel:
Figure 33-6 shows what this default summary page will look like after the next steps are complete. Figure 33-6. Summary posting in Internet ExplorerFollow these steps to create the default posting based upon the Job Summary template in the jobs channel:
Clicking the Job Offerings link on the left navigation should take you to the posting in the jobs channel named default, which should look something like Figure 33-6. Clicking the SQL Server DBA link should take you back to the detail page for that posting, and it should look something like Figure 33-7. Figure 33-7. Detail posting in Internet ExplorerInstall Woodgrove Sample Data SiteThe Woodgrove Bank sample that comes with CMS will be used to syndicate postings to the BOTS Consulting Web site. So if you don't already have Woodgrove installed, you need to do that now. There are detailed instructions for installing the sample both in the CMS help file and on MSDN. As always, you have the option of using your own site and just changing the names as needed. That is all the setup we need to do, so let's code up a Web Service! |