如果您正在SharePoint解决方案中部署一个事件接收器和一个内容类型,那您可以忽略此文。但是,如果您想要把一个事件接收器和一个已经存在的内容类型做关联,以下是示例代码:
//移除已经存在的assembly name,class 和type定义
foreach(SPEventReceiverDefinition definition in contentType.EventReceivers)
{
if(definition.Class != className && definition.Assembly != assemblyName
&& definition.Type != eventReceiverType)
{
continue;
}
definition.Delete();
contentType.Update(true);
break;
}
SPEventReceiverDefinition eventReceiverDefinition = contentType.EventReceivers.Add();
eventReceiverDefinition.Class = className; // String
eventReceiverDefinition.Assembly = assemblyName; // String
eventReceiverDefinition.Type = eventReceiverType; // SPEventReceiverType
eventReceiverDefinition.Data = documentType; // Arbitrary input data (String)
eventReceiverDefinition.Update();
contentType.Update(true);