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