Sharepoint SPSecurity.RunWithElevatedPrivileges对象引用未设置为对象的实例

时间:2015-03-31 07:26:48

标签: sharepoint nullreferenceexception

我在SharePoint中有一个安全的代码块,如下所示:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(rootSite))
    {
        _web = site.OpenWeb(subSite);
    }
});

大部分时间,它工作正常,但有时候,我收到了这个错误:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at NCS.MDA.FiMS.CreateReturnFileTask.BatchJobManager..ctor()

有没有人有任何想法? 感谢

1 个答案:

答案 0 :(得分:0)

猜测你的新SPSite(rootSite)会给你一个空值。

尝试使用检查null的n if语句包装方法

 SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(rootSite))
    {
       if(site != null)
     {
       _web = site.OpenWeb(subSite);

    }
            }
});

在if语句中添加else位,看看是否可以记录一些信息,如果通过调试无法找出site是否为null,则会给你一些反馈

干杯 Truez