“由于对象的当前状态,操作无效”部署 sharepoint 事件接​​收器时

发布于 2025-01-07 07:11:48 字数 1526 浏览 0 评论 0原文

我有一个简单的事件接收器,当我创建构造函数时,我在部署步骤中遇到错误,我仔细检查了功能、Elements.xml 文件和我的代码中的事件接收器范围,奇怪的是当我删除构造函数时(其中我使用服务定位器来获取我的实现类的实例)它工作正常。

我的代码是这样的:

    private INotificationService iNotificationService;
    public CongeER()
    {
        SPSite currentSite = SPContext.Current.Site;
        IServiceLocator locator = SharePointServiceLocator.GetCurrent(currentSite);

        try
        {
            iNotificationService = locator.GetInstance<INotificationService>();
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }

   public override void ItemAdded(SPItemEventProperties properties)
   {
       iNotificationService.NotifyByMail();
       base.ItemAdded(properties);
   }

这是我的 stackTrace:

  Feature Activation: Threw an exception, attempting to roll back.
  Exception: System.InvalidOperationException: 
  L'opération n'est pas valide en raison de l'état actuel de l'objet.     
  à Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionContentTypeAndEventReceiverBindings(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce)     
  à Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)     
  à Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boo...

I have a simple event receiver, when I create a constructor I got the error in deployment step, I double checked the event receiver scope in feature, Elements.xml file and my code, the weird thing is when I remove the constructor(where I use service locator to get an instance of my implementation class) it works fine.

my code is something like this:

    private INotificationService iNotificationService;
    public CongeER()
    {
        SPSite currentSite = SPContext.Current.Site;
        IServiceLocator locator = SharePointServiceLocator.GetCurrent(currentSite);

        try
        {
            iNotificationService = locator.GetInstance<INotificationService>();
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine(ex.Message);
        }
    }

   public override void ItemAdded(SPItemEventProperties properties)
   {
       iNotificationService.NotifyByMail();
       base.ItemAdded(properties);
   }

here is my stackTrace:

  Feature Activation: Threw an exception, attempting to roll back.
  Exception: System.InvalidOperationException: 
  L'opération n'est pas valide en raison de l'état actuel de l'objet.     
  à Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionContentTypeAndEventReceiverBindings(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce)     
  à Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)     
  à Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boo...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋凉 2025-01-14 07:11:48

如果您无法使用 SharePoint 警报功能,并且不需要可以轻松发送电子邮件的工作流,则应删除构造函数并将其代码放入 ItemAdded 方法中。

If you cannot use SharePoint Alert functionality and you do not need a workflow that can also send an email with minimum efforts you should remove the constructor and put its code into the ItemAdded method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文