SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a Blog in my site and change its layout. I'm using a webpart which is giving 401 error on public facing site.

My Code is:

public void CustomBlogLayout()
    SPSite thisSite = SPContext.Current.Site;
    using (SPWeb targetWeb = thisSite.AllWebs["/en-us/blogs/"])
        SPList Forms = targetWeb.Lists["Posts"];
        SPQuery qry = new SPQuery();
        qry.Query = @"<Query>" +
                        "<OrderBy>" +
                            "<FieldRef Ascending='FALSE' Name='Modified' />" +
                            "</OrderBy>" +
        SPListItemCollection SPLIC = Forms.GetItems(qry);
        LiteralControl print = null;
        string tlink = string.Empty;
        foreach (SPListItem item in SPLIC)
            tlink = "/en-us/blogs/Lists/Posts/Post.aspx?&ID=" + item.ID;

            string[] authorInfo = item["Author"].ToString().Split(';');
            string authorId = authorInfo[0];
            string author = authorInfo[1].Replace("#", string.Empty);
            string alink = "/en-us/blogs/_layouts/userdisp.aspx?ID=" + Convert.ToInt32(authorId);

            string body = item["Body"].ToString();

            print = new LiteralControl("<div class='ms-PostTitle' ><a href=" + tlink + " >" + item["Title"].ToString() + "</a></div>" +
                                        "<div class='ms-PostFooter' > by " + "<a href=" + alink + " >" + author + "</a>\t" + "on " + item["Modified"].ToString() + "</div>" +
                                        "<div class='ms-PostBody' >" + item["Body"].ToString() + "</div>"

Thanks in Advance

share|improve this question

The thisSite.AllWebs is loading all webs so unless all of the allow anonymous access this will fail.

Replace it with

using (SPWeb targetWeb = thisSite.OpenWeb("/en-us/blogs/")) 


SPWeb targetWeb = SPContext.Current.Web
share|improve this answer
Hi Per, Thanks again for your support. I have test both but same "401 UNAUTHORIZED" message. Even i remove all code and deploye wepart with no code it shows same error. I don't whats the problem? From last 1 week i am working on this issue but no success. I'm in big trouble now. :'( – Feroze Apr 11 '12 at 12:17
I also noticed that even i delete webpart from default.aspx page it still shows "401 UNAUTHORIZED" message on public facing site. However, posts are accessible even i add web part or not. check this link this is showing 401 and posts page is working fine link – Feroze Apr 11 '12 at 12:27
I can't see the web part on the post page. – Per Jakobsen Apr 11 '12 at 12:34
Yes Webpart is not on posts page and it is accessible i have also removed webpart from default.aspx page but it still giving 401. :( – Feroze Apr 11 '12 at 12:37

SPContext is a user aware object that will give you security exceptions in a lot of cases when used on an public facing web site without any authentication.

Create new SPSite/SPWeb objects instead:

using( var site = new SPSite( yourUrlHere ))
    using (var web = site.OpenWeb( yourUrl ))
    { rest of your code here... }
share|improve this answer

use RunWithElevatedPrivileges delegation


                using (SPSite site = new SPSite(bla bla..)
                    //my code
share|improve this answer

Your Answer


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.