Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have an auqward problem that's been nagging me on and off for half a years time. It turns out, when I'm finally taking a real shot at it that my query throws a NullReferenceException the second time I run the script. The first time I ask for 1 row in return, and the second time I ask for three rows in return and I get "Object reference not set to an instance of an object" on GetSiteData().

I have no idea why?

Source code:

    protected DataTable QueryMyNews(string PropertyValue)
    {
        using (SPSite newsSite = new SPSite(ProtocolHelper.SetContextProtocol(SPContext.Current.Site, SettingsManager.GetSetting(SettingsEnum.NewsUrl))))
        {
            using (SPWeb contextWeb = newsSite.OpenWeb())
            {
                string viewfields = "<FieldRef Name=\"Title\" />";
                viewfields += "<FieldRef Name=\"FileRef\" />";
                viewfields += "<FieldRef Name=\"Comments\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"PublishingPageImage\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"ToppNyhet\" Nullable='TRUE'  />";
                viewfields += "<FieldRef Name=\"_ModerationStatus\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"Organisation\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"Created\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"Audience\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"ArticleStartDate\" Nullable='TRUE'/>";
                viewfields += "<FieldRef Name=\"{61cbb965-1e04-4273-b658-eedaa662f48d}\" Nullable='TRUE' />";
                viewfields += "<FieldRef Name=\"TaskDueDate\" Nullable='TRUE' />";

                CrossListQueryInfo query = new CrossListQueryInfo();
                query.FilterByAudience = true;
                query.ShowUntargetedItems = false;
                query.RowLimit = 3;
                query.WebUrl = SPContext.Current.Site.ServerRelativeUrl;
                query.Lists = "<Lists ServerTemplate=\"850\" />";
                query.Webs = "<Webs Scope=\"SiteCollection\" />";
                query.UseCache = false;
                query.ViewFields = viewfields;

                query.Query +=  "<Where>"+
                                    "<Or>"+
                                         "<Geq>"+
                                            "<FieldRef Name='TaskDueDate' />"+
                                            "<Value IncludeTimeValue='TRUE' Type='DateTime'><Today /></Value>"+
                                         "</Geq>"+
                                         "<IsNull>"+
                                            "<FieldRef Name='TaskDueDate' />"+
                                         "</IsNull>"+
                                    "</Or>"+
                               "</Where>";
                query.Query += "<OrderBy><FieldRef Name=\"Created\" Nullable=\"True\" Type=\"DateTime\" Ascending=\"FALSE\"/></OrderBy>";

                CrossListQueryCache myCache = new CrossListQueryCache(query);

                DataTable audienceAndForvaltning = myCache.GetSiteData(contextWeb); // Object refence not set to an instance of an object
share|improve this question
1  
Anyone ever figure this out? Having the same problem. SMW –  user15006 Mar 18 '13 at 11:29
1  
I stuck in same problem... I used same code in a different environment and it works there... anyone got a solution? –  user19265 Sep 2 '13 at 18:14

1 Answer 1

up vote 3 down vote accepted

I had same problem. After the deep investigation I found that inside the GetSiteData(contextWeb) method in same places used SPContext.Current.Web not contextWeb.

In my case case when SPContext.Current.Web is null I get an NullReferenceException

To fix it I wrote help class

public class ContextSwitcher : IDisposable
{
    private HttpContext _temp;
    private SPSite _site;
    private SPWeb _web;

    public ContextSwitcher(string url)
    {
        _site = new SPSite(url);
        _web = _site.OpenWeb();
        _temp = HttpContext.Current;

        HttpRequest request = new HttpRequest("", _web.Url, "");
        request.Browser = new HttpBrowserCapabilities();
        HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
        HttpContext.Current.Items["HttpHandlerSPWeb"] = _web;
    }

    public void Dispose()
    {
        HttpContext.Current = _temp;
        if (_web != null) _web.Dispose();
        if (_site != null) _site.Dispose();
    }
}

and rewrote my code using this class

using (ContextSwitcher contextSwitcher = new ContextSwitcher(webUrl))
        {
            CrossListQueryInfo query = new CrossListQueryInfo();
            query.Lists = "<Lists ServerTemplate=\"100\" />";
            query.Webs = "<Webs Scope=\"SiteCollection\" />";
            query.ViewFields = "<FieldRef Name=\"Title\" />";
            query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>TestTitle</Value></Eq></Where>";

            CrossListQueryCache cache = new CrossListQueryCache(query);

            DataTable resultTable = cache.GetSiteData(SPContext.Current.Web);
        }

This solved my problem.

share|improve this answer
    
Thanks for solning this old mystery! Great post! –  Benny Skogberg MCSA Nov 20 '13 at 18:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.