Creating an Alternate Version of a Posting

Alternate versions of postings are useful in presenting existing content in different formats. For example, you may want to create a "print friendly" version, at the simplest level, or you may need to create a PDA version, which presents a little more of a challenge. In either case, it's easy enough to create a new template that formats the content appropriately and then create a connected posting. We don't think you should have to create a new posting every time you add content. Instead, there should be a way to simply "reformat" existing content on the fly. It's possible to do just that, by dynamically associating a posting's content with an alternate template. The idea was provided by the earlier version of CMS (2001), which had a direct API call to accomplish what we're doing here (there are good reasons why it was dropped, though). Since CMS 2002 no longer supports that API call, you'll have to write a little code to replicate that functionality. The following code was provided by Pat Miller (of Microsoft) on the Microsoft-hosted newsgroup microsoft.public.cmserver.general; we simply commented it a bit more and changed some variable names to protect the innocent (see Listing 36-3).

Listing 36-3 Repurposing content programmatically dynamic template switching
 public string UrlUsingAlternateTemplate(string templateName) {       // Set a variable to the current CMS Context       CmsHttpContext myContext = CmsHttpContext.Current;       // Set a variable to point to the current posting; we'll use this       // object to get to its template and to that template's       // connected templates.       Posting thisPosting = myContext.Posting;       if ( thisPosting != null )       {             // Retrieve a reference to the current posting's             // template             Template thisTemplate = thisPosting.Template;             if ( thisTemplate != null )             {                   // Use the requested template name to find it in the                   // collection connected templates.                   Template templateToSwitchTo = thisTemplate.ConnectedTemplates[templateName];                   if ( templateToSwitchTo != null )                   {                         // Determine whether the TGI we found is connected                         // to a physical template file.                         if ( templateToSwitchTo.SourceFile != "" )                         {                               // Return the source path and file of the                               // alternate template, along with the query                               // string of the current posting.  The query                               // string will indicate the GUID of the posting                               // (which contains the content) to display                               // within the context of the alternate template.                               return templateToSwitchTo.SourceFile + "?" +                                     Request.QueryString.ToString();                         }                         else                         {                               return null;                         }                   }                   else                   {                         return null;                   }             }             else             {                   return null;             }       }       else       {             return null;       } } 

In Listing 36-3 we're using the connection between the current posting's template and its connected templates. In other words, this function will only work if you have templates with connected templates in your solution. One advantage to this approach is that all connected templates share the same placeholder definitions, so you're assured that content in the original posting shows up on the alternate version. Keep in mind, however, that you're passing in the name of the template as a string. This will only work if all the connected templates have unique names.



Microsoft Content Management Server 2002. A Complete Guide
Microsoft Content Management Server 2002: A Complete Guide
ISBN: 0321194446
EAN: 2147483647
Year: 2003
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net