Can someone guide me through event receivers in SharePoint Apps?

Highlighted
Occasional Contributor

Hi everyone!

 

So i m trying to use event receiver for my SharePoint app and followed an example from Microsoft.

 

Here is the code :

 

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

namespace LeaveRequestWeb.Services
{
    public class RemoteEventReceiver1 : IRemoteEventService
    {
        /// <summary>
        /// Handles events that occur before an action occurs, such as when a user adds or deletes a list item.
        /// </summary>
        /// <param name="properties">Holds information about the remote event.</param>
        /// <returns>Holds information returned from the remote event.</returns>
        public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();

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

            return result;
        }

        /// <summary>
        /// Handles events that occur after an action occurs, such as after a user adds an item to a list or deletes an item from a list.
        /// </summary>
        /// <param name="properties">Holds information about the remote event.</param>
        public void ProcessOneWayEvent(SPRemoteEventProperties properties)
        {
            string firstName = Convert.ToString(properties.ItemEventProperties.AfterProperties["FirstName"]);
            int listItemId = properties.ItemEventProperties.ListItemId;
            Guid listId = properties.ItemEventProperties.ListId;
            using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
            {
                if (clientContext != null)
                {
                    try
                    {
                        Web web = clientContext.Web;
                        List list = web.Lists.GetById(listId);
                        ListItem item = list.GetItemById(listItemId);
                        clientContext.Load(clientContext.Web);
                        clientContext.ExecuteQuery();

                        item["FirstNamePhonetic"] = "olan Phonetic";
                        item.Update();
                        clientContext.ExecuteQuery();
                    }
                    catch (Exception oops)
                    {
                        System.Diagnostics.Trace.WriteLine(oops.Message);
                    }
                }
            }
        }
    }
}

What it does basically is just change the First Name Phonetic field when an item is added but it doesn't work. Any ideas?

 

Also how can i debug this?

Even though i added the connection string of my azure namespace , i can't seem to manage it to debug ! I have tried everything but still get tthis message :

 

Cannot register Services/RemoteEventReceiver1.svc on Microsoft Azure Service Bus: The remote server returned an error: (500) Internal Server Error.

0 Replies