Microsoft Visual Studio 2010 提供了一个可用于生成事件接收器的项目类型,事件接收器会在 Microsoft SharePoint 2010 网站上选择事件之前或之后执行操作。此示例演示如何将事件添加到自定义列表项的添加和更新操作中。

此 SharePoint 直观操作方法介绍了用于在 Visual Studio 2010 中创建和部署事件接收器的以下步骤:

  1. 重写 itemAdding 事件和 itemUpdating 事件。
  2. 验证正在将项目添加到的列表是否为“Open Position”列表。
  3. 提升权限,使此代码能访问安全网站以检索批准的职务。
  4. 将批准的“Job Titles”与在“Open Position”列表中创建的新项的职位进行比较。
  5. 在“Job Title”未获得批准时取消此事件。

在此示例中,安全子网站包含一个名为“Job Definitions”的列表,此列表为组织中的角色指定允许的职务。除职务之外,此列表还包含有关职务的工资机密信息,因此应阻止用户获取此信息。主网站中有一个名为“Open Positions”的列表,该列表将跟踪组织中的空缺。您为 itemAddingitemUpdating 事件创建两个事件接收器,它们将验证开放职位是否匹配“Job Definitions”列表中批准的职位之一。

先决条件

开始之前,创建子网站和所需列表。

创建“Job Definitions”子网站

  1. 在主网站的“网站操作”菜单上,单击“新建网站”。

  2. 在“新建网站”对话框中,单击“空白网站”。

  3. 在该对话框的右侧,单击“更多选项”。

  4. 在“标题”框中,键入 Job Definitions

  5. 在“网站地址”框中键入 JobDefinitions

  6. 在“权限”部分,单击“使用独有权限”,然后单击“创建”。

  7. 在“此网站的访问者”部分,选择“使用现有用户组”,然后选择“工作组网站所有者”。单击“确定”。

创建“Job Definitions”列表

  1. 在“Job Definitions”网站中,创建一个名为“Job Definitions”的自定义列表,此列表包含以下列:

    • Title(默认列)
    • MinSalary(货币)
    • MaxSalary(货币)
    • Role Type(选项:“Permanent”、“Contract”)
  2. 向此列表添加多个工作。请记下为创建的每个工作指定的职务,因为您稍后需要用到它们。

创建“Open Positions”列表

  • 在父网站中,创建一个名为“Open Positions”的自定义列表,此列表包含以下列:

    • Title(默认列)
    • Location(单行文本)

创建事件接收器

紧接着,在 Visual Studio 2010 中创建一个“事件接收器”项目,然后向事件接收器文件添加代码。

在 Visual Studio 2010 中创建 SharePoint 2010 事件接收器

  1. 启动 Visual Studio 2010。

  2. 在“文件”菜单上,单击“新建”,然后单击“项目”。

  3. 在“新建项目”对话框中的“已安装的模板”部分,展开“Visual Basic”或“Visual C#”,展开“SharePoint”,然后单击“2010”。

  4. 在模板列表中,单击“事件接收器”。

  5. 在“名称”框中键入 VerifyJob

  6. 保留其他字段的默认值不变,然后单击“确定”。

  7. 在“要使用哪个本地站点进行调试?”列表中,选择您的网站。

  8. 选择“部署为场解决方案”选项,然后单击“下一步”。

  9. 在“选择事件接收器设置”页的“需要哪种类型的事件接收器?”列表中,选择“列表项事件”。

  10. 在“哪个项应为事件源?”列表中,选择“自定义列表”。

  11. 在“处理以下事件”下,选中“正在添加项”和“正在更新项”复选框。单击“完成”。

修改事件接收器文件

  1. 在事件接收器文件中,向类添加以下代码。

    Public Function CheckItem(ByVal properties As SPItemEventProperties) As Boolean
    Dim jobTitle As String = properties.AfterProperties("Title").ToString()
    Dim allowed As Boolean = False
    Dim jobDefWeb As SPWeb = Nothing
    Dim jobDefList As SPList
    Dim privilegedAccount As SPUser = properties.Web.AllUsers("SHAREPOINT\SYSTEM")
    Dim privilegedToken As SPUserToken = privilegedAccount.UserToken Try
    Using elevatedSite As New SPSite(properties.Web.Url, privilegedToken)
    Using elevatedWeb As SPWeb = elevatedSite.OpenWeb()
    jobDefWeb = elevatedWeb.Webs("JobDefinitions")
    jobDefList = jobDefWeb.Lists("Job Definitions") For Each item As SPListItem In jobDefList.Items
    If item("Title").ToString() = jobTitle Then
    allowed = True
    Exit For
    End If
    Next End Using
    End Using Return (allowed) Finally
    jobDefWeb.Dispose()
    End Try
    End Function
    bool checkItem(SPItemEventProperties properties)
    {
    string jobTitle = properties.AfterProperties["Title"].ToString();
    bool allowed = false;
    SPWeb jobDefWeb = null;
    SPList jobDefList;
    SPUser privilegedAccount = properties.Web.AllUsers[@"SHAREPOINT\SYSTEM"];
    SPUserToken privilegedToken = privilegedAccount.UserToken;
    try
    {
    using (SPSite elevatedSite = new SPSite(properties.Web.Url, privilegedToken))
    {
    using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
    {
    jobDefWeb = elevatedWeb.Webs["JobDefinitions"];
    jobDefList = jobDefWeb.Lists["Job Definitions"];
    foreach (SPListItem item in jobDefList.Items)
    {
    if (item["Title"].ToString() == jobTitle)
    {
    allowed = true;
    break;
    }
    }
    }
    }
    return (allowed);
    }
    finally
    {
    jobDefWeb.Dispose();
    }
    }
  2. 在“EventReceiver1”文件中,将 ItemAdding 方法替换为以下代码。

    Public Overrides Sub ItemAdding(properties as SPItemEventProperties)
    Try
    Dim allowed As Boolean = True If properties.ListTitle = "Open Positions" Then
    allowed = CheckItem(properties)
    End If If allowed = False Then
    properties.Status = SPEventReceiverStatus.CancelWithError
    properties.ErrorMessage = _
    "The job you have entered is not defined in the Job Definitions List"
    properties.Cancel = True
    End If Catch ex As Exception
    properties.Status = SPEventReceiverStatus.CancelWithError
    properties.ErrorMessage = ex.Message
    properties.Cancel = True
    End Try
    End Sub
    public override void ItemAdding(SPItemEventProperties properties)
    {
    try
    {
    bool allowed = true; if (properties.ListTitle == "Open Positions")
    {
    allowed = checkItem(properties);
    } if (!allowed)
    {
    properties.Status = SPEventReceiverStatus.CancelWithError;
    properties.ErrorMessage =
    "The job you have entered is not defined in the Job Definitions List";
    properties.Cancel = true;
    }
    }
    catch (Exception ex)
    {
    properties.Status = SPEventReceiverStatus.CancelWithError;
    properties.ErrorMessage = ex.Message;
    properties.Cancel = true;
    }
    }
  3. 在“EventReceiver1”文件中,将 ItemUpdating 方法替换为以下代码。

    Public Overrides Sub ItemUpdating(properties as SPItemEventProperties)
    Try
    Dim allowed As Boolean = True If properties.ListTitle = "Open Positions" Then
    allowed = CheckItem(properties)
    End If If allowed = False Then
    properties.Status = SPEventReceiverStatus.CancelWithError
    properties.ErrorMessage = _
    "The job you have entered is not defined in the Job Definitions List"
    properties.Cancel = True
    End If Catch ex As Exception
    properties.Status = SPEventReceiverStatus.CancelWithError
    properties.ErrorMessage = ex.Message
    properties.Cancel = True End Try
    End Sub
    public override void ItemUpdating(SPItemEventProperties properties)
    { bool allowed = true; if (properties.ListTitle == "Open Positions")
    {
    allowed = checkItem(properties);
    } try
    { if (!allowed)
    {
    properties.Status = SPEventReceiverStatus.CancelWithError;
    properties.ErrorMessage =
    "The job you have entered is not defined in the Job Definitions List";
    properties.Cancel = true;
    }
    }
    catch (Exception ex)
    {
    properties.Status = SPEventReceiverStatus.CancelWithError;
    properties.ErrorMessage = ex.Message;
    properties.Cancel = true;
    }
    }

部署项目

  1. 在解决方案资源管理器中,右键单击该项目,然后单击“部署”

  2. 在 SharePoint 网站的“Open Positions”列表中,单击“添加新项”。

  3. 在“Title”字段中,为未包含在安全子网站中的“Job Definitions”列表中的工作说明提供职务。

  4. 单击“保存”。您将收到一条来自事件接收器的消息。

  5. 在“Title”字段中,为包含在安全子网站中的“Job Definitions”列表中的工作说明提供职务。

  6. 单击“保存”。这将创建此职位。

  • 此解决方案将重写 ItemAddingItemUpdating 方法,并验证正在将项目添加到的列表是否为“Open Positions”列表。如果是,则调用 CheckItem 方法,以传入与此事件关联的属性。
  • CheckItem 方法中,提升权限以确保成功访问安全子网站。将批准的列表中的职务和此事件所关联的 properties.AfterProperties 属性的职务进行比较。如果任何职务匹配,则将 allowedBoolean 变量设置为 true,并且此方法将返回。
  • 根据 allowed 变量的值,调用方法将允许此事件或设置 properties.ErrorMessage 属性,然后使用 properties.cancel 取消此事件。

在 Visual Studio 2010 中创建 SharePoint 2010 事件接收器的更多相关文章

  1. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]

    写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...

  2. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目

    注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...

  3. 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service

    在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单.如下: 第一步:创建一个“ASP.Net Empty Web Application”项目 创建 ...

  4. 在Visual Studio 2015 中添加SharePoint 2016 开发模板

    前言 SharePoint 2016已经发布很久了,然而,默认安装VS2015以后,却没有SharePoint 2016的开发模板.其实问题很简单,和VS2012开发SharePoint 2013一样 ...

  5. Visual Studio 2015中创建C#的Android项目提示"Value cannot be null"的解决方法

    由于之前本机已安装过Android SDK,在安装Visual Studio 2015时跳过了,并没有为Xamarin指定对应路径导致.Visual Studio顶部菜单:Tools > Opt ...

  6. 简单记录在Visual Studio 2013中创建ASP.NET Web API 2

    在很多跨平台的应用中就需要Web API ,比如android与数据库的交互. Create a Web API Project 选择新建项目下的模板下的Visual C#节点下的Web节点,在模板列 ...

  7. 在Visual Studio 2012中使用VMSDK开发领域特定语言(二)

    本文为<在Visual Studio 2012中使用VMSDK开发领域特定语言>专题文章的第二部分,在这部分内容中,将以实际应用为例,介绍开发DSL的主要步骤,包括设计.定制.调试.发布以 ...

  8. Visual Studio 2013 无法创建MVC项目,系统找不到指定的文件.(Exception from HRESULT:08x0070002)

    在Visual Studio 2013中创建新MVC项目,(PS:现在创建个MVC项目,差点都找不到在哪,汗!-) 确定后提示,系统找不到指定的文件.(Exception from HRESULT:0 ...

  9. Visual Studio 2010中创建ASP.Net Web Service

    转自:http://blog.csdn.net/xinyaping/article/details/7331375 很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net ...

随机推荐

  1. Spring.net 学习IOC------通过构造器注入

    别的不多说,咱们先上代码 1> object.xml 的文件内容 <?xml version="1.0" encoding="utf-8" ?> ...

  2. css display属性介绍

    none此元素不会被显示. block此元素将显示为块级元素,此元素前后会带有换行符. inline默认.此元素会被显示为内联元素,元素前后没有换行符. inline-block行内块元素.(CSS2 ...

  3. HDU 5861 Road

    首先要计算出每一条路最早开始的那一天,然后最晚结束的那一天. 这些天之间这条边都必须$open$,然后就变成一个线段树区间$+val$的问题了,最后询问一个每个点的$val$是多少. 注意:数据中有$ ...

  4. uva 12356 Army Buddies

    简单的并查集应用. #include<stdio.h> #include<string.h> #include<math.h> #include<algori ...

  5. 全球互联网技术大会GITC 2016 最炫酷技术盛宴

    2016年对于全球互联网产业来说,可谓是不折不扣的"创新爆发年",科技创新的更迭速度和多元化趋势都呈现出全所未见的增长态势.我们看到,云计算.大数据等在多年前萌发的技术创新正在快速 ...

  6. html 超链接(a)详细讲解

    a:link : http://www.cnblogs.com/yangfeng/archive/2009/07/25/1530962.html 超级链接 超级链接是网站中使用比较频繁的HTML元素, ...

  7. s=a+aa+aaa+aaaa+aa...aaaa

    main(){ int a,n,count=1; long int sn=0,tn=0; cout<<"input a and n:"; cin>>a> ...

  8. stack(STL)

    //Stack STL //在STL中,栈是以别的容器作为底部结构,再将 //接口改变,使之符合栈的特性 //一共5个常用操作函数 //构造析构 stack<Elem>c; //build ...

  9. Java 4

    1.继承的问题 子类是父类的一个扩展,子类可以利用父类的属性与行为,这种情况子类会破坏父类的封装 为了保持父类良好的封装性,设计父类有以下规则: 如果要把某类设计为最终类则需要添加final修饰符,或 ...

  10. IOS 类似网易新闻客户端内容滚动菜单跟随居中组件

    需求分析: 1.类似网易新闻客户端页面滚动组件.菜单栏对应菜单项一直居中 2.点击菜单栏可以切换到对应的page 3.滑动页面可以自动切换相应的菜单.并且对应的菜单栏居中显示 4.初始化时可以自定义菜 ...