SharePoint:从 ContentType 取消注册 EventReceiver 时出错
我在取消注册某些内容类型的事件接收器时遇到一些问题。 内容类型和接收器是我自己部署和注册的,因此我不会尝试删除任何 MOSS 内置或内部事件接收器。
我尝试使用以下代码片段对此进行存档:
using (SPSite site = new SPSite("http://wssdev06/"))
{
using (SPWeb web = site.RootWeb)
{
// web.AllowUnsafeUpdates = true;
SPContentType type = web.AvailableContentTypes[<ContentTypeName>];
while (type.EventReceivers.Count > 0)
{
type.EventReceivers[0].Delete();
}
type.Update();
// web.AllowUnsafeUpdates = false;
}
}
不幸的是,命令“type.Update()”抛出一个异常,告诉我该集合无法修改。 正如您在代码中看到的,我已经尝试了不同的方法来解决此问题,例如允许不安全的更新或以提升的权限运行此代码。 但我总是遇到同样的例外。
那么我做错了什么?
I've got some problems unregistering some eventreceivers form a contenttype. The contenttype and the receivers were deployed and registered by myself so I don't try to remove any MOSS built-in or internal eventreceivers.
I trying to archive this with the following code snippet:
using (SPSite site = new SPSite("http://wssdev06/"))
{
using (SPWeb web = site.RootWeb)
{
// web.AllowUnsafeUpdates = true;
SPContentType type = web.AvailableContentTypes[<ContentTypeName>];
while (type.EventReceivers.Count > 0)
{
type.EventReceivers[0].Delete();
}
type.Update();
// web.AllowUnsafeUpdates = false;
}
}
Unfortunately the command "type.Update()" throws an exception telling me that the collection cannot be modified. As you can see in the code I've already tried different things to solve this problem, as allowing unsafe updates or running this code with elevated privileges. But I always get the same exception.
So what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是“AvailableContentTypes”属性返回您一个READ-ONLY集合。
您还应该使用“ContentTypes”属性,一切都应该没问题。
问候,
杜格。
Your problem is that the "AvailableContentTypes" property returns you a READ-ONLY collection.
You should also use the 'ContentTypes' property, ans everything should be fine.
Regards,
Dug.
该类型是否基于发布类型? 我在更改这些类型时遇到了问题。 否则,您可以尝试更改该类型的整个 SchemaXml。 我发现有时这种方法有效,而其他方法则无效。
Is the type based on a publishing type? I have had issues changing those types. Otherwise you might try changing the entire SchemaXml of the type. I have found that this sometimes works when other methods do not.
您应该能够设置
type.ReadOnly = false
以启用对该类型的写入。同样,您的 SPWeb 不需要废弃 - 请参阅 Roger Lamb 的博客此处了解更多详细信息。
You should be able to set
type.ReadOnly = false
to enable writing to the type.As well, your SPWeb does not need to be disposed - see Roger Lamb's blog here for more details.