关于eventhandler的一个例程(SharePoint2007 + VS10)
在SharePoint2007中,自定义写代码,如何在SharePoint中使用该功能,简单总结为:
1)写代码。
2)创建一个强签名strong-name的Assembly.
3)用程序注册.
以下这篇博客写了creating and adding an event handler的全过程。
http://farhanfaiz.wordpress.com/2007/12/31/creating-adding-an-event-handler/15/
1)site下new doc lib - DLOne,add column - Comment。
2)In VS->new project->Class Library.
add reference->change class name->Inherit class
namespace DocLibEventHandler
{
public class DocLibEventHandlerClass : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem doc = properties.ListItem;
doc["Comments"] = “Document has been added”;
doc.Update();
}
//you can use following methods as well
public override void ItemAdding(SPItemEventProperties properties) { }
public override void ItemUpdated(SPItemEventProperties properties) { }
public override void ItemUpdating(SPItemEventProperties properties) { }
}
3) Signing the assembly.
4)Build project F6。
5)Copying into GAC。(直接将工程下的dll文件拖进assembly下)
6)register the Event handler。
这里的方法是新建Console project,并设置为startup project,否则VS10会有报错。
namespace DocLinRegApp{
class Program {
static void Main(string[] args) {
SPSite sp = new SPSite(“http://sitecollection”);
SPWeb website = sp.OpenWeb();
SPList DocLib = website.Lists["DLOne"];
string assm = “DocLibEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1d6b41a3ed9a92cb”;
string class = “DocLibEventHandler.DocLibEventHandlerClass”;
DocLib.EventReceivers.Add(SPEventReceiverType.ItemAdded, assm, class);
DocLib.EventReceivers.Add(SPEventReceiverType.ItemAdding, assm, class);
DocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assm, class); DocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assm, class);
}
}
}
还有一种方法是在sharepoint2007里设置assembly的值等,并且add solution and deploy it。
http://eallies.blog.51cto.com/375118/78815
7)change in document lib,就能看到column中加了内容。
注意:VS10中要选择.NET Framework 3.5,x64和x86的选择也有可能报错。
VS10中如果安装WSPBuilder出问题,不能直接build as wsp,可以通过命令行的方式里生成wsp文件:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe" -if "$(TargetPath)"
该语句也能实现将dll文件在assembly里注册。