原文:http://www.cnblogs.com/lrforever/archive/2010/02/10/1667129.html
1、在 Sharepoint Desinger里拷贝列表的DispForm.aspx.重命名为DispForm1.aspx;
2、在DispForm1.aspx添加该列表的数据视图;
3、在dvt_1.rowview XSL template添加附件的地方加上 <SharePoint:AttachmentsField ControlMode="Display" FieldName="Attachments" runat="server" Visible="true"/>
4、测试可以正常显示
but
如果在同一网站集的其他站点引用此列表,建立此列表的数据视图,采用同上的办法却会报错
System.InvalidOperationException: 对象的当前状态使该操作无效,实在搞不懂啥问题。
希望高手能现身,指点一下。多谢啦
2009年2月24日更新内容如下:
上述的方法在跨站点的时候没法使用 ,但为了解决这个问题,采用了编写用户控件的方法。
代码如下


using
Microsoft.SharePoint;
public partial class UCAttachment : System.Web.UI.UserControl { protected void Page_Load( object sender, EventArgs e) { if ( ! this .IsPostBack) { try { GetAttachment(); // 取得附件的方法 } catch (Exception ex) { Response.Write(ex); } }
} private void GetAttachment() { int ID = 0 ; // 取得页面参数 string id = Request.QueryString[ " ID " ]; int .TryParse(id, out ID); // 转化为int型 if (ID == 0 ) { Response.Write( " 无效ID " ); } else { SPSite site = new SPSite( " http://x.x.x.x/ " );
SPWeb Myweb = site.OpenWeb( " myweb " ); SPList StartStepList = Myweb.Lists[ " mylist " ]; // 打开列表
SPListItem StartStepItem = StartStepList.Items.GetItemById(ID); // 取得当前信息条记录 SPAttachmentCollection attach = StartStepItem.Attachments; // 取得当前信息条的附件 if (attach.Count > 0 ) { Label1.Text = " 附件: " ; for ( int i = 0 ; i < attach.Count; i ++ ) { String url = attach.UrlPrefix + attach[i];
ListItem li = new ListItem(attach[i], url);
BulletedList1.Items.Add(li); } } } } }
public partial class UCAttachment : System.Web.UI.UserControl { protected void Page_Load( object sender, EventArgs e) { if ( ! this .IsPostBack) { try { GetAttachment(); // 取得附件的方法 } catch (Exception ex) { Response.Write(ex); } }
} private void GetAttachment() { int ID = 0 ; // 取得页面参数 string id = Request.QueryString[ " ID " ]; int .TryParse(id, out ID); // 转化为int型 if (ID == 0 ) { Response.Write( " 无效ID " ); } else { SPSite site = new SPSite( " http://x.x.x.x/ " );
SPWeb Myweb = site.OpenWeb( " myweb " ); SPList StartStepList = Myweb.Lists[ " mylist " ]; // 打开列表
SPListItem StartStepItem = StartStepList.Items.GetItemById(ID); // 取得当前信息条记录 SPAttachmentCollection attach = StartStepItem.Attachments; // 取得当前信息条的附件 if (attach.Count > 0 ) { Label1.Text = " 附件: " ; for ( int i = 0 ; i < attach.Count; i ++ ) { String url = attach.UrlPrefix + attach[i];
ListItem li = new ListItem(attach[i], url);
BulletedList1.Items.Add(li); } } } } }