我有从 Sharepoint 2010 文档库中提取一些图像的代码。当我登录并测试一切正常时,但当它发布时,它会强制登录屏幕。
这是踢球者:如果我通过普通 HTML 引用图像,则发布的页面不需要登录。只有当我尝试将图像拉入并将它们的列表构建到 asp:literal 控件中时。
据我所知,列表上的权限允许匿名阅读。某处有问题。请帮忙!
下面是我正在使用的代码:
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;
}
更新:Aquila 的回答是这样的。我最终将第一行更改为 SPWeb oSite = SPContext.Current.Site.RootWeb; 而不是“SPSite oSite = SPContext.Current.Site”,它工作得很好。以下是新代码:
公共无效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;
}