Wednesday 11 December 2013

SharePoint 2010 Sandbox solution 3 – Sandbox Proxy


As now we are aware of architecture and execution model of Sandboxed solutions, this blog should be dedicated to a sample sandbox solution. Do you think it is required? I don’t think so because it’s a very straight forward task which doesn’t motivates me to dedicate a full blog J. Instead we’ll take another topic – Sandbox Proxy or Full Trust Proxy.

If you want to execute full trust code in a sandbox solution then the only way of doing so is to write a Sandbox Proxy.



A sandbox proxy must be registered to User Code Host Service so that it can be called from a sandbox solution. Below are the steps to create and register a proxy

Step 1: Create SP Project


1.      Create an Empty  SharePoint Project

2.      Update the AssemblyInfo class and add following attribute to the class

 

using Microsoft.SharePoint.UserCode;

 

Step 2: Create Argument Class


1.      Add a new class “TestProxyArgs” with public access modifier

2.      Add following

using Microsoft.SharePoint.UserCode

 

3.      Make the class serializable and inherit from SPProxyOperationArgs class as following

[serializable]
public class TestProxyArgs : SPProxyOperationArgs { }

 

4.      Add properties that you want to pass from the proxy. Only serializable types can be used as properties here

public string TestArg { get; set; }
 

5.      Add read-only properties for the type name and assembly name of the proxy operations class (which we will create in the next step). This is required in registering the proxy operation and when invoking the operation from sandboxed code. By adding these properties to the proxy arguments class, we ensure that they are available from all locations when required.

6.      Add read-only properties for the type name and assembly name of the proxy operations class. The type name should be fully qualified and the assembly name should be the four-part strong name of the assembly

public static string ProxyOperationTypeName
{
  get
  {
    return "TestProxy.TestProxyOps";
  }
}
 
public static string ProxyAssemblyName
{
  get
  {
    return "TestProxy, Version=1.0.0.0, Culture=neutral,   
            PublicKeyToken=2dfe43bced7458f6";
  }
}

Step 3: Create Proxy Operations Class


1.      Add a new class named TestProxyOps to the project with a Execute method as following
public class TestProxyOps : SPProxyOperation {
public override object Execute (SPProxyOperationArgs args)
{
}
        }
 

2.      In the Execute method, cast the SPProxyOperationsArgs parameter to your proxy operations argument type, which in this case is TestProxyArgs.

   var proxyArgs = args as TestProxyArgs;

 

3.      Retrieve the arguments from the proxy arguments class and perform any full-trust logic

// Retrieve arguments from the proxy arguments class.
string testArg = proxyArgs.TestArg;
 
// Perform full-trust logic; for example, call a WCF service.
FullTrustMethod(testArg);
 
 

 

Step 4: Register Proxy Operation


  1. Add a new feature named TestProxyFeature to the project
  2. Make the scope of the feature to Farm
  3. Add an event receiver to the TestProxyFeature
  4. Add the following using statements to the TestProxyFeature.EventReceiver class

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint.UserCode;

 

  1. Uncomment the FeatureActivated method.
  2. In the FeatureActivated method, add the following code to retrieve the local user code service

SPUserCodeService userCodeService = SPUserCodeService.Local;

 

  1. Add the following code to create a new proxy operation type, based on your proxy operation class

var TestOperation = new SPProxyOperationType(

                            TestProxyArgs.ProxyAssemblyName,

                            TestProxyArgs.ProxyOperationTypeName);

 

  1. Add the following code to register your proxy operation type with the local user code service.

userCodeService.ProxyOperationTypes.Add(TestOperation);

userCodeService.Update();

 

  1. Deploy your sandbox proxy to the test environment.

Step 5: Use the proxy from Sandbox solution


  1. In the sandboxed solution, add a reference to the sandbox proxy assembly
  2. Create an instance o­f the proxy arguments class and set any property values

var proxyArgs = new TestProxyArgs();

proxyArgs. TestArg = "Test Arg";

 

  1. Call the SPUtility.ExecuteRegisteredProxyOperation method, passing in the assembly name of the proxy operation, the type name of the proxy operations class, and the proxy arguments instance. In this case, the assembly name and the type name are provided by static properties of the proxy arguments class, as described in step 2.

var result = SPUtility.ExecuteRegisteredProxyOperation(

 TestProxyArgs.ProxyAssemblyName,

 TestProxyArgs.ProxyOperationTypeName,

 proxyArgs);

 

That’s it. Hope this would help you guys J
Please note that I posted this blog orignally on http://aspe-it.com/blog/2013/sharepoint-2010-sandbox-solution-3-sandbox-proxy/

No comments:

Post a Comment