我正在使用名为employeeProfile的SP2010开发一个webpart。 我的问题是我无法在webpart HTML页面中显示图片。远程服务器返回错误:(401)未经授权。
这是HTML代码:
<img alt="User Pic" src=<%#Eval("Photo")%>>
using (SPSite site1 = new SPSite(SPContext.Current.Site.ID, userToken))
{
using (SPWeb web1 = site1.OpenWeb(SPContext.Current.Web.ID))
{
//GET EMPLOYEE INFORMATION FROM LIST EMPLOYEE
SPList employee = web1.Lists["Employee"];
SPQuery Query = new SPQuery();
Query.Query = "<Where>" +
"<Eq><FieldRef Name='Windows_x0020_ID' /><Value Type='User'>" +
currentUser + "</Value></Eq>" +
"</Where><OrderBy>" +
"<FieldRef Name='Created' />" +
"</OrderBy>";
Query.RowLimit = 1;
SPListItemCollection ColItem = employee.GetItems(Query);
if (ColItem.Count > 0)
{
SPListItem getItem = ColItem[0];
DataTable dataTable = new DataTable();
dataTable = ColItem.GetDataTable();
dataTable.Columns.Add("Photo").DefaultValue = "";
if (getItem.Attachments.Count > 0)
{
SPFile file = null;
for (int i = 0; i < getItem.Attachments.Count; i++)
{
file = getItem.ParentList.ParentWeb.GetFile(getItem.Attachments.UrlPrefix + getItem.Attachments[i].ToString());
//dataTable.Columns.Add("Photo").DefaultValue = SPContext.Current.Site.RootWeb.Url.ToString() + "" + "/" + file.
}
foreach (DataRow row in dataTable.Rows)
{
row["Photo"] = SPContext.Current.Site.RootWeb.Url.ToString() + "" + "/" + file.Url;
}
}
dataEmployee.DataSource = dataTable;
dataEmployee.DataBind();
}
答案 0 :(得分:0)
这可能是权限问题。用户是否可以访问网络?使用SPSecurity.RunwithElevatedPrevilage。这可能会对你有所帮助。
<img alt="User Pic" src=<%#Eval("Photo")%>SPSecurity.RunWithElevatedPrivileges(delegate(){
using (SPSite site1 = new SPSite(SPContext.Current.Site.ID, userToken))
{
using (SPWeb web1 = site1.OpenWeb(SPContext.Current.Web.ID))
{
//GET EMPLOYEE INFORMATION FROM LIST EMPLOYEE
SPList employee = web1.Lists.TryGetList("Employee");
SPQuery Query = new SPQuery();
Query.Query = "<Where>" +
"<Eq><FieldRef Name='Windows_x0020_ID' /><Value Type='User'>" +
currentUser + "</Value></Eq>" +
"</Where><OrderBy>" +
"<FieldRef Name='Created' />" +
"</OrderBy>";
Query.RowLimit = 1;
SPListItemCollection ColItem = employee.GetItems(Query);
if (ColItem.Count > 0)
{
SPListItem getItem = ColItem.FirstOrDefault();
DataTable dataTable = new DataTable();
dataTable = ColItem.GetDataTable();
dataTable.Columns.Add("Photo").DefaultValue = "";
if (getItem.Attachments.Count > 0)
{
SPFile file = null;
for (int i = 0; i < getItem.Attachments.Count; i++)
{
file = getItem.ParentList.ParentWeb.GetFile(getItem.Attachments.UrlPrefix + getItem.Attachments[i].ToString());
//dataTable.Columns.Add("Photo").DefaultValue = SPContext.Current.Site.RootWeb.Url.ToString() + "" + "/" + file.
}
foreach (DataRow row in dataTable.Rows)
{
row["Photo"] = SPContext.Current.Site.RootWeb.Url.ToString() + "" + "/" + file.Url;
}
}
dataEmployee.DataSource = dataTable;
dataEmployee.DataBind();
}
}
}
}
);