Friday, January 15, 2010

Context Bug in SharePoint Object Model for Web Services

There is a known issue in the SharePoint object model when getting the site context from a web service. It seems that some of the properties of HttpContext used by MOSS in static classes (i.e. ServerContext) are not initialised properly.

Server context will work fine from pages, web parts, console apps etc, but not under web services deployed to SharePoint.

Until this is fixed by MS, a work-around is to add in the following code after you get the server context but prior to doing anything with it.

' get context
Dim octx As ServerContext = ServerContext.GetContext(site)
' execute workaround for object model bug
If Not HttpContext.Current Is Nothing Then
If HttpContext.Current.Items("HttpHandlerSPWeb") Is Nothing Then
HttpContext.Current.Items("HttpHandlerSPWeb") = site.RootWeb
End If
If HttpContext.Current.Items("Microsoft.Office.ServerContext") Is Nothing Then
HttpContext.Current.Items("Microsoft.Office.ServerContext") = octx
End If
End If
' now use the context.....

Keep this in mind if you are doing any sharepoint web services.

No comments:

Post a Comment