Mishal
Mishal

Reputation: 41

SharePoint: SPContext.Current returns null for in SharePoint foundation 2010

I am using the below code to access the web-application from sharepoint.

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

it works fine with the sharepoint server 2010(MOSS2010). But when I work with SharePoint foundation 2010(WSS4.0) SPContext.Current always returns null. Can anyone tell me what could be the reason for this? and how can i resolve this issue?

Thanks and regards, Sharmila

Upvotes: 3

Views: 10193

Answers (2)

undefined
undefined

Reputation: 34299

One of the common reasons I've seen where SPContext is found to be null is when you have an incorrectly configured AAM. Its really important that sharepoint understands what URLs are configured for your site because if your URL is not registered it cant tell if the URL is inside the SPContext hence create it.

The most common change Ive seen is modifying the host headers of the IIS site. This is an enormous no-no, please don't ever do this (use AAM's instead). If you do go ahead and modify these any of your newly added headers will only be partly functional. Inside of SharePoint there is a lot of components which rely directly on the URL and in particular on looking this URL up internally. What you find (for the developers) is that references to SPContext.Current (a core piece of SharePoint goodness) will not work. This causes havoc on random SharePoint pages, web parts and custom features.

Take a look here if you want to know how to setup AAMs (and find out why they are so important) take a look at my blog post on the subject http://blog.staticvoid.co.nz/2011/05/sharepoint-alternate-access-mappings.html

Upvotes: 4

Howard
Howard

Reputation: 704

When you built your code, did you build it on an SP2010 Server machine, and then deploy the code to a Foundation machine? If you did, I suspect that you'll need to rebuild the code on a Foundation server, referencing the Foundation assemblies, not the Server assemblies.

Upvotes: 0

Related Questions