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

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
share|improve this question
1  
Just a comment to your code: Are you sure you are disposing each currentWeb ? SPSite.OpenWeb –  eirikb Sep 13 '12 at 17:11
add comment

1 Answer 1

You're calling a method on a null value:

p["Author"].ToString() <-- Error is here
p.Properties["Author"] <-- null

You should perform a check against this value to make sure it's not null.

Aside from that though, I couldn't tell you why there is no author associated with that post without more information, but that's something to look into. Do you know who the author of post ID=11 is?

share|improve this answer
add comment

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.