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

感情迁移 提交于 2020-02-02 10:14:53

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/8725255/sharepoint-spcontext-current-returns-null-for-in-sharepoint-foundation-2010

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!