locked
How to remove eventreceiver when handling AppUninstalling in a RER without getting access denied RRS feed

  • Question

  • I am trying to remove an eventreceiver from the clientContext.Web.EventReceivers

    by using DeleteObject() but I am getting Access Denied exception

    In my appmanifest.xml I already granted full control to the app

    Within my AppEventReceiver I have:

    if (properties.EventType == SPRemoteEventType.AppUninstalling)

    {

                    SharePointContextToken contextToken = TokenHelper.ReadAndValidateContextToken(properties.ContextToken, OperationContext.Current.IncomingMessageHeaders.To.Host);
                    string appOnlyAccessToken = TokenHelper.GetAppOnlyAccessToken(contextToken.TargetPrincipalName, properties.AppEventProperties.HostWebFullUrl.Authority, contextToken.Realm).AccessToken;

                    //string appOnlyAccessToken = TokenHelper.GetAppOnlyAccessToken(SHAREPOINT_PID, properties.AppEventProperties.HostWebFullUrl.Authority, TokenHelper.GetRealmFromTargetUrl(properties.AppEventProperties.HostWebFullUrl)).AccessToken;
                    using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(properties.AppEventProperties.HostWebFullUrl.ToString(), appOnlyAccessToken))
                    {
                        try
                        {
                            clientContext.Load(clientContext.Web);
                            clientContext.Load(clientContext.Web.EventReceivers);
                            clientContext.Load(clientContext.Web, web => web.Title);
                            clientContext.Load(clientContext.Web, web => web.CurrentUser.LoginName);
                            clientContext.ExecuteQuery();

                            //IEnumerable<EventReceiverDefinition> receivers = clientContext.LoadQuery(clientContext.Web.EventReceivers.Where(e => e.ReceiverName == "GroupUserAddedRER"));
                            //clientContext.ExecuteQuery();
                            EventReceiverDefinitionCollection receivers = clientContext.Web.EventReceivers;

                            List<EventReceiverDefinition> receiversToDelete = new List<EventReceiverDefinition>();
                            foreach (var rec in receivers)
                            {
                                if (rec.ReceiverName == "GroupUserAddedRER")
                                    receiversToDelete.Add(rec);
                            }

                            foreach (var recToDelete in receiversToDelete)
                            {
                                recToDelete.DeleteObject();
                            }
                            clientContext.ExecuteQuery();
                        }
                        catch (Exception)
                        {
                            result.Status = SPRemoteEventServiceStatus.CancelNoError;
                        }
                    }

    }


    Wednesday, June 12, 2013 11:27 PM

Answers

  • I think this has to do with siteloading and developer sites.

    This works when you deploy your app to a non-dev site. I created a list and I logged the count of the remote event receivers before installing the app and after uninstalling it and it works!

    • Marked as answer by RicardoAleG Tuesday, April 14, 2015 7:06 PM
    Tuesday, April 14, 2015 7:06 PM

All replies

  • I'm also having this issue. My code is similar to yours but I'm using the following to initiate my context:

    using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
    I can iterate the list of eventreceivers and read the details so I know that I'm accessing the right section.

    Saturday, July 13, 2013 9:46 PM
  • Same access denied message here on the clientContext.ExecuteQuery(); line.
    After the eventReceiver.DeleteObject()

    did you guys figure out a solution to avoid the access denied message?

    Tuesday, July 16, 2013 1:02 PM
  • Unfortunatelly not yet. What I will try to do is to create a sandboxed solution and then putting it the solution gallery in my O365 site

    Tuesday, July 16, 2013 7:10 PM
  • Has anybody found a solution yet?

    Unfortunatly I am facing the same Problem....

    Wednesday, July 24, 2013 8:06 AM
  • Hi,

    Check you manifest.xml

    You must have the property AllowAppOnlyPolicy to "true" like below, to use the "GetAppOnlyAccessToken" method

    <AppPermissionRequestsAllowAppOnlyPolicy="true">

             Rights of your app...

    </AppPermissionRequests>

    Thursday, August 1, 2013 12:02 PM
  • Manifest already has that set in it. Every other command we execute returns successfully apart from this delete.
    Thursday, August 1, 2013 11:36 PM
  • I'm facing the same issue. Also totally by accident noticed something interesting - if I call DeleteObject method without ExecuteQuery, every other ExecuteQuery call after that fails with the "Access Denied" error. Something obviously is not right there, it's as if DeleteObject method puts everything into a different ClientContext in the background.

    A bug? Or has anyone resolve it?


    • Edited by elena.n Friday, October 25, 2013 5:20 AM
    Friday, October 25, 2013 5:20 AM
  • Same issue here.  Autohosted App trying to delete a custom web event receiver.  Any luck resolving?
    Friday, November 1, 2013 4:39 PM
  • Same issue here.

    Trying to delete the event receiver from console application, so it's not an issue with SP app permissions.

    I'm using a site collection administrator account.

    Wednesday, February 5, 2014 1:31 PM
  • As far as i have testet.

    Its only the app that installed the receiver that can remove it again.

    Works fine as long I add and remove from same app - but i have had those same problems when debugging - keeps adding on appinstalled - and then forgets to cleanup on remove and then it becomes impossible to remove them.

    But generally - when working with a deployed app - it will have rights to remove it again - on appuninstalling if it created it.

    Monday, February 10, 2014 5:25 PM
  • here the solution... http://www.rlvision.com/blog/working-with-the-appinstalled-and-appuninstalling-events-in-sharepoint-apps/#comment-10713

    Tuesday, December 2, 2014 12:05 PM
  • I think this has to do with siteloading and developer sites.

    This works when you deploy your app to a non-dev site. I created a list and I logged the count of the remote event receivers before installing the app and after uninstalling it and it works!

    • Marked as answer by RicardoAleG Tuesday, April 14, 2015 7:06 PM
    Tuesday, April 14, 2015 7:06 PM