如何为列表添加事件创建远程事件接收器?
How to create Remote Event Receiver for List Added event?
我想将 Item Added 远程事件接收器附加到每个新列表,因此我想为 List Added 事件创建远程事件接收器。请帮忙。
添加了新库的示例代码供您参考。
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;
}
我想将 Item Added 远程事件接收器附加到每个新列表,因此我想为 List Added 事件创建远程事件接收器。请帮忙。
添加了新库的示例代码供您参考。
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;
}