This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
AppEventReceiver.svc.cs
103 lines (83 loc) · 3.13 KB
/
AppEventReceiver.svc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.EventReceivers;
using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace Contoso.Core.EventReceiversWeb.Services
{
public class AppEventReceiver : IRemoteEventService
{
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
SPRemoteEventResult result = new SPRemoteEventResult();
switch (properties.EventType)
{
case SPRemoteEventType.AppInstalled:
HandleAppInstalled(properties);
break;
case SPRemoteEventType.AppUninstalling:
HandleAppUninstalling(properties);
break;
case SPRemoteEventType.ItemAdded:
HandleItemAdded(properties);
break;
}
return result;
}
public void ProcessOneWayEvent(SPRemoteEventProperties properties)
{
}
/// <summary>
/// Handles when an app is installed. Activates a feature in the
/// host web. The feature is not required.
/// Next, if the Jobs list is
/// not present, creates it. Finally it attaches a remote event
/// receiver to the list.
/// </summary>
/// <param name="properties"></param>
private void HandleAppInstalled(SPRemoteEventProperties properties)
{
using (ClientContext clientContext =
TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
new RemoteEventReceiverManager().AssociateRemoteEventsToHostWeb(clientContext);
}
}
}
/// <summary>
/// Removes the remote event receiver from the list and
/// adds a new item to the list.
/// </summary>
/// <param name="properties"></param>
private void HandleAppUninstalling(SPRemoteEventProperties properties)
{
using (ClientContext clientContext =
TokenHelper.CreateAppEventClientContext(properties, false))
{
if (clientContext != null)
{
new RemoteEventReceiverManager().RemoveEventReceiversFromHostWeb(clientContext);
}
}
}
/// <summary>
/// Handles the ItemAdded event by modifying the Description
/// field of the item.
/// </summary>
/// <param name="properties"></param>
private void HandleItemAdded(SPRemoteEventProperties properties)
{
using (ClientContext clientContext =
TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
if (clientContext != null)
{
new RemoteEventReceiverManager().ItemAddedToListEventHandler(clientContext, properties.ItemEventProperties.ListId, properties.ItemEventProperties.ListItemId);
}
}
}
}
}