2
votes

I have a SharePoint 2010 event receiver that listens for add/update/delete events on a SharePoint list. When an event occurs the event receiver written in C# needs to pass these events to a Java program. My code is posted below. I can deploy the receiver fine and it works. I get all the SharePoint events I need. Now my problem is passing these events to Java. I get security exceptions every time I try to open a socket or write out SharePoint events to a file. For example, look at the ItemDeleting method I posted below that fires every time an item is deleted from a SharePoint list. When I try to open a socket to Java I get System.Net.DnsPermission exception. If I don't open a socket and try to write to a file instead, I get a System.Security.Permissions.FileIOPermission exception.

I looked around the forums and I tried a couple of things to troubleshoot it. First, I set trust level="Full" in web.xml of SharePoint. Then I set the DeploymentTarget="GlobalAssemblyCache" of my assembly. This doesn't help. What else can I do? Thank you in advance for your responses.

My event receiver code:

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>
1

1 Answers

0
votes

You need FullTrust and run code that accesses system resources (like files) under "SharePoint:System" account.

Starting point - SPSecurity.RunWithElevatedPrivileges.

See How to store temp files in SharePoint 2010 hosted code? .