Jul 04 2017 12:43 AM
I have an Addin the creates a couple of web scoped Event receivers. One to fire whenever a new list is created to add a required content type and one to fire whenever a new web is created to attach the event receivers to the newly created web.
This works, but the problem is if the Addin is removed and I want to delete the event receivers. The top level ones get deleted, but I can't figure out how to loop through and remove the event receivers from the sub-webs.
Here is my code for handling the app uninstalling event
private void HandleAppUninstalling(SPRemoteEventProperties properties) { using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false)) { if (clientContext != null) { try { System.Diagnostics.Trace.WriteLine("Removing event receivers from top level"); new RemoteEventReceiverManager().RemoveEventReceiversFromHostWeb(clientContext, LISTADDEDRECEIVER_NAME); new RemoteEventReceiverManager().RemoveEventReceiversFromHostWeb(clientContext, WEBADDEDRECEIVER_NAME); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } } } }
and the code for the method that removes the event receivers
internal void RemoveEventReceiversFromHostWeb(ClientContext clientContext, string receiverName) { if (clientContext != null) { clientContext.Load(clientContext.Web.EventReceivers); clientContext.ExecuteQuery(); var rer = clientContext.Web.EventReceivers.Where(e => e.ReceiverName == receiverName).FirstOrDefault(); try { System.Diagnostics.Trace.WriteLine("Removing " + receiverName + " at " + rer.ReceiverUrl); rer.DeleteObject(); clientContext.ExecuteQuery(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } } }
I've had some success looping through the subwebs, but can't figure out how to pass the right client context and access token through. Once the AddIn has been removed I can't delete the event receiver and it pretty much makes the site screwed.
Jul 07 2017 05:11 PM