How can this throw a NullReferenceException?
I have the following code:
EventLog el1 = new EventLog();
el1.Log = "Application";
el1.Source = "SharePoint Foundation";
el1.WriteEntry("Start", EventLogEntryType.Information);
el1.WriteEntry("SPContext : " + (SPContext.Current == null ? "nothing" : "something"), EventLogEntryType.Information);
el1.WriteEntry("Web ID: " + (SPContext.Current.Web == null ? "nothing" : "something"), EventLogEntryType.Information);
The first two WriteEntry
log just fine, and the second one logs "something". But the third one will always error out. Is my brain just fried, or should this never happen?
PLEASE NOTE:
The second WriteEntry
writes "something" to the logs. This would mean that SPContext.Current
is not null.
Update:
I don't know if it makes a difference but I am running this with elevated privileges using SPSecurity.RunWithElevatedPrivileges. Here is my code:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
EventLog el1 = new EventLog();
el1.Log = "Application";
el1.Source = "SharePoint Foundation";
el1.WriteEntry("Start", EventLogEntryType.Information);
el1.WriteEntry("SPContext : " + (SPContext.Current == null ? "nothing" : "something"), EventLogEntryType.Information);
el1.Wri开发者_StackOverflowteEntry("Web ID: " + (SPContext.Current.Web == null ? "nothing" : "something"), EventLogEntryType.Information);
}
catch (Exception ex)
{
EventLog el1 = new EventLog();
el1.Log = "Application";
el1.Source = "SharePoint Foundation";
el1.WriteEntry(ex.Message + System.Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
throw ex;
}
});
And my stack trace:
Object reference not set to an instance of an object.
at Copy_Special.CrossSiteCopy.<>c__DisplayClass1.<Execute>b__0()
FYI this code is part of a SharePoint Custom Workflow Action...
Update: Completely rewriting my answer according to new clarifications provided in the question and in the comments bellow this answer.
The last line should, at least, read as follows:
SPContext.Current == null || SPContext.Current.Web == null ? "nothing" : "something"
SPContext.Web
property includes a fairly complex logic that under certain circumstances creates a newSPWeb
instance. Hence, most likely, it may fail with theNullReferenceException
although that behavior is not documented. In many cases it yields anInvalidOperationException
as well.Under elevated privileges there's no valid
SPContext.Current
. You have to open the web site again to retrieve aSPWeb
instance that would work under the different security context.
Sample code for correct privilege elevation:
// site and web objects working with the current user's privileges
SPSite userSite = SPContext.Current.Site;
SPWeb userWeb = SPContext.Current.Web;
// elevate privileges
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// get new site and web objects working with elevated privileges
using (SPSite elevatedSite = new SPSite(userSite.ID))
{
using (SPWeb elevatedWeb = ElevatedsiteColl.OpenWeb(userWeb.ID))
{
// …code using elevatedSite and elevatedWeb…
}
}
});
Try this:
EventLog el1 = new EventLog();
el1.Log = "Application";
el1.Source = "SharePoint Foundation";
el1.WriteEntry("Start", EventLogEntryType.Information);
var c = SPContext.Current;
el1.WriteEntry("SPContext : " + (c == null ? "nothing" : "something"), EventLogEntryType.Information);
if (c != null) el1.WriteEntry("Web ID: " + (c.Web == null ? "nothing" : "something"), EventLogEntryType.Information);
精彩评论