UserProfileManager.UserExists and “Operation is not valid due to the current state of the object”

I have clean install of MOSS 2007 and I have one .aspx page that makes some request to SSP. All the code in this page is running in elevated permissions and I decide programmatically what kind of content this page shows to visitor. When I check if user profile exists I get the following error message: Operation is not valid due to the current state of the object.

I found excellent hack written by Kristian Kjær. As it turns out this problem raises when some items are missing from current HTTP context. This time we are saved because we can easily set those items to values expected by SharePoint.


HttpContext context = HttpContext.Current;

if (HttpContext.Current != null)

{

    if (context.Items["HttpHandlerSPWeb"] == null)

        context.Items["HttpHandlerSPWeb"] = site.RootWeb;

    if (context.Items["Microsoft.Office.ServerContext"] == null)

        context.Items["Microsoft.Office.ServerContext"] = serverContext;

}


I hope it helps somebody.

No Comments