SharePoint 2013 app 实战之 RemoteEventReceiver

RemoteEventReceiver 是在SharePoint 2013 app中引入的一个新特性,它可以在SharePoint 内容触发某个事件(删除item,添加item,修改 item等)时调用一个远程的WCF 服务,在这个远程的WCF服务 的 publicSPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties) 方法中你可以对这个事件做进一步处理,最终返回一个 SPRemoteEventResult来标示是否继续改事件(比如可以返回 result.Status =SPRemoteEventServiceStatus.CancelWithError; 来终止事件)。

 

本文新建app的过程略过,不会的博友请参考:http://blog.csdn.net/farawayplace613/article/details/8677596。

下面说明下步骤:

1. 新建 RemoteEventReceiver

    a. 选中SharePoint app project, 右击 => New Item, 选择 RemoteEventReceiver,点击 Add

     

    b. 类型选择"List Item Events", 事件源选择 "Custom List",处理事件选择"An Item is being added","An Item is being deleted","An Item is being updated", 点击Finish

    SharePoint 2013 app 实战之 RemoteEventReceiver_第1张图片

    c. 完成后在SharePoint App web project中会多出一个 RemoteEventReceiver1.svc

    

 

2. 在SharePoint app project中添加一个List instance 用于测试

SharePoint 2013 app 实战之 RemoteEventReceiver_第2张图片

 

3. 更新RemoteEventReceiver1.svc.cs中的代码(若遇到delete item则,报错并终止删除事件)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;

namespace SharePointApp9Web
{
    public class RemoteEventReceiver1 : IRemoteEventService
    {
        public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();

            using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
            {
                if (clientContext != null)
                {
                    clientContext.Load(clientContext.Web);
                    clientContext.ExecuteQuery();
                }
            }

            if (properties.EventType == SPRemoteEventType.ItemDeleting)
            {                
                    result.ErrorMessage = "You can't delete!";
                    result.Status = SPRemoteEventServiceStatus.CancelWithError;                
            }        

            return result;
        }

        public void ProcessOneWayEvent(SPRemoteEventProperties properties)
        {
        }
    }
}

4. 调试 RemoteEventReceiver

   a.点击VS2012的start

   b. 选择trust 该app

   c. 打开 http://YourSharePointServer/YourAppName/Lists/List1 (注意替换YourSharePointServer,YourAppName)

   d. 添加一个 item

   e. 在ProcessEvent 方法中设置若干断点

   f. 尝试删除一个Item

    SharePoint 2013 app 实战之 RemoteEventReceiver_第3张图片

    
   5. 当尝试删除一个Item时效果为:

   SharePoint 2013 app 实战之 RemoteEventReceiver_第4张图片 

 

   6. 为App web之外的list 注册 RemoteEventReceiver

     注意,用以上方法添加的RemoteEventReceiver只能作用于App web 中的list(内容),如果要给App web之外的list(如Host web 中的list) 注册 RemoteEventReceiver就需要使用如下Client Ojbet代码:

// need add name space using System.Reflection;

EventReceiverDefinitionCreationInformation eventReceiver = new EventReceiverDefinitionCreationInformation();
                eventReceiver.EventType = EventReceiverType.ItemAdding;
                eventReceiver.ReceiverAssembly = Assembly.GetExecutingAssembly().FullName;
                eventReceiver.ReceiverClass = "HTools_EventReceiver";
                eventReceiver.ReceiverName = "HTools_EventReceiver";
                eventReceiver.ReceiverUrl = "http://hh-wks-robb:2145/HTools_EventReceiver.svc";
                eventReceiver.SequenceNumber = 1000;

                Web web = SPutil.getSP("http://sub.domain.com/sites/apps");
                List targetList = web.Lists.GetByTitle("Host Web List");
                targetList.EventReceivers.Add(eventReceiver);
                
                web.Context.ExecuteQuery();

 

   常见问题:

         a. 如果不能触发RemoteEventReceiver,请尝试为Host web 开启SSL :     http://blogs.msdn.com/b/fabdulwahab/archive/2013/01/21/configure-ssl-for-sharepoint-2013.aspx

         

        b. 如果遇到"Could not establish trust relationship with remote server",请于请求代码前加上:

           TokenHelper.TrustAllCertificates();

 

 

        

你可能感兴趣的:(debug,实战,SharePoint,2013,注册远程事件)