0
votes

I have code that's pulling some images from a Sharepoint 2010 document library. When I'm logged in and testing everything works just fine but when it's published it forces a log in screen.

Here's the kicker: if I reference the images through normal HTML, the published page doesn't require a login. It's only when I try to pull the images in and build a list of them into an asp:literal control.

The permissions on the list allow anonymous read, as far as I can tell. Something is fishy somewhere. Please help!

Below is the code I'm using:

public void GetBanners() {

string strCode = "";

using (SPSite oSite = SPContext.Current.Site) {

    SPList oList = oSite.AllWebs[0].Lists["MainPage Banner Library"];
    SPView lv = oList.Views["All Pictures"];
    SPListItemCollection lic = oList.GetItems(lv);

    foreach (SPListItem li in lic) {
        if(li["Enabled"] is bool && (bool)li["Enabled"] == true) {
            if(li["URL"] is string && li["URL"].ToString() != "") {
                strCode = strCode + "<a href=\"" + GetURL(li["URL"].ToString()) + "\" target=\"_blank\"><img src=\"/MainPage Banner Library/" + li["Name"].ToString() + "\" alt=\" \" /></a>";
            } else {
                strCode = strCode + "<img src=\"/MainPage Banner Library/" + li["Name"].ToString() + "\" alt=\" \" />";
            }

        }
    }   
}
ltBanners.Text = strCode;
}

UPDATED: Aquila's answer was kind of it. I wound up changing the first line to SPWeb oSite = SPContext.Current.Site.RootWeb; instead of "SPSite oSite = SPContext.Current.Site" and it worked just fine. Below is the new code:

public void GetBanners() {

string strCode = "";

    SPWeb oSite = SPContext.Current.Site.RootWeb;
    SPList oList = oSite.Lists["MainPage Banner Library"];
    SPView lv = oList.Views["All Pictures"];
    SPListItemCollection lic = oList.GetItems(lv);

    foreach (SPListItem li in lic) {
        if(li["Enabled"] is bool && (bool)li["Enabled"] == true) {
            if(li["URL"] is string && li["URL"].ToString() != "") {
                strCode = strCode + "<a href=\"" + GetURL(li["URL"].ToString()) + "\" target=\"_blank\"><img src=\"/MainPage Banner Library/" + li["Name"].ToString() + "\" alt=\" \" /></a>";
            } else {
                strCode = strCode + "<img src=\"/MainPage Banner Library/" + li["Name"].ToString() + "\" alt=\" \" />";
            }

        }
    }   

ltBanners.Text = strCode;

}

1

1 Answers

0
votes

It may be the line SPList oList = oSite.AllWebs[0].Lists["MainPage Banner Library"]; that is causing your problem as this needs to read all lists for the web then do a title comparison to return the list you are after.

Try using either SPList oList = oSite.AllWebs[0].Lists[ListGUID]; or SPList oList = oSite.AllWebs[0].GetList(ServerRelativeListURL);.

Quote from MSDN - Handling Large Folders and Lists

Whenever possible, acquire a reference to a list by using the list's GUID or URL as a key.

You can retrieve an SPList object from the SPWeb.Lists property by using the list's GUID or display name as an indexer. Using SPWeb.Lists[GUID] and SPWeb.GetList(strURL) is always preferable to using SPWeb.Lists[strDisplayName]. Using the GUID is preferable because it is unique, permanent, and requires only a single database lookup. The display name indexer retrieves the names of all the lists in the site and then does a string comparison with them. If you have a list URL instead of a GUID, you can use the GetList method in SPWeb to look up the list's GUID in the content database before retrieving the list.