覆盖共享点列表事件
我需要向列表的添加事件添加自定义处理。然而,该事件似乎并未触发。我希望在 EventReceivers 中定义的自定义处理程序能够在自定义类型的新项目添加到列表时触发。 我将不胜感激任何建议。 这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace MyTestGroupListFeature
{
class MyTestGroupListFeature : SPFeatureReceiver
{
private const string LISTNAME = "Acquisition Groups";
private const string CONTENTTYPE_LISTNAME = "Contenttype List";
private const string FOO = "FOO";
private const string SITE_TEMPLATE_EXTENSION = ".stp";
public override void FeatureInstalled
(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling
(SPFeatureReceiverProperties properties)
{
FeatureDeactivating(properties);
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb web = properties.Feature.Parent as SPWeb;
SPList list = GetList(web, LISTNAME);
SPSite site = web.Site;
site.AllowUnsafeUpdates = true;
SPContentType newCType = CreateSourceSystemContentType(web);
SPList listct = web.Lists[CONTENTTYPE_LISTNAME];
AddEventReceiverToList(listct);
AddContentTypeToList(web, listct, newCType);
}
catch { }
}
/// <summary>
/// Creates a content type for Source System
/// </summary>
private SPContentType CreateSourceSystemContentType(SPWeb site)
{
//Create site columns
string acronymFieldName = site.Fields.Add("Acronym", SPFieldType.Text, true);
SPFieldText acronymField = (SPFieldText)site.Fields.GetFieldByInternalName(acronymFieldName);
acronymField.Group = "AQSIDM Columns";
acronymField.Update();
string nameFieldName = site.Fields.Add("Name", SPFieldType.Text, true);
SPFieldText nameField = (SPFieldText)site.Fields.GetFieldByInternalName(nameFieldName);
acronymField.Group = "AQSIDM Columns";
acronymField.Update();
string descriptionFieldName = site.Fields.Add("Description", SPFieldType.Text, true);
SPFieldText descriptionField = (SPFieldText)site.Fields.GetFieldByInternalName(descriptionFieldName);
descriptionField.Group = "AQSIDM Columns";
descriptionField.Update();
// Get the parent content type.
SPContentTypeId itemCTID = new SPContentTypeId("0x01");
SPContentType itemCT = site.AvailableContentTypes[itemCTID];
//Create SourceSystem content type:
SPContentType sourceSystemCT = new SPContentType(itemCT, site.ContentTypes, "Source System");
sourceSystemCT.Group = "Source System Content Types";
//Add columns to content type
sourceSystemCT = site.ContentTypes[sourceSystemCT.Id];
SPFieldLink acronymLink = new SPFieldLink(acronymField);
sourceSystemCT.FieldLinks.Add(acronymLink);
//
SPFieldLink nameLink = new SPFieldLink(nameField);
sourceSystemCT.FieldLinks.Add(nameLink);
//
SPFieldLink descriptionLink = new SPFieldLink(descriptionField);
sourceSystemCT.FieldLinks.Add(descriptionLink);
//
//sourceSystemCT.Update();
string assemblyName = System.Reflection.Assembly.GetEntryAssembly().FullName;
string ctReceiverName = "MyTestGroupListFeature.EventReceivers";
sourceSystemCT.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, ctReceiverName);
sourceSystemCT.Update(true);
site.ContentTypes.Add(sourceSystemCT);
return sourceSystemCT;
}
/// <summary>
/// Adds a custom content type to site's list
/// </summary>
private void AddContentTypeToList(SPWeb web, SPList list, SPContentType ct)
{
list.ContentTypesEnabled = true;
list.ContentTypes.Add(ct);
list.Update();
// Add the item:
SPListItem newItem = list.Items.Add();
newItem["Acronym"] = "Acronym Field Added";
newItem.Update();
}
/// <summary>
/// Programmatically add new event receivers
/// </summary>
private void AddEventReceiverToList(SPList list)
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
eventReceiver.Name = "On Updating";
eventReceiver.Type = SPEventReceiverType.ItemAdding;
eventReceiver.SequenceNumber = 200;
eventReceiver.Assembly = assembly.FullName;
eventReceiver.Class = "MyTestGroupListFeature.EventReceivers";
eventReceiver.Update();
}
}
class EventReceivers : SPItemEventReceiver
{
/// <summary>
/// Test behavior when adding an item
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SourceSystem ss = new SourceSystem();
ss.Name = "sharepointss";
ss.Acronym = "sharepoint acronym";
ss.Description = "desc";
ss.EndPoint = new Uri(@"\testURI");
ss.Update("foo");
ss.Create(String.Format("SourceSystem_Create_SP_{0:o}", DateTime.Now));
}
}
}
I need to add custom handling to a lists' adding event. However, the event does not appear to be firing. I would like my custom handler defined in EventReceivers to fire anytime a new item of the custom type is added to the list.
I would appreciate any suggestions.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace MyTestGroupListFeature
{
class MyTestGroupListFeature : SPFeatureReceiver
{
private const string LISTNAME = "Acquisition Groups";
private const string CONTENTTYPE_LISTNAME = "Contenttype List";
private const string FOO = "FOO";
private const string SITE_TEMPLATE_EXTENSION = ".stp";
public override void FeatureInstalled
(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling
(SPFeatureReceiverProperties properties)
{
FeatureDeactivating(properties);
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb web = properties.Feature.Parent as SPWeb;
SPList list = GetList(web, LISTNAME);
SPSite site = web.Site;
site.AllowUnsafeUpdates = true;
SPContentType newCType = CreateSourceSystemContentType(web);
SPList listct = web.Lists[CONTENTTYPE_LISTNAME];
AddEventReceiverToList(listct);
AddContentTypeToList(web, listct, newCType);
}
catch { }
}
/// <summary>
/// Creates a content type for Source System
/// </summary>
private SPContentType CreateSourceSystemContentType(SPWeb site)
{
//Create site columns
string acronymFieldName = site.Fields.Add("Acronym", SPFieldType.Text, true);
SPFieldText acronymField = (SPFieldText)site.Fields.GetFieldByInternalName(acronymFieldName);
acronymField.Group = "AQSIDM Columns";
acronymField.Update();
string nameFieldName = site.Fields.Add("Name", SPFieldType.Text, true);
SPFieldText nameField = (SPFieldText)site.Fields.GetFieldByInternalName(nameFieldName);
acronymField.Group = "AQSIDM Columns";
acronymField.Update();
string descriptionFieldName = site.Fields.Add("Description", SPFieldType.Text, true);
SPFieldText descriptionField = (SPFieldText)site.Fields.GetFieldByInternalName(descriptionFieldName);
descriptionField.Group = "AQSIDM Columns";
descriptionField.Update();
// Get the parent content type.
SPContentTypeId itemCTID = new SPContentTypeId("0x01");
SPContentType itemCT = site.AvailableContentTypes[itemCTID];
//Create SourceSystem content type:
SPContentType sourceSystemCT = new SPContentType(itemCT, site.ContentTypes, "Source System");
sourceSystemCT.Group = "Source System Content Types";
//Add columns to content type
sourceSystemCT = site.ContentTypes[sourceSystemCT.Id];
SPFieldLink acronymLink = new SPFieldLink(acronymField);
sourceSystemCT.FieldLinks.Add(acronymLink);
//
SPFieldLink nameLink = new SPFieldLink(nameField);
sourceSystemCT.FieldLinks.Add(nameLink);
//
SPFieldLink descriptionLink = new SPFieldLink(descriptionField);
sourceSystemCT.FieldLinks.Add(descriptionLink);
//
//sourceSystemCT.Update();
string assemblyName = System.Reflection.Assembly.GetEntryAssembly().FullName;
string ctReceiverName = "MyTestGroupListFeature.EventReceivers";
sourceSystemCT.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, ctReceiverName);
sourceSystemCT.Update(true);
site.ContentTypes.Add(sourceSystemCT);
return sourceSystemCT;
}
/// <summary>
/// Adds a custom content type to site's list
/// </summary>
private void AddContentTypeToList(SPWeb web, SPList list, SPContentType ct)
{
list.ContentTypesEnabled = true;
list.ContentTypes.Add(ct);
list.Update();
// Add the item:
SPListItem newItem = list.Items.Add();
newItem["Acronym"] = "Acronym Field Added";
newItem.Update();
}
/// <summary>
/// Programmatically add new event receivers
/// </summary>
private void AddEventReceiverToList(SPList list)
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
eventReceiver.Name = "On Updating";
eventReceiver.Type = SPEventReceiverType.ItemAdding;
eventReceiver.SequenceNumber = 200;
eventReceiver.Assembly = assembly.FullName;
eventReceiver.Class = "MyTestGroupListFeature.EventReceivers";
eventReceiver.Update();
}
}
class EventReceivers : SPItemEventReceiver
{
/// <summary>
/// Test behavior when adding an item
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SourceSystem ss = new SourceSystem();
ss.Name = "sharepointss";
ss.Acronym = "sharepoint acronym";
ss.Description = "desc";
ss.EndPoint = new Uri(@"\testURI");
ss.Update("foo");
ss.Create(String.Format("SourceSystem_Create_SP_{0:o}", DateTime.Now));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
激活该功能后,您是否验证事件接收器是否已附加到列表中?这可以通过编程方式或使用 SharePoint Manager 等工具来完成。
After you activated the feature, did you verify whether or not the event receiver was attached to the list? This can be done programatically or using tools such as SharePoint Manager.