“Cannot complete this action. Please try again.” when trying to access the taxonomy data

In my article on accessing SharePoint data from a WCF service, I have described necessary configuration of the service, which is needed to impersonate right user, and access SharePoint sites, lists and list items under impersonated user’s credentials.

It was all working as a charm, until I have tried to access the taxonomy data (to create new terms) from that very same service – and got a really strange Exception:

Der Vorgang kann nicht erfolgreich beendet werden. Bitte versuchen Sie es erneut.

impersonation-scenario

OK, just for the record: I have an English SharePoint installation, with German language pack installed, and my default language is set to English. So what the heck is an exception in German doing here? Especially when German is not the only LIP I have installed (Bosnian and French are also present). I still didn’t figure out that one… But, anyway, I speak German, so I knew that the exception is actually:

Cannot complete this action. Please try again.

There it is. My favorite SharePoint exception. Long time no see, I kind of missed you.

Since this is a generic SharePoint exception which could mean anything and nothing, more info has came from the ULS:

ERROR: Failed to OpenThreadToken, LastError=1008

OK, there we are, this is more like it. Impersonation. Or better, lack of it. Even if you manage to get the SPContext under the right credentials, it doesn’t mean that you have full ASP.NET impersonation in place.

Quick look at the web.config of my service, Asp.Net Impersonation has been turned on…? Crap, no, it hasn’t. That was the problem.

Go to IIS and enable the ASP.NET Impersonation on your Web Service:

05

In the service’s web.config, you should see following in the <system.web> node:

<system.web>
  ...
  <identity impersonate="true" />
</system.web>

IISRESET, another try, and everything works, my new Term is stored properly:

TERM

So, when you try to access SharePoint 2010 Taxonomy data from a web service, and you get a message “Cannot complete this action. Please try again” (in German!), it is a clear sign that you have forgot to set ASP.NET impersonation, right?

That’s why we love SharePoint.

PS. It’s not only taxonomy, this error could haunt you in other scenarios, too, like creating programmatically site collections from web service, etc..