0

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>" +
                     "</Query>";
        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>"
                                       );
            this.Controls.Add(print);
        }
    } 
}

Thanks in Advance

3 Answers 3

1

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/")) 

or

SPWeb targetWeb = SPContext.Current.Web
4
  • 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, 2012 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, 2012 at 12:27
  • I can't see the web part on the post page. Apr 11, 2012 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, 2012 at 12:37
0

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... }
0

use RunWithElevatedPrivileges delegation

SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(bla bla..)
                {  
                    //my code
                }
            });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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