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;
}