MissingMethodException when calling SPWeb.Recycle() in Sandbox solution

Service Pack 1 for SharePoint 2010 introduced a new method for the SPWeb object - Recycle(). This allows you to send a site to the recycle bin, rather than deleting it outright. I’ve used it before in Farm solutions without any issue.

However, I’m now working on a sandbox solution, and when I tried using it, I got the error:

MissingMethodException Method not found: ‘Void Microsoft.SharePoint.SPWeb_SubsetProxy.Recycle()’.
at Microsoft.SharePoint.SPWeb.Recycle__Inner()
at Microsoft.SharePoint.SPWeb.Recycle()

Hmm. That’s strange; my call to recycle seems fine, and the documentation says it’s available in the Sandbox, but internally there seems to be a missing method.

Out of curiousity, I tried using Delete() rather than Recycle(), and this worked correctly. So what gives?

Next I opened Reflector, and opened the Microsoft.SharePoint assembly from 14/Usercode. Note that this is not the same as the Microsoft.SharePoint assembly from 14/ISAPI. The Usercode one is the assembly used in the Usercode service, and it is a subset of the full object model. It also works differently internally.

Anyway, I opened the SPWeb.Recycle__Inner() method:

Okay, only two lines. Long story short, I checked the SubsetOMEnabler.VerifySubsetOMEnabled() line, and it was doing something about controlling what OM was available. I couldn’t see anything that would result in the stacktrace I’d got, though.

However, the other line could. It casts an object to an SPWeb_SubsetProxy instance, and then calls Recycle() on it. This was the method that seemed to be missing, so I checked the SPWeb_SubsetProxy class to see if it had such a method.

Hmm. There’s a notable lack of a Recycle() method, just like the exception said.

So how does this compare with the SPWeb.Delete() method? Well, it uses the same pattern – a call to an inner method which casts the same object to an SPWeb_SubsetProxy class, and then it calls Delete() on the subset proxy.

The difference is, though, that the SPWeb_SubsetProxy class actually has a Delete() method.

Conclusions? Well, I’m not sure, but I think…

  • The error message was right – the method appears to be missing.
  • Compilation of the usercode version of the Microsoft.SharePoint assembly probably didn’t pick it up due to the ‘cast and call a method on the resulting object’ line. If it’d been:
SPWeb_SubsetProxy px = (SPWeb_SubsetProxy)base.__InnerObj;
px.Recycle();

… then I suspect the compiler would’ve complained.

  • As this Object Model stands, it is not possible to recycle sites in Sandbox solutions. Which is sad, ‘cos that’s nice functionality.

Has anyone else seen this problem? I was trying this on a machine that was patched up to the February 2012 Cumulative Updates.

(I’m a bit hesitant as often ‘faults’ in APIs are down to developer understanding rather than actual faults, in my experience).

One thought on “MissingMethodException when calling SPWeb.Recycle() in Sandbox solution

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>