如何为列表添加事件创建远程事件接收器?

时间:2019-03-01 10:12:30

标签: sharepoint-online

我想将“添加项的远程事件接收器”附加到每个新列表,因此我想为“添加列表的事件”创建远程事件接收器。请帮忙。

1 个答案:

答案 0 :(得分:0)

添加了新库的示例代码供您参考。

private void AppInstalledMethod(SPRemoteEventProperties _properties)
        {
            using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(_properties, false))
            {
                if (clientContext != null)
                {
                    Web web = clientContext.Web;
                    clientContext.Load(web);
                    clientContext.Load(clientContext.Site);
                    clientContext.Load(clientContext.Site.EventReceivers);
                    clientContext.ExecuteQuery();
                    EventReceiverDefinitionCreationInformation receiver = new EventReceiverDefinitionCreationInformation();
                    receiver.EventType = EventReceiverType.ListAdded;
                    OperationContext op = OperationContext.Current;
                    Message msg = op.RequestContext.RequestMessage;
                    receiver.ReceiverUrl = msg.Headers.To.ToString();
                    receiver.ReceiverName = "EventForNewLibrary";
                    receiver.Synchronization = EventReceiverSynchronization.Synchronous;
                    receiver.SequenceNumber = 5000;
                    clientContext.Site.EventReceivers.Add(receiver);
                    clientContext.ExecuteQuery();
                }
            }
        }

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();

            switch (properties.EventType)
            {
                case SPRemoteEventType.AppInstalled:
                    AppInstalledMethod(properties);
                    break;
                case SPRemoteEventType.ListAdded:
                    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                    {
                        if (clientContext != null)
                        {
                            var listID=properties.ListEventProperties.ListId;
                            //to do
                        }
                    }
                    break;
                default:
                    break;
            }

            return result;
        }