Tuesday, June 16, 2015

SharePoint 2010 Sandbox Solution 3 – Sandbox Proxy



Now we are aware of the architecture and execution model of Sandboxed solutions. This blog is 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 a SharePoint Project

  1. Create a new Empty SharePoint Project
  2. Update the AssemblyInfo class file and add the 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 the following using statement:

    Microsoft.SharePoint.UserCode
  3. Make the class serializable and inherit from SPProxyOperationArgs class, using the  following code:

    [serializable]

    public class TestProxyArgs : SPProxyOperationArgs { }
  4. Add properties that you want to pass from the sandbox to 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.

    public class TestProxyOps : SPProxyOperation {

    Override the Execute method:

    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 classusing Microsoft.SharePoint.Administration;
    using Microsoct.SharePoint.UserCode;
  5. Uncomment the FeatureActivated method.
  6. In the FeatureActivated method, add the following code to retrieve the local user code serviceSPUserCodeService userCodeSwervice = SPUserCodeService.Local;
  7. Add the following code to create a new proxy operation type, based on your proxy operation classvar TestOperation = new SPProxyOperationType(
    TestProxyArgs.ProxyAssemblyName,
    TestProxyArgs.ProxyOperationTypeName);
  8. Add the following code to register your proxy operation type with the local user code service:userCodeService.ProxyOperationTypes.Add(TestOperation);
    uswerCodeService.Update();
  9. 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 of the proxy arguments class and set any property valuesvar proxyArgs = new TestProxyArgs();
    proxyArgs. TestArg = “Test Arg”;
  3. Call the SPUtility.ExecuteRegisteredProxyOperation method, passing in the assembly name of the proxy operation, the type name of the proxys 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 descried in step 2.var result = SPUtility.ExecuteRegisteredProxyOperation(
    TestProxyArgs.ProxyAssemblyName,
    TestProxyArgs.ProxyOperationTypeName,
    proxyArgs);

No comments:

Post a Comment