使用SPFolderCollection以及SPFolder对象分层次显示文档列表

 Shaerpoint的文档库的功能很强大,显示文档列表的方式也很多,我们可以使用系统的视图显示文档,我们也可以定义视图来显示文档列表。在系统提供的视图中您可以选择是否显示文件夹。但是,我们有时候希望向察看本地资源管理器那样的分层查看文档列表,即展现出文档以及文档夹之间的层次关系。我这里介绍使用SPFoldercollection SPFolder对象模型来实现这个功能。

首先,由于我们的Webpart可能要用于显示不同的文档库,所以,我们自定义一个属性用于传递您要显示的文档库的名称:

namespace WPDocLibTree

{

/// <summary>

/// Description for WebPart1.

/// </summary>

[DefaultProperty("Text"),

ToolboxData("<{0}:WPDocLibTree runat=server></{0}:WPDocLibTree>"),

XmlRoot(Namespace="WPDocLibTree")]

public class WPDocLibTree : Microsoft.SharePoint.WebPartPages.WebPart

{

private const string defaultText = "";

 

private string text = defaultText;

private string _myDoc="";

 

[Browsable(true),

Category("Miscellaneous"),

DefaultValue(defaultText),

WebPartStorage(Storage.Personal),

FriendlyName("Text"),

Description("Text Property")]

public string Text

{

get

{

return text;

}

 

set

{

text = value;

}

}

 

//文档库名称

[Category("文档库定义")]

[DefaultValue("")]

[WebPartStorage(Storage.Personal)]

[FriendlyNameAttribute("文档库名称")]

[Description("请输入要显示的文档库德名称")]

[Browsable(true)]

[XmlElement(ElementName="myDoc")]

public string myDoc

{

get

{

return _myDoc;

}

set

{

_myDoc=value;

}

}

 

然后,在RenderWebPart来显示我们的文档列表:

 

protected override void RenderWebPart(HtmlTextWriter output)

{

//首先,我们通过下列语句格式化我们的文档列表:

output.Write("<style><!--.folder1 { font-size: 10pt; color: #000080;margin-left: 50; font-weight:bold;}");

output.Write (".file1 { font-size: 10pt; color: #3366CC;margin-left: 50; }");

output.Write(".folder2 { font-size: 10pt; color: #000080;margin-left: 70; font-weight:bold}");

output.Write (".file2 { font-size: 10pt; color: #3366CC;margin-left: 70 }");

output.Write(".folder3 { font-size: 10pt; color: #000080;margin-left: 90; font-weight:bold}");

output.Write (".file3 { font-size: 10pt; color: #3366CC;margin-left: 90 }");

output.Write(".folder4 { font-size: 10pt; color: #000080;margin-left: 110; font-weight:bold}");

output.Write (".file4 { font-size: 10pt; color: #3366CC;margin-left: 110 }");

output.Write ("--></style>");

//打开当前站点

SPWeb site=SPControl.GetContextWeb(Context);

//获取站点中的所有文件夹

SPFolderCollection folders=site.Folders;

//获取指定的文档库(文件夹)

SPFolder folder=folders[_myDoc];

//获取所有文档

SPFileCollection files=folder.Files;

for(int i=0;i<files.Count;i++)

{

output.Write("<a class='file1' target='_blank' href=' "+files[i].ServerRelativeUrl.ToString()+"'><img  border=/"0/" src=/"../_layouts/images/keyword.gif/">&nbsp;&nbsp;"+files[i].Name.ToString()+"</a><br>");

}

//检查是否有子文件夹

SPFolderCollection subFolders=folder.SubFolders ;

if(subFolders.Count>1)

{

 // 显示子文件夹  

    foreach(SPFolder subFolder in subFolders)

    {

//过滤掉系统文件夹“forms

if(subFolder.Name.ToString().ToUpper()!="FORMS")

{

//folder=subFolder;

output.Write("<a class='folder1' target='_blank' href=' "+subFolder.ServerRelativeUrl.ToString()+"'><img  border=/"0/" src=/"../_layouts/images/folder.gif/">&nbsp;&nbsp;"+subFolder.Name.ToString()+"</a><br>");

SPFileCollection files1=subFolder.Files;

//显示子文件夹中的文件

foreach(SPFile file1 in files1)

{

output.Write("<a class='file2' target='_blank' href=' "+file1.ServerRelativeUrl.ToString()+"'><img  border=/"0/" src=/"../_layouts/images/keyword.gif/">&nbsp;&nbsp;"+file1.Name.ToString()+"</a><br>");

 

}

}

    }

}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想好点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值