SharePoint2010 - 使用代码创建、删除、更新、激活、停用列表






4.80/5 (4投票s)
在 SharePoint 2010 中使用代码创建、删除、更新、激活、停用列表
使用代码创建列表
对于本次演示,您将使用上述团队网站。
A 部分:创建和删除作者列表
(1) 打开 Visual Studio 2010
(2) 文件 > 新建 > 项目
- 选择 Windows
- Windows 窗体应用程序
- 名称:WindowsFormsApplications1
- 点击确定按钮
(3) 在解决方案资源管理器中
- 右键单击引用 > 添加
- 选择 .NET 选项卡
- 选择 Microsoft.SharePoint
- 点击“确定”
(4) 在解决方案资源管理器中
- 右键单击属性 > 打开
- 在属性窗口中,按如下所示设置属性。
(5) 打开 Form1 页面
- 创建两个按钮名称和一个列表框,如上图所示。
(6) Form1.cs 页面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(7) 运行 (F5)
- 点击创建列表按钮
(8) 现在浏览团队网站 (http://tosu-pc/SitePages/Home.aspx)
- 刷新页面
- 作者列表已创建,如上图所示
- 点击作者列表
删除作者列表
(9) 返回 Visual Studio 运行模式
- 现在点击删除列表按钮
- 已删除的作者列表显示在 listBox1 中
(10) 浏览团队网站
- 刷新它
- 作者列表已从列表中删除,如下图所示
B 部分:将默认列(标题)的名称更改为用户名
(1) 更新 Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
// update the Default column (Title)
var title = list.Fields["Title"];
title.Title = "User Name";
title.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) 运行 F5
(3) 在浏览器中检查
- 标题列被用户名取代
B 部分:在列表中添加新列
(1) 更新 Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
// update the Default column (Title)
var title = list.Fields["Title"];
title.Title = "User Name";
title.Update();
// Add New column
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate" , SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) F5
(3) 在浏览器中检查
- 刷新它
- 作者列表已创建,列名:用户名、员工、工资/费率和简历
- 点击添加新项目
- 在弹出窗口中输入项目,最后点击保存
- 视图被填充,如下所示
C 部分:使用验证
- 如果不是员工,则工资低于 50 美元
(1) 更新 Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
// use validation [if not employee than sal is less than 50$
list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
list.ValidationMessage = "The maximum rate for a contributor is $50";
list.Update();
// update the Default column (Title)
var title = list.Fields["Title"];
title.Title = "User Name";
title.Update();
// Add New column
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate" , SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) F5
- 确保在创建之前删除了作者列表
- 在浏览器中查看
- 点击添加新项目
D 部分:使用 VS 向列表中添加项目
(1) 更新 Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const string SiteUrl = "http://tosu-pc/SitePages/Home.aspx";
public Form1()
{
InitializeComponent();
}
private void CreateListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
// use validation [if not employee than sal is less than 50$
list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
list.ValidationMessage = "The maximum rate for a contributor is $50";
// update the Default column (Title)
var titleFildName = "Title";
var title = list.Fields[titleFildName];
title.Title = "User Name";
title.Update();
// Add New column
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate" , SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
list.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
// Add items into the List
var item = list.Items.Add();
item[titleFildName] = "Abhishek";
item[empFieldName] = false;
item[rateFieldName] = 30;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rajkumar";
item[empFieldName] = true;
item[rateFieldName] = 50;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rahul";
item[empFieldName] = true;
item[rateFieldName] = 60;
item.Update();
item = list.Items.Add();
item[titleFildName] = "milind";
item[empFieldName] = false;
item[rateFieldName] = 40;
item.Update();
listBox1.Items.Add("Authors list created");
}
}
private void DeleteListButton_Click(object sender, EventArgs e)
{
using (var site = new SPSite(SiteUrl))
{
var web = site.RootWeb;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
listBox1.Items.Add("Authors list deleted");
}
}
}
}
(2) F5
- 确保先删除列表。
- 在浏览器中检查。
- 点击 milind。
E 部分:将代码转移到实际的 SharePoint
(1) 打开另一个 Visual Studio 2010 实例
(2) 文件 > 新建 > 项目
(3) 在解决方案资源管理器中
- 右键单击功能
- 添加功能
(4) 将功能重命名为 CreateList
- 按照上图所示进行更改并保存
- 右键单击 CreateList > 添加事件接收器
(5) 更新 CreateList.EventReceiver.cs
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
namespace ListAndLibraries1.Features.CreateList
{
[Guid("cc22c160-bb85-4906-a4ee-8f49d4e93203")]
public class CreateListEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
var list = web.Lists[listId];
list.OnQuickLaunch = true;
// use validation [if not employee than sal is less than 50$
list.ValidationFormula = "IF([Employee], TRUE, [Salary/Rate] <=50)";
list.ValidationMessage = "The maximum rate for a contributor is $50";
// update the Default column (Title)
var titleFildName = "Title";
var title = list.Fields[titleFildName];
title.Title = "User Name";
title.Update();
// Add New column
var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
var rateFieldName = list.Fields.Add("Salary/Rate", SPFieldType.Currency, true);
var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);
var bio = list.Fields[bioFieldName] as SPFieldMultiLineText;
bio.NumberOfLines = 8;
bio.RichText = false;
bio.Update();
list.Update();
var view = list.DefaultView;
view.ViewFields.Add(empFieldName);
view.ViewFields.Add(rateFieldName);
view.ViewFields.Add(bioFieldName);
view.Update();
// Add items into the List
var item = list.Items.Add();
item[titleFildName] = "Abhishek";
item[empFieldName] = false;
item[rateFieldName] = 30;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rajkumar";
item[empFieldName] = true;
item[rateFieldName] = 50;
item.Update();
item = list.Items.Add();
item[titleFildName] = "Rahul";
item[empFieldName] = true;
item[rateFieldName] = 60;
item.Update();
item = list.Items.Add();
item[titleFildName] = "milind";
item[empFieldName] = false;
item[rateFieldName] = 40;
item.Update();
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists.TryGetList("Authors");
if (list != null)
{
list.Delete();
}
}
}
}
(6) 更改功能接收器,使其不自动激活
- 现在功能不会自动激活,我们必须手动激活它。
(7) 部署
- 刷新页面
- 网站操作 > 网站设置
- 在网站操作内部
- 选择管理网站模板
- 创建作者列表并激活功能
- 点击激活
- 作者列表已创建
- 点击作者列表
(8) 检查验证
- 点击列表选项卡
- 点击功能区中的列表设置
- 点击验证设置
(9) 停用功能
- 网站操作 > 网站设置
- 在网站操作 > 管理网站功能内部
- 点击停用,如下图所示。
- 点击停用此功能