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;

… 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).

About these ads
MissingMethodException when calling SPWeb.Recycle() in Sandbox solution

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s