I decided that the best way to handle this new functionality was to create a ‘WikiFormat’ feature. So how do you create such a feature? Well it happens in 5 stages.
1. Setup
2. Code the feature.xml
3. Code the SPWikiFormatFeatureReceiver
4. Code the SPWikiFormatEventReceiver
5. Deploy your new feature
Step 1
First, we will open up Visual Studio and create a C# class project. You will need to add a Project Reference to Microsoft.SharePoint.
This next step isn’t required but helps me with deployment later on. Create a ‘Template’ folder and directly under it create a ‘Features’ folder. Next create one more folder. This will be the name of your feature. I called mine WikiFormat. We want to create an XML file in the final folder and name it feature.xml.
For completeness, add two C# classes at the root level. One will be your FeatureReceiver (SPWikiFormatFeatureReceiver) and the other will be your Event Receiver (SPWikiFormatEventReceiver). When you are done, your project structure should look something like the diagram below:

You will also need to sign your assembly with a key since we will be deploying the final .DLL to the Global Assembly Cache (GAC).
Step 2
Description="Formats contents of columns into Wiki Content"
Version="1.0.0.0"
Scope="Web"
Hidden="FALSE"
DefaultResourceFile="core"
ReceiverAssembly="SWB.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d44f145d86ae4ad3"
ReceiverClass="SWB.SharePoint.SPWikiFormatFeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/">
Step 3
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SWB.SharePoint
{
class SPWikiFormatFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb("/wiki-test/");
//SPList wikiSite = web.Lists["Wiki_x0020_Pages"];
string pathURL = "/wiki-test/Wiki%20Pages";
SPList wikiSite = web.GetList(pathURL);
string className = "SWB.SharePoint.SPWikiFormatEventReceiver";
string assemblyName = "SWB.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d44f145d86ae4ad3";
wikiSite.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
wikiSite.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);
wikiSite.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assemblyName, className);
web.Dispose();
site.Dispose();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties){}
public override void FeatureInstalled(SPFeatureReceiverProperties properties) {}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {}
}
Step 4
//Please not that the < character has been replaced with a { so format changes would not occur in this blog post.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections;
namespace SWB.SharePoint
{
class SPWikiFormatEventReceiver : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
}
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
FormatItem(properties);
}
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
FormatItem(properties);
}
private void FormatItem(SPItemEventProperties properties)
{
try
{
//Set update flag
bool updateWikiContent = false;
//Stop other event Firing();
this.DisableEventFiring();
//Get the list item from the event properties
SPListItem item = properties.ListItem;
SPView defaultView = item.ParentList.DefaultView;
//Build the Wiki Content
StringBuilder wikiContent = new StringBuilder();
SPViewFieldCollection fields = defaultView.ViewFields;
for (int i = 0; i < fields.Count - 1; i++)
{
try
{
string columnName = fields[i].ToString();
if (columnName != "Edit")
{
columnName = columnName.Replace("_x0020_", " ");
string formatedName = "{h3}" + columnName + "{/h3}{br}";
string inputValue = item[fields[i]] + "{br}";
if (item[fields[i]].ToString().Length > 0)
{
updateWikiContent = true;
wikiContent.Append(formatedName);
wikiContent.Append(inputValue);
}
}
}
catch { }
}
if (updateWikiContent)
{
item["WikiField"] = wikiContent;
}
//Update the list item to apply the new value
item.Update();
}
catch (Exception ex)
{
properties.ErrorMessage = ex.ToString();
properties.Cancel = true;
}
finally
{
//Re-enable event firing
this.EnableEventFiring();
}
}
}
}
Step 5
Now, it is time to deploy. The first thing we will want to do is to move the newly created DLL to the GAC. I prefer the drag and drop method to the GACUTIL but either way works great. Once the DLL is in place, you can copy your folder structure into the 12 hive (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12). If you elected not to use the same file structure I did, you can copy your feature folder (WikiFormat) to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES.
Now we want to install and activate the feature. To install the feature you will need to use the STSADM utility. This is located in the bin folder of the 12 hive.
The syntax is as follows:
Stsadm –o installfeature –name WikiFormat
Once you install the feature, you can activate it one one of several ways. You can use the STSADM tool again using the activeatefeature operation or you can activate the feature at the site level. Don’t forget your IISRest!!!
You are now ready to test your new feature. The code provided reformats the wiki on adds and updates.


0 comments:
Post a Comment