Recently I was challenged with the task to set the layout and content of a wiki page when a new page is added to a team site. As I am used to work with SharePoint publishing, the task sounded easy, but I was wrong.
Text Layout
My first path was to figure out where SharePoint puts the wiki “text layouts”. I discovered this is not how it works. The layouts available for wiki’s are not configurable anywhere.
But using some PowerShell it was easy to get and set the layout as it is the HTML content of the “Wiki Field” column in the list.
The HTML content consists of two parts. The layout table and the layout data.
The layout data describes visibility of the header and footer and the number of columns.
Event receiver
To set the content the first thing in my mind was to add an
ItemAdding
event receiver,
associated with ListTemplateId 119
(WebPageLibrary).
I deployed the solution and added a page and 🎉: no content!
Using the debugger to verify my event receiver was triggered, I went to the next option:
adding an ItemAdded
event receiver.
This time I got an exception the page was already modified by another user.
Refreshing the page gave me the default content.
So, this told me 2 things:
- It is possible to set default content
- I forgot to set the
Synchronize
property
So, after fixing the second thing, I deployed once again and got: no content!
As I used ListItem.Update
method in my receiver,
I got a version history where it showed the content was set, but the final version still ended up empty.
When faced with utter desperation, working with SharePoint has taught me you always have an escape: launch Reflector.
There I found this gem of code in the SubmitBtn_Click
method of the CreateWebPage Class
:
So, no matter what I do in ItemAdding
or ItemAdded
, the content always ends up empty!
After this discovery, the fix was removing the code from the ItemAdding
and ItemAdded
events and moving it to the
ItemUpdated
method
(synchronous) and added a check if the WikiField content is an empty string.
Now every wiki page I add to the team site contains the correct text layout and contains the default HTML.