我有一個SharePoint 2010事件接收器,用於偵聽SharePoint列表中的添加/更新/刪除事件。當發生事件時,用C#編寫的事件接收器需要將這些事件傳遞給Java程序。我的代碼發佈在下面。我可以將接收器部署得很好,它可以工作。我獲得了我需要的所有SharePoint事件。現在我的問題是將這些事件傳遞給Java。每次嘗試打開套接字或將SharePoint事件寫入文件時,都會收到安全性異常。例如,查看下面發佈的ItemDeleting方法,每次從SharePoint列表中刪除項目時都會觸發該方法。當我嘗試打開一個Java套接字時,我得到System.Net.DnsPermission異常。如果我沒有打開套接字並嘗試寫入文件,我得到一個System.Security.Permissions.FileIOPermission異常。Sharepoint事件接收器安全異常
我環顧了論壇,我嘗試了幾件事來解決它。首先,我在SharePoint的web.xml中設置信任級別=「完整」。然後我設置我的程序集的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_Feature1\Feature.xml" />
</FeatureManifests>
</Solution>
Spasibo Alexei。我將代碼更改爲您所建議的內容,並且不再有例外。但是,每當我嘗試添加/刪除文件時,我現在都會在GUI上看到一個錯誤。它說錯誤創建沙箱代碼對象的實例。該對象不能從沙盒代碼組件創建。這隻發生在我的代碼正在運行時。你知道什麼可能導致它? – james
不確定。我認爲你有接收器是沙盒,結果不能做很多事情。如果是這種情況,您可能需要以不同的方式部署它(場範圍功能?)。查看日誌以獲取更多有用的信息。 –