Subscribe in a reader

Add to Technorati Favorites Overriding Sharepoint Page Render method

Overriding Sharepoint Page Render method

by JamieMcAllister 12/18/2008 11:35:00 AM

Today’s article is taken from another real world problem.

 

One of my MOSS portals hosts a large amount of Business Process documentation as MOSS Publishing Pages. The original MS Word based documentation that these pages sprang from now bears little relation to the most recent guidance in MOSS.

 

The business want to be able to transform the MOSS Publishing Pages into “MS Word 2003” compatible documents at will. You might have heard of Document Converters, a MOSS feature that converts documents into publishing pages? This requirement was like that, but in reverse, and definitely not available out of the box.

 

I quickly identified a commercial component that converts HTML mark-up to Rich Text Format documents. This seemed like an ideal path to take. RTF documents are feature rich enough for what was wanted and compatible with a wide variety of tools. RTF is an International Standard format.

 

This still left the challenge of where to obtain the HTML representing the Publishing Page content. Using the Sharepoint API it’s easy enough to get hold of the mark-up within the fields of any publishing page. That isn’t enough though because the publishing page you see on screen is created from the content, the page layout, the master page, and CSS melded together.

 

Back in my ASP.NET days I would’ve solved this problem by overriding the Render method of my ASP.NET page. This would give me the very same mark-up that was being sent to the browser. Ideal as that sounds I realised that I’d not seen a single example of anyone tapping into the Render method of a MOSS Publishing Page anywhere! How do you do it?

 

The only clue I found was on Zac Smith’s website;

http://www.trinkit.co.nz/blog/archive/2007/04/19/guide-to-making-sharepoint-xhtml-compliant.aspx

 

Zac had overridden the Render method of Master Pages. His requirement was to clean up the Sharepoint mark-up to make it more XHTML compliant. Alas, Zacs solution didn’t suit me because he was using inline code in the master page. Enabling Inline Code opens up a security can of worms that I don’t even want to think about!

 

As an alternative, I immediately thought of Andrew Connells article on Sharepoint Code Behind pages; http://www.andrewconnell.com/blog/articles/UsingCodeBehindFilesInSharePointSites.aspx

 

Andrews article deals with the creation of an application page with code behind. It didn’t take much experimentation before I was able to create code behind files for Master Pages, and Page Layouts too. This was just what I wanted.

 

In the end the Master Page code behind option suited my problem best. I created a custom master page based on the existing custom master page that my portal uses, and added a code behind. Within five minutes I was able to manipulate my Publishing Page mark-up however I liked by overriding Render within the code behind!

 

After a relatively small amount of fun and games, my Render method looked something like this;

 protected override void Render(HtmlTextWriter writer)   

{

        StringBuilder sb = new StringBuilder();

        StringWriter sw = new StringWriter(sb);

        hWriter = new HtmlTextWriter(sw);

         base.Render(hWriter); //Get the rendered page

         //Write out string to RTF Component HERE

        MyVendor.HtmlToRtf.Converter converter = new MyVendor.HtmlToRtf.Converter();

                string rtfFile = converter.ConvertString(sb.ToString());

         if (rtfFile.Length > 0)

        {

            Response.Buffer = true;

            Response.Clear();

            Response.ContentType = "application/msword";

            Response.Write(rtfFile);

            Response.Flush();

            Response.End();

        }

    }

 

So there you have it - full control of your rendered mark-up from Sharepoint. This could help you with XHTML Compliance, Document Output, Mobile Device Rendering, and lots of other neat applications that just need imagining!

     

Currently rated 1.5 by 2 people

  • Currently 1.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

.NET | Mobile | Sharepoint | WCM

Related posts

Comments

2/13/2009 7:06:48 PM

Ted Assur

Hi Jamie:

I have done similar work (successfully) for XHTML compliance. Recently, we're going back through and moving many lines of code from a dll in the GAC to code that will be loaded into the bin with CAS.

Strangely, I've found a couple of areas where rendering override begins to fail when removed from the GAC. I begin to get strange security errors that I would not expect from rendering overrides. Specifically, when the override methods are removed, the reduced-security code works fine, when it's reintroduced in a bin assembly, it begins to have problems again. Move the dll back into the GAC, and it works fine.

Have you seen anything similar with your efforts? Are GACing your master page code behind with this Render override?

Ted Assur us

2/17/2009 3:24:48 PM

Jamie

Ted,

I did see some issues when using CAS. Much the same as you it seems, I got some errors that I wouldn't expect with Render override. We had no problems going with the GAC and that's where we settled.

To compare notes, the CAS settings I had were represented by this excerpt from the Manifest.xml file;

<CodeAccessSecurity>
<PolicyItem>
<PermissionSet class="NamedPermissionSet" version="1" Description="Allow access to Base Render Method">
<IPermission class="AspNetHostingPermission" version="1" Level="Minimal"/>
<IPermission class="SecurityPermission" version="1" Flags="Execution" />
<IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" version="1" Unrestricted="True" />
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="True" />

</PermissionSet>
<Assemblies>
<Assembly Name="HTMLToRTF" Version="1.0.0.0" PublicKeyBlob="0x00240000048000009400000006020000002400005253413100040000010001009766A88EC2E06ADFFEA91731D98385FD14084E5C8DBD417B7C4129617612BA9B2773B18CFA930D584C49C45CC9E07DAFA109EA45B4D81ECC5C258767E16D9B8DA6868E0EFF166D816F102D71FCC2A3C1F7560239A2E543CE92D9B106527F32FD7E56B307E35EA9EC6B653443F38D3CE9D42F87FEFC93898EB5D3D246ED840FA0" />
</Assemblies>
</PolicyItem>
</CodeAccessSecurity>

Jamie gb

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Jamie McAllister Jamie McAllister
Manchester (UK) Based Consultant for Northridge Solutions Ltd.


Jamie McAllister Linked In
        
        
        
        

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in

Disclaimer: The software and source code on this website is provided "AS IS"
with no warranties of any kind. The entire risk arising out of the use or
performance of the software and source code is with you.