0

我创建了一些网站栏/内容类型和列表定义作为功能的一部分。我想将 eventreceiver 附加到内容类型。我添加了将事件接收器附加到内容类型的代码。使用 spmanager 我可以看到事件接收器已附加到内容类型,但是当我从内容类型创建列表时,事件接收器丢失了。有任何想法吗。我的代码如下

  public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                string asmName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
                string itemReceiverName = "xxxxxx.Intranet.SP.xxxxx.PermissionsUpdaterEventReceiver";

                ////surely a better way to get all lists than this
                ////re - do
                using (SPSite thisSite = (SPSite)properties.Feature.Parent) {
                    using (SPWeb web = thisSite.RootWeb) {
                        SPContentType RambollNewsContentType = web.ContentTypes["RambollNewsContentType"];
                        RambollNewsContentType.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, itemReceiverName);
                        RambollNewsContentType.Update(true);
                    }
                }    
            }
4

2 回答 2

0

我不确定这是否与您的问题有关,但是

但是您能否在将内容类型添加到列表之前尝试将事件接收器添加到内容类型。

我认为必须先添加事件接收器,因为在将内容类型添加到列表时,内容类型不会直接添加到列表中,而是将其副本添加到列表中。因此,当您将内容类型添加到列表时,还没有事件接收器。

如果我理解你的问题是错误的,请纠正我?

谢谢

于 2013-03-13T11:41:35.737 回答
0

我正在成功使用它。我留下了我的日志记录和日志记录方法。

    /// <summary>
    /// This method is executed on feature activation.
    /// It attaches the event receiver to the content type.
    /// </summary>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        LogMessage("FeatureActivated - Start");
        SPSite site = (SPSite)properties.Feature.Parent;
        LogMessage("FeatureActivated - 1");
        SPWeb web = site.RootWeb;
        LogMessage("FeatureActivated - 2");
        //ListContentTypes(web);
        SPContentType ctype = web.ContentTypes["Wells Project Task List"];
        LogMessage("FeatureActivated - 3");
        LogMessage("ctype name: " + ctype.Name.ToString());
        if (ctype != null)
        {
            LogMessage("FeatureActivated - I have a content type. Web url: " + web.Url);
            SPEventReceiverDefinition er = ctype.EventReceivers.Add();
            er.Class = "Wells.SharePoint.ProjectManagementEventReceiver";
            er.Assembly = "ProjectManagementEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1cb21c41d80b7ae";
            er.Type = SPEventReceiverType.ItemAdded;
            er.Name = "ItemAdded";
            er.SequenceNumber = 10001;
            er.Update();
            ctype.Update(false);
            LogMessage("FeatureActivated - After ctype.update");
            web.Dispose();
        }
        else
            LogMessage("CT not found: " + web.Url);




        LogMessage("FeatureActivated - End");
    }

    static void ListContentTypes(SPWeb web)
    {
        foreach (SPContentType ct in web.ContentTypes)
        {
            LogMessage("CT: " + ct.Name.ToString());
        }
    }


    /// <summary>
    /// This method is executed on feature deactivation.
    /// It removes the event receiver from the content type
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        LogMessage("Feature DeActivated  - Start");
        SPSite site = (SPSite)properties.Feature.Parent;
        SPWeb web = site.OpenWeb();
        SPContentType contentType = web.ContentTypes["NAME OF CONTENT TYPE"];
        if (contentType != null)
        {
            LogMessage("im have a content type. Web url: " + web.Url);
            int i;
            //Use the above integer to loop through the event recievers on the first list and delete the above assembly
            for (i = 0; i < contentType.EventReceivers.Count; i++)
            {
                LogMessage("ER Count: " + contentType.EventReceivers.Count);
                if (contentType.EventReceivers[i].Assembly.Contains("ProjectManagementEventReceiver"))
                {
                    contentType.EventReceivers[i].Delete();

                    LogMessage("Deleting event receiver from CT");


                }
            }
            contentType.Update();
        }
        else
            LogMessage("CT not found: " + web.Url);

        LogMessage("Feature DeActivated  - End");

    }

static void LogMessage(string msg)
    {
        StreamWriter wrtr = null;
        try
        {
            wrtr = new StreamWriter("C:\\Logs\\FeatureActivatedDeactivated.txt", true);
            wrtr.WriteLine(msg + "--[" + System.DateTime.Now.ToString() + "]" + Environment.NewLine);
            wrtr.WriteLine(Environment.NewLine + "==================================");
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (wrtr != null)
            {
                wrtr.Close();
                wrtr.Dispose();
            }
        }
    }
于 2014-01-17T16:18:05.513 回答