Bluedog Limited > SharePoint Thoughts > Posts > Fixing the HTML that InsertWebPartIntoWikiPage generates
August 09
Fixing the HTML that InsertWebPartIntoWikiPage generates

Buried deep in the recess of the WebPartPages class, there is a static method that belongs to the WikiEditPage class and its sole purpose is to allow you to insert WebParts into a wiki page.

The static method Microsoft.SharePoint.WebPartPages.WikiEditPage.InsertWebPartIntoWikiPage basically takes 3 parameters – the file, the webpart, and the text position of where you want to inject the web part.

All is fine and dandy except for one thing – the html that is produced by the method is invalid.  The method basically injects the html shown on the slide below. 

image

Take special note of the next to last <div> tag. 

Self-closing tag results in broken wiki page!

<div style='display:none' id="vid_{0}"/>

The tag as written is self-closing.  Unfortunately, when the text is processed by SharePoint’s parser, the self-closing notation is not recognized as valid html and the parser leaves this tag open.  This results in a very broken wiki page because the closing </div> does not exist for the segment of html that was just injected into the page.  Take a look at the image below – 3 start tags but only 2 end tags.

image

The fix is pretty easy.  Create your own version of InsertWebPartIntoWikiPage! 

All you need to do is change that next to last div tag from one that uses the self-closing notation over to one that properly uses an end tag.

image

Once you do this, your html segment will look like...

image

Nice and clean with the right number of start and end tags. More importantly, your wiki page will work as expected.

Later this week, I’ll show you a really cool way to use regex to control the location of the web parts that you’re trying to add to the wiki page.

-Maurice

Reference Post: Regex for Wiki Page regions

Comments

When do you use this?

At my company we have a requirement where certain web parts need to appear on all wiki pages in a particular site collection. Your approach certainly appears to be a viable solution to that problem. However, I didn't know about this at the time I had to solve the problem and came up with a different solution.

Instead, I embedded the web parts in the page layout for the wiki page.

I'm wondering specifically what problem you were trying to solve with this solution. There are problems with embedding the web parts in the layout page and I may end up going to your approach at some point, but am curious about the problem you were trying to solve.

Thanks,
Doug Hemminger
 on 9/27/2011 2:43 PM

Re: Fixing the HTML that InsertWebPartIntoWikiPage generates

Hey Doug,

Embedding web parts into a page and within a web part zone incur a huge penalty of forcing the page to become customized.  Adding web parts programmatically make it easy to adjust the page as though you were doing it via the browser.  The clear advantage is that you are not forcing SharePoint to consider the page as a Customized page.

To more directly answer your question, I use programmatic approaches all the time as pages are built (creation, migration) and general maintenance.

-Maurice
Maurice PratherNo presence information on 9/27/2011 3:03 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


captcha

Please verify the text shown in the image

Attachments


© Bluedog Limited 2004-2012
Aptillon, Inc.
Microsoft Certified Master - SharePoint 2010 & 2007
Sign In