Use external assembly and webservices in sandboxed solutions; create full trust proxy

by Bas Hammendorp 10. July 2010 03:33

Imagine a situation where you want to use the advantages of a sandboxed solution, but also need some extra functions from (for example) a generic library. To accomplish this, you need to create a full trust proxy.

A sandbox solution runs in its own isolated process, the SPUCWorkerProcess.exe (this proces needs to be attached when you need to debug). This process has limited options regarding c# code, access to file system etc. To extend these option, you can write a full trus proxy. There are a few steps to take:

1. Create a new empty sharepoint 2010 (farm) project
In this project you need to create two classes, one class inherits from SPOperation and the other from SPOperationArgs

The first class contains the execute method in where you can execute you code

public class MyProxyOperation : SPProxyOperation
{
   public override object Execute(SPProxyOperationArgs args)
   {
      if (args != null)
      {
         // use the arguments
         MyProxyOperationArgs arguments = args as MyProxyOperationArgs;
        
         try
         {
            // execute your code
         }
         catch (Exception ex)
         {
            return ex.Message;
         }
       }
      return String.Empty;
   }
}

The method Execute is the only method you can use in the class, this should contain the code which you want to execute out of the sandbox. This code runs in the 'normal' w3p process.

The second method contains the arguments which you want to pass to the proxyoperation class:

[Serializable]
public class MyProxyOperationArgs : SPProxyOperationArgs
{
   public string argument1 { get; set; }
   public int argument2 { get; set; }
}

The properties in this class must be serializable.

2. Add the attribute [assembly: AllowPartiallyTrustedCallers()] to the AssemblyInfo.cs of this project so the assembly accepts the calls from the sandbox solution.

3. Create a new empty sharepoint 2010 (sanboxed) project
This project contains the call to the proxyoperation execute method in the previous project.

MyProxyOperationArgs myProxyOperationArgs = new MyProxyOperationArgs();
myProxyOperationArgs.argument1 = "Hello world";
myProxyOperationArgs.argument2 = 101;
                        e
string result = SPUtility.ExecuteRegisteredProxyOperation(
                <full assemblyname>,
                <typename>, myProxyOperationArgs).ToString();

Here you can see that you need to create the proxyoperation arguments, fill the properties and give this to the execute method. To call the execute method, you need to use the SPUtility.ExecuteRegisteredProxyOperation method which contains the full assembly name, the typename (with namespace) and you arguments. This will return an object which you (for example) can cast to a string.

So this should be the way to use additional code in sandboxed solution, but be carefull what to use and what not. After all, the sandbox has some important advantages in security and resources which you don't want to knock down.

One last important tip: when you deploy a new assembly with you proxy operations, you need to restart the SPUsercode service. You can do this with the command line operations 'net stop SPUsercodeV4' and 'net start SPUsercodeV4'

Good luck!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

C# | SharePoint 2010 | Sandbox solution

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

My name is Bas Hammendorp, I work as an Enterprise Engineer at PGGM. My specialties are ASP.NET, C#, SQL and Sharepoint 2007.

Google ads