Wiki Page Libraries are "Special"
Written on February 15, 2008 by Bryan
I was looking at the best way to export and import a wiki page library within a site that is not specifically a wiki site. There are not a lot of good ways to export and import lists in general. The only two ways I am really even aware of are:
- Saving as a list template and using it to create a new list
- Using SharePoint Designer to export and import a specific list
Neither of these methods worked out very well when trying to migrate a wiki page library. A few problems arose when I tried both of them:
- The behavior that links the root folder of the document library to the Home.aspx wiki page breaks.
- It appeared that only some of the content was being exported and imported. I’m not really sure what would cause some content to export and some to not, but I speculated that this may have been related to having multiple versions of each page.
- Of course, the created and modified dates are not retained and the users associated with each page are not maintained. This didn’t matter so much in my case though.
I decided to resort to taking a look at the API and trying to use the object model to do the work. The content copying problem seemed easy enough to resolve using the SPItem.CopyTo method for each item in the source library.
SPDocumentLibrary srcWiki = (SPDocumentLibrary)srcWeb.Lists[srcWikiName];
Guid dstWikiGuid = dstWeb.Lists.Add(srcWiki.Title, srcWiki.Description, dstWeb.ListTemplates[“Wiki Page Library”]);
foreach (SPListItem item in srcWiki.Items)
{
item.CopyTo(dstUrl + srcWiki.Title + “/” + item.File.Name);
}
The content copied fine, but it still didn’t seem to fix the welcome page issue. Going to the root of the wiki page library would still take you to the Forms/AllItems.aspx page, the default view. It took some help from .NET Reflector to uncover how this behavior works when creating a Wiki Page Library through the UI.
Unlike site definitions which have special folders inside the 12 Hive under TEMPLATE\SiteTemplates, list definitions are defined as features. The “Wiki Page Library” list definition is associated with the WebPageLibrary feature. There are no DLLs associated with this feature, but rather a lot of XML that defines the entire library schema for wikis. However, when you create a Wiki Page Library through the UI, it appears that there is another feature that also gets activated called WikiWelcome. This feature does have a DLL that it uses with a class called SPWikiWelcomeFeatureReceiver. Ah, a simple feature receiver event handler. I used .NET Reflector to take a look. There is very little code involved.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb parent = (SPWeb) properties.Feature.Parent;
SPFolder rootFolder = parent.RootFolder;
rootFolder.WelcomePage = SPUtility.GetLocalizedString([…]);
rootFolder.Update();
}
I had actually stumbled across the RootFolder.WelcomePage by accident thinking it would solve my problem before I had even found this, but alas I had done a list.Update() instead of a rootFolder.Update() at the end so, of course, I missed the boat. However, now I see that this is really all there is to it. I added one extra line so that the library will be displayed on the Quick Launch if it was set to display like that on the original library, another thing that doesn’t seem to get copied over automatically. Here’s what I added:
dstWiki.OnQuickLaunch = srcWiki.OnQuickLaunch;
dstWiki.RootFolder.WelcomePage = srcWiki.RootFolder.WelcomePage;
dstWiki.RootFolder.Update();
dstWiki.Update();
The SPItem.CopyTo() method copies the whole item including versioning, so it should take care of the content issue as far as I have been able to tell so far. However, it doesn’t appear to solve the problem of the created and modified dates or the user who created the items getting copied over. Since it’s not that important, I didn’t handle this, but I could conceivably grab this data from the source list and explicitly set it on the new one if I wanted to try to make things exactly the same as the original list. I need to do a little bit more testing perhaps, but now I have a console app utility for migrating wiki page libraries from one site to another.

Leave a comment
You must be logged in to post a comment.