internal bool UpdateDocLib(ClientContext context, TaskDetail taskDetail)
        {
            bool retValue = true;

            try
            {
                Helper.AddLog("UpdateDocLib", "Getting record : Bamert AP Documents, ItemId : " + taskDetail.ItemId + ", AssignedTo : " + taskDetail.NewUserLoginId);
                List     list = context.Web.Lists.GetByTitle("Bamert AP Documents");
                ListItem itm  = list.GetItemById(Convert.ToInt32(taskDetail.ItemId));

                var newUserId = context.Web.EnsureUser(taskDetail.NewUserLoginId);
                Helper.AddLog("UpdateDocLib", "Loading Item + NewUserLgoinId");

                context.Load(list);
                context.Load(itm);
                context.Load(newUserId);
                context.ExecuteQuery();
                Helper.AddLog("UpdateDocLib", "Loaded Item + NewUserLgoinId");

                if (newUserId == null)
                {
                    Helper.AddLog("User object is null");
                    return(retValue = false);
                }

                Helper.AddLog("UpdateDocLib", "User object is not null");

                string approvers = Convert.ToString(itm["Approvers"]);
                Helper.AddLog("UpdateDocLib", "Old Approvers : " + approvers);
                Helper.AddLog("UpdateDocLib", "Replacing Approvers : " + taskDetail.OldUserName + " With " + newUserId.Title);
                approvers = approvers.Replace(taskDetail.OldUserName, newUserId.Title);
                Helper.AddLog("UpdateDocLib", "New Approvers : " + approvers);
                itm["Approvers"] = approvers;
                Helper.AddLog("UpdateDocLib", "Updating item ");
                itm.Update();
                context.ExecuteQuery();
                Helper.AddLog("UpdateDocLib", "Updated item ");
                retValue = true;
            }
            catch (Exception ex)
            {
                Helper.AddLog("UpdateDocLib-Error", ex.ToString());
                retValue = false;
            }
            return(retValue);
        }
        internal TaskDetail GetListItemDetails(ClientContext context, int itemId)
        {
            Helper.AddLog("GetListItemDetails", "Getting record");
            var      taskDetail = new TaskDetail();
            List     list       = context.Web.Lists.GetByTitle(LIST_TITLE);
            ListItem itm        = list.GetItemById(itemId);

            context.Load(list);
            context.Load(itm);
            context.ExecuteQuery();

            taskDetail.ListId      = Convert.ToString(itm["RelatedItems"]);
            taskDetail.RelatedItem = Convert.ToString(itm["RelatedItems"]);
            if (Convert.ToString(itm["AssignedTo"]).Length > 0)
            {
                var userDetail = (FieldUserValue)itm["AssignedTo"];
                taskDetail.OldUserId      = userDetail.LookupId.ToString();
                taskDetail.OldUserName    = userDetail.LookupValue;
                taskDetail.OldUserLoginId = userDetail.Email;
            }
            if (taskDetail.RelatedItem.Length > 32)
            {
                WFRelatedItemDetails[] json = JsonConvert.DeserializeObject <WFRelatedItemDetails[]>(taskDetail.RelatedItem);
                taskDetail.ItemId = Convert.ToString(json[0].ItemId);
                taskDetail.ListId = Convert.ToString(json[0].ListId);
                taskDetail.WebId  = Convert.ToString(json[0].WebId);

                Helper.AddLog("GetListItemDetails", "Related Item ItemId : " + taskDetail.ItemId);
                Helper.AddLog("GetListItemDetails", "Related Item ListId : " + taskDetail.ListId);
                Helper.AddLog("GetListItemDetails", "Related Item WebId : " + taskDetail.WebId);
            }
            Helper.AddLog("GetListItemDetails", "Related Item : " + taskDetail.RelatedItem);
            Helper.AddLog("GetListItemDetails", "AssignedTo Id : " + taskDetail.OldUserId);
            Helper.AddLog("GetListItemDetails", "AssignedTo Name : " + taskDetail.OldUserName);
            Helper.AddLog("GetListItemDetails", "AssignedTo Name : " + taskDetail.OldUserLoginId);
            //Do not execute the call.  We simply create the list in the context,
            //it's up to the caller to call ExecuteQuery.
            return(taskDetail);
        }
        public void ItemUpdatingToListEventHandler(ClientContext clientContext,
                                                   SPRemoteEventProperties properties, SPRemoteEventResult result)
        {
            try
            {
                Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "");
                var    relatedItem       = new TaskDetail();
                string beforeName        = "";
                string assignToFieldName = "AssignedTo";

                relatedItem = GetListItemDetails(clientContext, properties.ItemEventProperties.ListItemId);

                try
                {
                    if (properties.ItemEventProperties.BeforeProperties.ContainsKey(assignToFieldName))
                    {
                        beforeName = Convert.ToString(properties.ItemEventProperties.BeforeProperties[assignToFieldName]);
                        Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "Before :" + beforeName);
                    }
                    else
                    {
                        Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "No Before");
                    }
                }
                catch (Exception ex1)
                {
                    beforeName = "Before Name got Error : " + ex1.ToString();
                    Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", beforeName);
                }
                string afterName = "";
                try
                {
                    if (properties.ItemEventProperties.AfterProperties.ContainsKey(assignToFieldName))
                    {
                        afterName = Convert.ToString(properties.ItemEventProperties.AfterProperties[assignToFieldName]);
                        Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "After : " + afterName + ", After Variable Length should be > 5 " + afterName.Length.ToString());
                        if (afterName.Length > 5)
                        {
                            Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "Spliting now");
                            string[] splitAfterName = afterName.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                            Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "Splited array length : " + splitAfterName.Length.ToString());
                            if (splitAfterName.Length > 0)
                            {
                                Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "getting user login name");
                                string userName = splitAfterName[(splitAfterName.Length - 1)];
                                Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "got user login name : " + userName);
                                Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "getting user object");
                                var afterUserObj = clientContext.Web.EnsureUser(userName);
                                clientContext.Load(afterUserObj);
                                clientContext.ExecuteQuery();
                                Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "got user object & checking null");
                                if (afterUserObj != null)
                                {
                                    Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "user object is not null");
                                    relatedItem.NewUserId      = afterUserObj.Id.ToString();
                                    relatedItem.NewUserLoginId = afterUserObj.LoginName;
                                    relatedItem.NewUserName    = afterUserObj.Title;
                                    Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "user object info : Id=" + afterUserObj.Id.ToString() + ", LoginName=" + afterUserObj.LoginName + ", Name =" + afterUserObj.Title);
                                    UpdateDocLib(clientContext, relatedItem);
                                }
                            }
                        }
                        //var userName = (FieldUserValue)properties.ItemEventProperties.AfterProperties[assignToFieldName];
                        //Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "User Id : " + userName.LookupId);
                        //Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "User Name : " + userName.LookupValue);
                        //Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "User Email : " + userName.Email);
                        //relatedItem.NewUserId = userName.LookupId.ToString();
                        //relatedItem.NewUserName = userName.LookupValue;
                        //relatedItem.NewUserLoginId = userName.Email;
                    }
                    else
                    {
                        Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "No After");
                    }
                }
                catch (Exception ex2)
                {
                    afterName = "After Name got Error : " + ex2.ToString();
                    Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", afterName);
                }

                // only for demo we check here the Description
                var testList = clientContext.Web.Lists.GetByTitle("TestList");
                clientContext.Load(clientContext.Web);
                clientContext.Load(testList);
                clientContext.ExecuteQuery();
                ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
                var newItem = testList.AddItem(newItemInfo);
                newItem["Title"]       = DateTime.Now.ToString();
                newItem["TaskDetails"] = "Before : " + beforeName + ", After : " + afterName;
                newItem["DocDetails"]  = relatedItem.ListId;
                newItem.Update();
                clientContext.ExecuteQuery();

                result.Status = SPRemoteEventServiceStatus.Continue;
            }
            catch (Exception oops)
            {
                Helper.AddLog("RemoteEventReceiverManager-ItemUpdatingToListEventHandler", "error : " + oops.ToString());
                result.Status       = SPRemoteEventServiceStatus.CancelWithError;
                result.ErrorMessage = oops.Message;

                System.Diagnostics.Trace.WriteLine(oops.Message);
            }
        }