首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试EventReceiver?

如何测试EventReceiver?
EN

Stack Overflow用户
提问于 2012-09-07 18:59:17
回答 2查看 10.1K关注 0票数 0

我几乎疯了,试图让一个SharePoint列表事件接收器工作。我已经在VS中创建了一个EventReceiver项目,并且可以调试它,但是断点不能工作。基本上,这是同一个问题,很多人都有过,但他们的解决方案似乎都行不通。我之前发布过这篇文章,我认为我的事件接收器代码应该可以工作,但我似乎无法让它在列表上工作。(下面粘贴我的代码)

基本上,我所需要的就是让事件接收者重命名上传的文档。考虑一下这种情况,如果一个文档被上传为Client A文档,并且是第一个文档,那么它应该被称为Client A文档1。如果下一个上传的文档称为Client A文档,那么它应该重命名为Client A Document 2,依此类推。现在,如果上传了另一个名为Client文档的文档,那么它应该只是Client文档1,因为没有同名的其他文档。现在,我认为下面的代码实现了这种行为(代码是由罗伯特·切里尔帮助编写的!)但我不知道怎么测试。

我是上传一个文档还是创建一个新的文档?我试过这两种方法,但都没有用,有人想过如何做到这一点吗?我开始对这个要求失去理智了。

代码语言:javascript
复制
public override void ItemAdding(SPItemEventProperties properties)
{
   base.ItemAdding(properties);

   SPListItem item = properties.ListItem;

   if (item == null || item["Name"] == null) //item["Name"] == null)
       return; //or better yet, log 

   string oldFileName = item["Name"].ToString();

   int positionOfPeriod = oldFileName.LastIndexOf(".");
   string tempFileName = oldFileName.Substring(0, positionOfPeriod);

   SPQuery query = BuildArbitraryQuery(properties.List, "Name", tempFileName, true);
   int count = properties.List.GetItems(query).Count;
   String fileName, fileExtension;

   if (positionOfPeriod == -1)
   {
       fileName = oldFileName;
       fileExtension = "";
   }
   else
   {
       fileName = oldFileName.Substring(0, positionOfPeriod);
       fileExtension = oldFileName.Substring(positionOfPeriod);
   }

   string newFileName = fileName + "-xx" + count.ToString() + fileExtension;

   item["Name"] = newFileName;

   Console.WriteLine("New File Name: " + newFileName);

   try
   {
       properties.Web.AllowUnsafeUpdates = true;
       EventFiringEnabled = false;

       item.Update();
   }
   finally
   {
       properties.Web.AllowUnsafeUpdates = false;
       EventFiringEnabled = true;
   }
}
/// <summary> 
/// Builds an arbitrary SPQuery which filters by a single column value. 
/// </summary> 
/// <param name="list">The list you will run the query against.</param> 
/// <param name="columnDisplayName">The Display Name of the column you want to filter.</param> 
/// <param name="value">The value to filter against.</param> 
/// <returns>A new SPQuery object ready to run against the list.</returns> 
public static SPQuery BuildArbitraryQuery(SPList list, string columnDocumentName, string value, bool deepSearch)
{
   if (list == null)
       throw new ArgumentNullException("You cannot pass a null list to Helper.BuildArbitraryQuery.");

   if (!list.Fields.ContainsField(columnDocumentName))
       throw new ArgumentException("The SharePoint List \"" + list.Title + "\" does not contain the Field \"" + columnDocumentName + "\".");

   string internalName = list.Fields[columnDocumentName].InternalName;
   SPQuery query = new SPQuery();
   query.Query = "<Where><Eq><FieldRef Name=\"" + internalName + "\"/><Value Type=\"Text\">" + value + "</Value></Eq></Where>";

   if (deepSearch)
       query.ViewAttributes += "Scope='RecursiveAll'";

   return query;
}

EDIT:-------------------------------------------------- Ok,所以我做了一些测试,从相同的项目类型(事件接收者)开始,创建了一个非常简单的ItemAdded方法,将列表项的名称更改为当前日期。现在,这可以在自定义列表上使用,但我似乎无法让它与文档库一起工作。

因此,从这个小测试中,我知道我可以使用F5将事件重新定义器注册到自定义列表(沙箱解决方案)并进行调试,但是文档库有什么不同呢?而我粘贴的代码是否不适合我在文档库上所做的工作呢?

这是我在小测试中使用的代码,但它不适用于文档库,即使我为文档库而不是自定义列表创建了一个新的项目类型(在ItemAdded中)

代码语言:javascript
复制
       SPListItem currentItem = properties.ListItem;
       currentItem["Title"] = DateTime.Now.ToString();
       currentItem.Update();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-07 19:39:38

以下是你应该做的事情:

  1. “重新启动IIS”,如果使用DLL,则卸载它们“
  2. 将DLL及其pdb文件放入适合此DLL的GAC文件夹中。
  3. 打开SharePoint网站,以便启动新的w3wp进程。
  4. 将VS项目附加到w3wp中,确保在“附加到进程”对话框中选择托管代码。
  5. 尝试上传一个文件6-你应该能够抓住断点现在。

您可以在构建后事件中调用Shell脚本来自动化所有这些步骤。

票数 1
EN

Stack Overflow用户

发布于 2012-09-07 19:07:49

你到底是怎么调试的?您需要附加到适当的w3wp.exe进程并添加一个项,如果断点不能工作,这通常是因为上一次部署的dll版本比您在visual studio中的代码更新(即使是最轻微的更改也会导致这种差异,比如新行等等)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12323916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档