我们可以创建自定义WebPart来与站点和列表数据共同运作。本编程任务将展示如何创建一个简单的Web Part来寻找当前网站下所有包含10条以上数据的列表,并显示其标题和列表项数量。
步骤
1、创建一个WebPart。这里假设你已经创建好了一个名为SimpleWebPart的应用程序。
2、在SimpleWebPart应用程序中打开WebCustomControl1.cs或.vb,添加如下的命名空间引用。
VB
Imports
Microsoft.SharePoint
Imports Microsoft.SharePoint.Utilities
Imports Microsoft.SharePoint.Utilities
C#
using
Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Utilities;
3、例子中原有的移除HtmlControl对象,以及对应的的变量声明,_mybutton_click事件,CreateChildControls方法等。
4、用下面的代码替换RenderWebPart方法中的内容:
VB
Dim
mySite
As
SPWeb
=
SPContext.Current.Web
output.Write(SPEncode.HtmlEncode(mySite.Title))
Dim subSites As SPWebCollection = mySite.Webs
Dim site As SPWeb
For Each site In subSites
output.Write(SPEncode.HtmlEncode(site.Title) & " <BR> " )
Dim lists As SPListCollection = site.Lists
Dim list As SPList
For Each list In lists
If list.ItemCount > 10 Then
output.Write(SPEncode.HtmlEncode(list.Title) & " :: "
& list.ItemCount & " <BR> " )
End If
Next list
Next site
output.Write(SPEncode.HtmlEncode(mySite.Title))
Dim subSites As SPWebCollection = mySite.Webs
Dim site As SPWeb
For Each site In subSites
output.Write(SPEncode.HtmlEncode(site.Title) & " <BR> " )
Dim lists As SPListCollection = site.Lists
Dim list As SPList
For Each list In lists
If list.ItemCount > 10 Then
output.Write(SPEncode.HtmlEncode(list.Title) & " :: "
& list.ItemCount & " <BR> " )
End If
Next list
Next site
C#
SPWeb mySite
=
SPContext.Current.Web;
output.Write(SPEncode.HtmlEncode(mySite.Title));
SPWebCollection subSites = mySite.Webs;
foreach (SPWeb site in subSites)
{
output.Write(SPEncode.HtmlEncode(site.Title) + " <BR> " );
SPListCollection lists = site.Lists;
foreach (SPList list in lists)
{
if (list.ItemCount > 10 )
{
output.Write(SPEncode.HtmlEncode(list.Title) + " : " +
list.ItemCount + " <BR> " );
}
}
}
output.Write(SPEncode.HtmlEncode(mySite.Title));
SPWebCollection subSites = mySite.Webs;
foreach (SPWeb site in subSites)
{
output.Write(SPEncode.HtmlEncode(site.Title) + " <BR> " );
SPListCollection lists = site.Lists;
foreach (SPList list in lists)
{
if (list.ItemCount > 10 )
{
output.Write(SPEncode.HtmlEncode(list.Title) + " : " +
list.ItemCount + " <BR> " );
}
}
}
上面的示例代码首先输出当前网站的标题,接着遍历所有的子网站并打印其标题,然后遍历该网站下所有的列表,找出列表项数量大于10的列表,打印其标题和项数。
5、在Build菜单中,点击Build Solution来编译该WebPart。
6、在web.config文件中提升WSS的信任级别。
web.config文件通常位于//Inetput/wwwroot/wss/VirtualDirectories/80下。找到下面的行:
<
trust
level
="WSS_Minimal"
originUrl
=""
/>
将其替换为:
<
trust
level
="WSS_Medium"
originUrl
=""
/>
7、重启IIS,使对信任级别的更改生效。
在任何一个Web部件页或主页中拖入该WebPart,就可以看到显示的列表结果了。