We have a webpart on our SiteCollection home page which aggregates the most recent posts from a number of blog subsites. The code looks like this:
using (SPSite site = new SPSite(GetBlogSiteCollectionUrl()))
{
foreach (SPWeb currentWeb in site.OpenWeb().GetSubwebsForCurrentUser())
{
if (!currentWeb.IsRootWeb)
{
try
{
(from p in currentWeb.Lists["Posts"].Items.OfType<SPListItem>()
where (string)p["Approval Status"] == "0"
select new BlogPost
{
Title = p["Title"] == null ? string.Empty : p["Title"].ToString(),
Url = p["FileDirRef"].ToString() + "/Post.aspx?ID=" + p["ID"].ToString(),
Author = BlogPostHelper.GenerateAuthor(p["Author"].ToString()),
Date = Convert.ToDateTime(p["PublishedDate"].ToString()),
FullText = p["Body"] == null ? string.Empty : p["Body"].ToString(),
NewsFeed = new NewsDataFetcherImplementation() { ID = currentWeb.ID, Name = currentWeb.Title, Url = p["FileDirRef"].ToString() + "/Post.aspx?ID=" + p["ID"].ToString() },
Snippet = BlogPostHelper.GenerateSnippet(p["Body"] == null ? string.Empty : p["Body"].ToString(), _snippetLength),
}).ToList().ForEach(post => allPosts.Add(post));
}
catch (Exception ex)
{
Logger.LogException(ex, LogCategory.BlogFetcher,"Error fetching posts from {0}".FormatIt(currentWeb.Name));
}
}
}
}
return allPosts.OrderByDescending(m => m.Date).Take(numItems).ToList();
BlogPost is a entity class custom to our project.
The problem we are getting is that for some users, the call to p["Author"] throws this error:
System.ArgumentException: Value does not fall within the expected range
But only on one of the blog sub sites, and only for a few users. (haven't tried enough users to establish a pattern, other than it only occurs for non-admin users)
While debugging the issue, I put in the following watches:
p {Microsoft.SharePoint.SPListItem} Microsoft.SharePoint.SPListItem
p["ID"] 11 object {int}
p["PublishedDate"] {2/10/2012 6:53:00 PM} object {System.DateTime}
p["Approval Status"] "0" object {string}
p["Author"] 'p["Author"]' threw an exception of type 'System.ArgumentException' object {System.ArgumentException}
p["Created By"] 'p["Created By"]' threw an exception of type 'System.ArgumentException' object {System.ArgumentException}
p[SPBuiltInFieldId.Author] 'p[SPBuiltInFieldId.Author]' threw an exception of type 'System.ArgumentException' object {System.ArgumentException}
p.Properties["Author"] null object
p.Properties["Created By"] null object
currentWeb
? SPSite.OpenWeb