冰镇可乐 发表于 2015-9-25 11:41:45

我的第一个SharePoint WebPart

  本文是我的第一个WebPart开发过程,参考文档演练:为 SharePoint 创建 Web 部件。
  
  1. 启动Visual Studio 2010,新建一SharePoint项目,项目模板选择SharePoint > 2010 > 空白SharePoint项目。确定项目名称和存放位置后点击“确定”按钮。

  
  2. 指定用于调试的SharePoint网站和安全级别:注意:这里我们选择“部署为场解决方案”。点击“完成”按钮,项目创建成功。

  
  3. 为新建的SharePoint项目添加新项,项目模版选择SharePoint > 2010 > Web部件。确定部件名称后点击“添加”按钮。

  
  4. 打开新添加Web部件的代码文件(本文中是WebPart1.cs),用下面的代码替换。



using System;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace HelloWebPart.WebPart1
{

public class WebPart1 : WebPart
{
private DataGrid grid;
private static string verbText = "Show Managers Only";
private Label errorMessage = new Label();
protected string xmlFilePath;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
WebDisplayName("Path to Employee Data File"),
WebDescription("Location of the XML file that contains employee data")]
public string DataFilePath
{
get
{
return xmlFilePath;
}
set
{
xmlFilePath = value;
}
}
protected override void CreateChildControls()
{
// Define the grid control that displays employee data in the Web Part.
grid = new DataGrid();
grid.Width = Unit.Percentage(100);
grid.GridLines = GridLines.Horizontal;
grid.HeaderStyle.CssClass = "ms-vh2";
grid.CellPadding = 2;
grid.BorderWidth = Unit.Pixel(5);
grid.HeaderStyle.Font.Bold = true;
grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
// Populate the grid control with data in the employee data file.
try
{
DataSet dataset = new DataSet();
dataset.ReadXml(xmlFilePath, XmlReadMode.InferSchema);
grid.DataSource = dataset;
grid.DataBind();
}
catch (Exception x)
{
errorMessage.Text += x.Message;
}
// Add control to the controls collection of the Web Part.
            Controls.Add(grid);
Controls.Add(errorMessage);
base.CreateChildControls();
}
public override WebPartVerbCollection Verbs
{
get
{
WebPartVerb customVerb = new WebPartVerb("Manager_Filter_Verb",
new WebPartEventHandler(CustomVerbEventHandler));
customVerb.Text = verbText;
customVerb.Description = "Shows only employees that are managers";
WebPartVerb[] newVerbs = new WebPartVerb[] { customVerb };
return new WebPartVerbCollection(base.Verbs, newVerbs);
}
}
protected void CustomVerbEventHandler(object sender, WebPartEventArgs args)
{
int titleColumn = 2;
foreach (DataGridItem item in grid.Items)
{
if (item.Cells.Text != "Manager")
{
if (item.Visible == true)
{
item.Visible = false;
}
else
{
item.Visible = true;
}
}
}
if (verbText == "Show Managers Only")
{
verbText = "Show All Employees";
}
else
{
verbText = "Show Managers Only";
}
}
}
}
  
  
5. 将下面的XML内容保存到一个文件,这里以C:\Shared Folders\Tutorials\HelloWebPart\HelloWebPart\data.xml为例。



<?xml version="1.0" encoding="utf-8" ?>
<employees xmlns="http://schemas.microsoft.com/vsto/samples">
<employee>
<name>David Hamilton</name>
<hireDate>2001-05-11</hireDate>
<title>Sales Associate</title>
</employee>
<employee>
<name>Karina Leal</name>
<hireDate>1999-04-01</hireDate>
<title>Manager</title>
</employee>
<employee>
<name>Nancy Davolio</name>
<hireDate>1992-05-01</hireDate>
<title>Sales Associate</title>
</employee>
<employee>
<name>Steven Buchanan</name>
<hireDate>1955-03-04</hireDate>
<title>Manager</title>
</employee>
<employee>
<name>Suyama Michael</name>
<hireDate>1963-07-02</hireDate>
<title>Sales Associate</title>
</employee>
</employees>
  
  6. 在 Visual Studio 中按“F5”,启动调试SharePoint项目,SharePoint 网站将自动打开。单击“网站操作”,然后单击“更多选项”,出现选择创建类型窗口。我们选择“Web部件页”类型,然后点击右边的“创建”按钮。

  
  7. 在新建Web部件页页面,输入页面的名称,然后点击“创建”按钮。

  
  8. Web部件页内容编辑页面被打开,见下图。

  
  9. 点击页眉区域的“添加Web部件”链接,从“类别”列表中选择“Custom”,从“Web部件”列表中选择“WebPart1”(这里的名称就是我们在第3步添加的WebPart名称),然后点击“添加”按钮。

  
  10. WebPart1部件被成功的添加到了页面,下图红框。

  
  11. 点击WebPart1右上角的箭头,然后点击“编辑Web部件”。

  
  12. Web部件属性窗格在右侧被打开。展开“杂项”节点,输入先前创建的 XML 文件的路径(C:\Shared Folders\Tutorials\HelloWebPart\HelloWebPart\data.xml),然后点击“确定”按钮。

  
  13. XML文件的内容被成功的显示在了WebPart1部件里。

  
  14. 点击WebPart1右上角的箭头,然后再点击“Show Manager Only”或“Show All Employees”,还可以实现显示数据范围的切换。
页: [1]
查看完整版本: 我的第一个SharePoint WebPart