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? Continue reading “MissingMethodException when calling SPWeb.Recycle() in Sandbox solution”

Advertisement
MissingMethodException when calling SPWeb.Recycle() in Sandbox solution

Bloody Stupid SPWeb properties

So, like many SharePoint objects, the SPWeb object has a property bag. Unlike many, it has two – Properties and AllProperties.

The problem I encountered with this was that I was trying to set some Search Center settings on a newly created Site Collection, and their capitalization was being persisted wrongly:

newRootWeb.AllProperties["SRCH_ENH_FTR_URL"] = settings.SearchCenterUrl;

This would be persisted as lower case – which was wrong. Lower case capitalisation isn’t the same upper. Nice. Continue reading “Bloody Stupid SPWeb properties”

Bloody Stupid SPWeb properties

Getting levels of the SharePoint Heirarchy and their Exceptions

Something that I have to do time and again is get some element of SharePoint’s heirarchy, such as a site collection, site, list or item. This is pretty typical – that’s why we all use USING to ensure proper disposal of SPSites and SPWebs, right? But what happens if the thing you’re after isn’t there? What exception get’s thrown? Well, this should be pretty clear:

try
{
    //FileNotFoundException if doesn't exist
    using (SPSite site = new SPSite(siteGuid))
    {
        //FileNotFoundException if doesn't exist
        using (SPWeb web = site.OpenWeb(webGuid))
        {
            //SPException if doesn't exist
            SPList list = web.Lists[listGuid];

            //ArgumentException if doesn't exist
            SPListItem item = list.GetItemByUniqueId(itemGuid);
        }
    }
}
catch (System.IO.FileNotFoundException fileEx2)
{
    // Site or Site Collection Not Found
}
catch (SPException spEx2)
{
    // List not found
}
catch (ArgumentException argEx2)
{
    // Item not found
}

Hopefully that might prove useful to someone – and a good reminder for me.

Getting levels of the SharePoint Heirarchy and their Exceptions

How to get an SPWeb object from a URL

One of the problems with SharePoint is that it’s very difficult to figure out what site is specified by a URL. After all, the URL to a particular page contains:

  • the Server
  • possibly (but not necessarily) a managed path and site collection
  • possibly (but not necessarily) a site
  • possibly (but not necessarily) a folder (such as ‘/lists/’)
  • possibly (but not necessarily) a list/library name
  • possibly (but not necessarily) a folder in a Library
  • the item itself.

Suffice to say, with all those optional bits, decomposing a URL to find the site is really hard. There is, however, a slightly obscure way of find this. You can create a site collection (SPSite) with a full URL, and then simply call OpenWeb() without any parameters to return you the site (SPWeb):

string path = "http://example/examplesite/_layouts/settings.aspx";
try
{
using (SPSite siteCollection = new SPSite(path))
{
using (SPWeb site = siteCollection.OpenWeb())
//Do something with the site
}
}
}

I found this when looking at the MSDN docs for SPSite.OpenWeb(). Check out the examples in there.

It’s a little weird that the SPSite object remembers information about how it was opened like that. But it is useful to know.

How to get an SPWeb object from a URL

How to find out what type of site a site is…

I’ve built a feature to active branding on a site including master page, navigation and themes, which I’ve been talking about a bit over the last few weeks. One issue, though is that meeting workspaces have a different master page to ‘normal’ master pages, so I need to set them to use a different ‘custom’ master page when the feature is activated. This means that my feature receiver has to ‘know’ if the site it’s being activated on is a Meeting Workspace, or some other site.

What defines the ‘type’ of a site is the template that was used in it’s creation. Continue reading “How to find out what type of site a site is…”

How to find out what type of site a site is…

Property bags are useful – but there isn't one on SPLists?

Edit: As Steven Van de Craen points out, you can use the Properties collection on the Root folder for the List. Neat, didn’t think of that.

I like property bags in things. Sure, they’re open to abuse, but they give an easy way of storing a little extra data, and as a developer, I find that very useful.

For those of you who are wondering what a property bag is, well, it’s a collection on a object where a programmer can just store stuff. E.g.

SPWeb site = properties.Feature.Parent as SPWeb;
site.Properties["Kumquat"] = "True";

Now, clearly the SPWeb object in SharePoint’s API doesn’t have a property called Kumquat; we’re defining a new one, and storing a string. In fact, string objects is all you can store.

Anyway, SPListItems and SPWeb both have property bags, but SPList and SPSite do not. Which is a little annoying, as I want to store some data at the list level, dammit. And I hadn’t realised this ommission until now…

Thoughts on Steven’s point – it’s a good one, and using the root folder would work. Naturally, this means that you’re storing the properties in the SPFolder object, but each list should have one of those, I think. It’s a bit of a pain as, if you’re using the Web Services then I’m sure that getting that root folder will take another call – but as I’ve not validated that the properties are available via the web services, then that might be a moot point anyway.

Property bags are useful – but there isn't one on SPLists?