我有一个 SharePoint 2010 事件接收器,用于侦听 SharePoint 列表中的添加/更新/删除事件。 当事件发生时,用 C# 编写的事件接收器需要将这些事件传递给 Java 程序。 我的代码发布在下面。 我可以很好地部署接收器并且它可以工作。 我得到了我需要的所有 SharePoint 事件。 现在我的问题是将这些事件传递给Java。 每次尝试打开套接字或将 SharePoint 事件写出到文件时,我都会收到安全异常。 例如,查看我在下面发布的 ItemDelete 方法,该方法每次从 SharePoint 列表中删除项目时都会触发。 当我尝试打开Java套接字时,我得到System.Net.DnsPermission异常。 如果我不打开套接字并尝试写入文件,则会出现System.Security.Permissions.FileIOPermission异常。
我环顾了一下论坛,并尝试了几件事来解决问题。 首先,我在 SharePoint 的 web.xml 中设置信任级别="Full"。 然后,我设置程序集的 DeploymentTarget="GlobalAssemblyCache"。 这无济于事。 我还能做什么? 提前感谢您的回复。
我的事件接收器代码:
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Net.Sockets;
using System.IO;
namespace EventReceiverTest
{
public class EventReceiver1 : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
}
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
}
public override void ItemDeleting(SPItemEventProperties properties)
{
//can't open a socket connection.Request for the permission of type 'System.Net.DnsPermission,... failed
TcpClient tc = new TcpClient("192.168.xxx.xxx", xxx);
Console.WriteLine("Server invoked");
NetworkStream ns = tc.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.WriteLine("item deleted");
sw.Flush();
StreamReader sr = new StreamReader(ns);
Console.WriteLine(sr.ReadLine());
//can't write to file. Request for the permission of type 'System.Security.Permissions.FileIOPermission,...failed
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\logs\test.txt");
file.WriteLine("item deleted " + properties.ListItem);
//can't output anything to a console
Console.Out.WriteLine("deleting an item ");
base.ItemDeleting(properties);
//but this part works
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
<Properties>
<Property Key="GloballyAvailable" Value="true" />
</Properties>
</Feature>
<?xml version="1.0" encoding="utf-8"?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/">
<Assemblies>
<Assembly Location="EventReceiver3.dll" DeploymentTarget="GlobalAssemblyCache" />
</Assemblies>
<FeatureManifests>
<FeatureManifest Location="EventReceiver3_Feature1Feature.xml" />
</FeatureManifests>
</Solution>
您需要 FullTrust 并运行在"SharePoint:System"帐户下访问系统资源(如文件)的代码。
起点 - SPSecurity.RunWithElevatedPrivileges.
请参阅如何在 SharePoint 2010 托管代码中存储临时文件?。