Spcontext.current è nullo per ihttphandler
-
10-12-2019 - |
Domanda
Sto cercando di implementare Ihttphandler in SharePoint 2010 e per riferimento sto usando questo: http://blogs.msdn.com/b/kaevans/archive/2010/08/04/eporloy-an-asp-net-httphandler-to-SharePoint-2010.aspx
Ma io sto ottenendo spontext.current è "null" e non riesce a recuperare il sito di contesto.
L'URL è come:
http://spsvr/sitecoll/subsite/_layouts/myfolder/myhandler.ashx?param1=paramvalue
. Soluzione
sto rispondendo alle mie domande.
Dopo aver revisionato @simondoy commenta che "è strano" e si è reso conto che sto davvero facendo qualche errore e poi realizzando errore.
Ho commesso errori, stavo correndo con il permesso elevato e dentro di ciò mi riferisco a SpContExt.Current.
Il codice di errore
public void ProcessRequest(HttpContext context)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
// 'SPContext.Current' null reference error
using (var site = new SPSite(SPContext.Current.Site.ID))
{
using (var web = site.OpenWeb(SPContext.Current.Web.ID))
{
// codes goes here
}
}
});
}
.
Il codice fisso
public void ProcessRequest(HttpContext context)
{
var curSite = SPContext.Current.Site;
var curWeb = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (var site = new SPSite(curSite.ID)) {
using (var web = site.OpenWeb(curWeb.ID))
{
// code goes here
}
}
});
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange