Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
Working with Remote Event Receivers in a Provider Hosted App - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2015-12-09

Hi All, I would like to share my experiences while working on provider-hosted App Remote Event Receiver. When I was working with Remote Event Receiver’s for the first time I faced a lot of challenges. For example, I was not able to find why my Remote Event Receiver was not triggering and after a lot of investigation, I found the cause of the problem. If you are new to provider-hosted App Remote Event Receiver’s this article will help you a lot.

1.  Right click on your HostWebProject add new Remote Event receiver – I am using here Item added event

2. Now as you can see your event receiver will be added to solution so once you will add event receiver one RemoteEventReciver1.svc file will be created in AppWeb Solution.

3. Once your RemoteEventReceiver has been added to solution, open your element.xml file update your  .svc Location URL

4. .svc file will be created in your AppWebProject

Now Write your code on RemoteEventReciver1.svc.cs

I have written my code on ProcessOnWayEvent that fires after an action occurs, such as after a user adds an item to a list (because I have chosen on ItemAdded)

Now your event receiver will be created and when you run “deploy” this will not work! I was facing exactly the same problem with my Event Receiver not triggering,  so the question is – how do we overcome from this issue? Okay, so lets move ahead.

Now, right-click on your HostWeb Project – double click on “Handle App Installed” (if you want to Perform something when your App Installed).

When “Handle App Installed” is “true” then it will generate a new AppEventReceiver.svc in the AppWebProject

Open AppEventReceiver.svc.cs file add below code

Now I have written my code on AppInstalled Event-  which will attached RemoteEventReceiver1 to My List

private void HandleAppInstalled(SPRemoteEventProperties properties)
        {
            try
            {
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    if (clientContext != null)
                    {
                        List olist = clientContext.Web.Lists.GetByTitle("TestCustom");
                        clientContext.Load(olist, o => o.EventReceivers);
                        clientContext.ExecuteQuery();
                        bool isReRExsist = false;
                        foreach (var receiver in olist.EventReceivers)
                        {
                            if (receiver.ReceiverName == "RemoteEventReceiver1")
                            {
                                isReRExsist = true;
                                break;
                            }
                        }
                        if (!isReRExsist)
                        {

                            string remoteUrl = "https://xyz.azurewebsites.net/Services/RemoteEventReceiver1.svc";
                            EventReceiverDefinitionCreationInformation eventReDefCreation = new EventReceiverDefinitionCreationInformation()
                            {

                                EventType = EventReceiverType.ItemAdded,
                                ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                                ReceiverName = "RemoteEventReceiver1",
                                ReceiverClass = "RemoteEventReceiver1",
                                ReceiverUrl = remoteUrl,
                                SequenceNumber = 15000
                            };
                            olist.EventReceivers.Add(eventReDefCreation);
                            clientContext.ExecuteQuery();

                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

        }

and detaching my EventReceiver from List when my app will uninstalled from site

private void HandleAppUnistalled(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
            {
                var list = clientContext.Web.Lists.GetByTitle("TestCustom");
                clientContext.Load(list);
                clientContext.ExecuteQuery();
                EventReceiverDefinitionCollection eventRColl = list.EventReceivers;
                clientContext.Load(eventRColl);
                clientContext.ExecuteQuery();
                List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
                foreach (EventReceiverDefinition erdef in eventRColl)
                {
                    if (erdef.ReceiverName == "RemoteEventReceiver1")
                    {
                        toDelete.Add(erdef);
                    }
                }

                //Delete the remote event receiver from the list, when the app gets uninstalled
                foreach (EventReceiverDefinition item in toDelete)
                {
                    item.DeleteObject();
                    clientContext.ExecuteQuery();
                }
            }
        }

Now deploy your code it will work fine….

I hope my article will help you guys pls. drop your comment if you will face any issue.

Happy Coding !!!

Thanks,

Pankaj Srivastava

(Visited 259 times, 1 visits today)

About the author 

Pankaj Srivastava

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.