惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
爱范儿
爱范儿
博客园 - 聂微东
美团技术团队
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG
罗磊的独立博客
V
Visual Studio Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
V
V2EX
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 吴东雷

配置NuGet自动下载丢失的Dll SharePoint2010站点在IE下的操作异常 ExtJS的store.sync向Asp.net MVC的Action提交时引发System.Reflection.AmbiguousMatchException异常 ReportViewerWebWebpart显示报表时显示的脚本错误:Uncaught Sys.ArgumentNullException: Sys.ArgumentNullException: 值不能为 null。参数名: panelsCreated[0] 【转载】javascript getComputedStyle,getPropertyValue,CurrentStyle说明 【原创】关于SPSecurity.RunWithElevatedPrivileges的一个问题[A problem about SPSecurity.RunWithElevatedPrivileges] 【原创】如何让SharePoint2010的内联代码支持.Net framework 3.5[How to embed inline code in aspx with .net framework 3.5 syntax] 【半原创】SharePoint CAML 查询生成器[SharePoint 2010 CAML Query Builder] 【原创】如何在DataFormWebPart中嵌入自定义控件[How to embeded custom control in DFWP] 【原创】SharePoint2010的SaveButton如何实现在跳转前给用户以提示[How to implement SaveButton display an alert when Redirect action] 【原创】在DataFormWebPart中将列表附件显示为图片(一)[How to display list item attachments as image in DFWP Part1] Asp.net中全局缓存的几种方式 修改Model中hasMany中自动生成的属性值 ExtJS中自定义组件并将事件暴露给调用者 从控制台测试SharePoint遇到的两个问题 The Sencha Class System(理解ExtJs定义类的过程) IIS7中Sites(站点), Applications(应用程序), Virtual Directories(虚拟目录)的粗浅理解 按钮的ajax请求时,一次点击两次提交的问题 MVC3中Razor模板引擎如何修改View的基类
【原创】在DataFormWebPart中将列表附件显示为图片(二)[How...
吴东雷 · 2012-08-31 · via 博客园 - 吴东雷

       在第一部分中实现的将附件显示成图片的方式,只能是在列表所在的站点中,跨站(Cross-site)的时候就不能显示了.我经过很长时间的尝试后发现,AttachmentField在跨转的时候,其内部的ItemContext只能是当前访问站点的Context,所以会在当前站点下去查找相应的List与Field,这导致了根本就找不到,所以也就什么也显示不出来了.

       在确定了不可能使用AttachmentField进行跨站显示附件后,我重新写了一个控件来模拟生成与AttachmentField一模一样的输出.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.Security;
using System.Web;
using System.Security.Permissions;

namespace Only.SPLibraryExtend.WebControls
{
    /// <summary>
    /// 用来显示附件列表 完全模拟SharePoint的AttachmentField的输出
    /// </summary>
    [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class AttachmentsField:Control
    {
        public int ItemId
        {
            get;
            set;
        }

        public string ListName
        {
            get;
            set;
        }

        public string WebUrl
        {
            get;
            set;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            SPSite site = SPContext.Current.Web.Site;

            using (SPWeb web = site.OpenWeb(WebUrl))
            {
                SPList list = web.Lists[ListName];
                SPListItem item = list.GetItemById(ItemId);
                HtmlTable table = new HtmlTable();
                table.Attributes["id"]="idAttachmentsTable";                
                table.Border = 0;
                table.CellSpacing = 0;
                table.CellPadding = 0;
                foreach (String attachmentname in item.Attachments)
                {
                    String attachmentAbsoluteURL = item.Attachments.UrlPrefix + attachmentname;
                    HtmlTableRow tr = new HtmlTableRow();
                    tr.Attributes["id"]=Guid.NewGuid().ToString();                    

                    HtmlTableCell cell = new HtmlTableCell();
                    cell.Attributes["class"] = "ms-vb";
                    
                    HtmlGenericControl span=new HtmlGenericControl("Span");
                    span.Attributes["dir"]="ltr";
                    
                    HtmlGenericControl A=new HtmlGenericControl("a");
                    A.Attributes["tabIndex"] = "1";
                    A.Attributes["onmousedown"] = @"return VerifyHref(this, event, '0', 'SharePoint.OpenDocuments.3', '');return false;";
                    A.Attributes["onclick"] =@"DispDocItemExWithServerRedirect(this, event, 'FALSE', 'FALSE', 'FALSE', 'SharePoint.OpenDocuments.3', '0', '');return false;";
                    A.Attributes["href"] = attachmentAbsoluteURL;
                    A.InnerText = attachmentname;

                    span.Controls.Add(A);
                    cell.Controls.Add(span);
                    LiteralControl nbsp = new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;");
                    cell.Controls.Add(nbsp);                    

                    tr.Cells.Add(cell);

                    table.Rows.Add(tr);
                }
                this.Controls.Add(table);
            }                        
        }
    }
}

这个控件需要配置ItemId,WebUrl以及ListName,以便能够找到相应的Item.这三个属性在配置DataFormWebPart进行跨转显示数据的属性是一样的,当然这三个属性在DataFormWebPart进行跨站时是配置在SPDataSource对象的参数上的.要将这个控件嵌入到DataFormWebPart中,需要

【原创】如何在DataFormWebPart中嵌入自定义控件[How to embeded custom control in DFWP]这篇文章中的相应内容,所以在DataFormWebPart中加入

<xsl:element name="Only:AttachmentsField">
    <xsl:attribute name="runat">server</xsl:attribute>
    <xsl:attribute name="ListName">园区企业</xsl:attribute>
    <xsl:attribute name="WebUrl">/admin</xsl:attribute>
    <xsl:attribute name="ItemId"><xsl:value-of select="@ID" /></xsl:attribute>
</xsl:element>

在配合了相应的CSS的的最终显示效果是

image

一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。