冠军

导航

为 Sharepoint 创建可视的 WebPart

原文地址:http://msdn.microsoft.com/zh-cn/library/ff597539(en-us).aspx

 

创建一个 Visual Web Part  项目

1. 在 VS2010 中选择文件 -> 新建 -> 项目

2. 在安装的项目模板中选择 Visual C# ,选择其中 Sharepoint 下的 2010

3. 选择 Visual Web Part  项目模板,见图1,提供项目名称,例如:SampleWebPart,项目的保存位置,然后确定。

图 1

4. 在 What local site do you want to use for debugging 下拉列表框中, 选择使用的站点 (例如http://localhost/sites/SampleWebSite). 还要选中 Deploy as a farm solution 选项,然后 Finish.

注意,在项目创建之后,解决方案管理器包含一个默认的 Visual Web Part, 名叫 VisualWebPart1 ,见图2, 还可以在解决方案管理器中的 Features 和 Package 节点,Feature 使用 SharePoint 理解的方式组织你的应用,Features 可以在站点级别或者 Web 级别被发布到 SharePoint Foundattion,在发布解决方案到 SharePoint Foundation 中的时候,Package 包含 Features 和其他的内容。

图2 解决方案中的 SampleWebPart  项目

在 WebPart 中加入 TreeView

1. 在解决方案管理器中,展开 VisualWebPart1  节点,右击 VisualWebPart1UserControl.ascx  文件,选择设计视图。

2. 在工具箱中,拖动 TreeView 到设计器。

3. 选中 TreeView ,在属性窗口中,设置 ID 为 siteStructure

 

加入代码

1. 在解决方案管理器中,展开  VisualWebPart1UserControl.ascx  节点,右击 VisualWebPart1UserControl.ascx.cs ,选择代码视图。

2. 插入代码

 1 using System;
 2 using System.Web.UI;
 3 using System.Web.UI.WebControls;
 4 using System.Web.UI.WebControls.WebParts;
 5 using Microsoft.SharePoint;
 6 using Microsoft.SharePoint.Utilities;
 7 using System.Web;
 8 namespace BonnevilleTestBed.Bonneville
 9 {
10   public partial class BonnevilleUserControl : UserControl
11   {
12     protected void Page_Load(object sender, EventArgs e)
13     {
14       SPWeb thisWeb = null;
15       TreeNode node;
16       thisWeb = SPContext.Current.Web;
17       //Add the Web's title as the display text for the tree node, and add the URL as the NavigateUri.
18       node = new TreeNode(thisWeb.Title, nullnull, thisWeb.Url, "_self");
19       //The Visual Web Part has a treeview control called siteStructure.
20       siteStructure.Nodes.Add(node);
21       //Get a reference to the current node, so child nodes can be added in the correct position.
22       TreeNode parentNode = node;
23       //Iterate through the Lists collection of the Web.
24       foreach (SPList list in thisWeb.Lists)
25       {
26         if (!list.Hidden)
27         {
28           node = new TreeNode(list.Title, nullnull, list.DefaultViewUrl, "_self");
29           parentNode.ChildNodes.Add(node);
30         }
31       }
32       foreach (SPWeb childWeb in thisWeb.Webs)
33       {
34         //Call our own helper function for adding each child Web to the tree.
35         addWebs(childWeb, parentNode);
36         childWeb.Dispose();
37       }
38       siteStructure.CollapseAll();
39     }
40     void addWebs(SPWeb web, TreeNode parentNode)
41     {
42       TreeNode node;
43       node = new TreeNode(web.Title, nullnull, web.Url, "_self");
44       parentNode.ChildNodes.Add(node);
45       parentNode = node;
46       foreach (SPList list in web.Lists)
47       {
48         if (!list.Hidden)
49         {
50           node = new TreeNode(list.Title, nullnull, list.DefaultViewUrl, "_self");
51           parentNode.ChildNodes.Add(node);
52         }
53       }
54       foreach (SPWeb childWeb in web.Webs)
55       {
56         //Call the addWebs() function from itself (i.e. recursively) 
57         //to add all child Webs until there are no more to add.
58         addWebs(childWeb, parentNode);
59      childWeb.Dispose();
60 
61       }
62     }
63   }
64 }

 

 

创建和发布 WebPart

1. 使用 F5 创建和发布 WebPart, 可以单步调试。

2. 另外,还可以选择 Build 菜单中的 Build Solution,检查 Build 中没有错误,然后,选择 Deploy Solution

 

创建 WebPart 页

1. 如果你使用 F5 调试你的应用,默认,显示你的 WebPart 的页面被显示出来,另外,打开 SharePoint 站点,点击 Site Actions,点击 View All Site Content, 点击 Create, 卷绕然后选择 Web Part Page 选择。

2. 在 WebPart 屏幕上,提供你的 WebPart 页的信息,例如名称,然后,进行模板布局。

3. 在 Document Library 下拉列表中,选择 Site Pages, 然后选择 Create,SharePoint 将会创建并显示你的 WebPart 页。

图3

 

将 WebPart 加入到 WebPart 页上

1. 在 Web Part 页上,在你希望显示 WebPart 的位置点击 Add a Web Part

2. 在 Categories  列表中,选择 Custom,在 Web Parts  中,点击 VisualWebPart1

3. 在页面顶部的 About the Web Part 中,点击 Add, WebPart 将如图4 所示,加到你所选择的区域,注意,列表和 SubWebs 将会以层次结构显示。

图4 加入到 WebPart 页之后的 WebPart

 

 


 

 

posted on 2010-12-15 22:55  冠军  阅读(841)  评论(0编辑  收藏  举报