2

Last month the below steps were working well for me to debug and test a SharePoint online remote event receiver locally:-

  1. Open Ngrok.exe >> run the following command inside ngrok:-
 ngrok authtoken 3***e
 ngrok http --host-header=rewrite  57269
  1. register a new app:- @ https://.sharepoint.com/sites//_layouts/15/AppRegNew.aspx >> enter the above Ngrok urls inside the App Redirect URL & App Domain.

  2. Inside the _layouts/15/appinv.aspx >> I search for the above app using Client ID >> and enter the following:-

 <AppPermissionRequests AllowAppOnlyPolicy="true">
   <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
 </AppPermissionRequests>
  1. Update the service's web config with the above ClientID and ClientSecret

  2. Register the new remove event receiver as follow:-

 Add-PnPEventReceiver -List "Order Management" -Name "TasksRER" -Url http://cc6e945e82f6.ngrok.io/service1.svc -EventReceiverType ItemUpdated -Synchronization Asynchronous

But today when I tried the above steps it failed >> where inside my event receiver when I tried to get the SharePoint context >> I will get that the Context is null:-

  public void ProcessOneWayEvent(SPRemoteEventProperties properties)
         {
             var prop = properties;
             var listItemID = properties.ItemEventProperties.ListItemId;
             var listTitle = properties.ItemEventProperties.ListTitle;
             using (ClientContext context = Helpers.GetAppOnlyContext(properties.ItemEventProperties.WebUrl))
             {
                 context.Load(context.Web);
                 context.ExecuteQuery();

Here is a screen shot from Visual Studio with the error i am getting when trying to get the context:-

enter image description here

Any advice if anything has been changed which is preventing me from running the above steps? which were working well last month? Thanks

here is the code for the GetAppOnlyContext

     public class Helpers
         {
             public static ClientContext GetAppOnlyContext(string siteUrl)
             {
                 try
                 {
                     Uri siteUri = new Uri(siteUrl);
                     string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                     string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;
        
                     return TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken);
                 }
        
                 catch (Exception ex)
                 {
                     Trace.TraceInformation("GetAppOnlyContext failed. {0}", ex.Message);
                 }
                 return null;
             }
        
             public static ClientContext GetAuthenticatedContext(string siteUrl)
             {
                 string userName = WebConfigurationManager.AppSettings.Get("AuthenticatedUserName");
                 string password = WebConfigurationManager.AppSettings.Get("AuthenticatedUserPassword");
                 return GetAuthenticatedContext(siteUrl, userName, password);
             }
        
             public static ClientContext GetAuthenticatedContext(string siteUrl, string userName, SecureString password)
             {
                 ClientContext ctx = new ClientContext(siteUrl);
                 ctx.Credentials = new SharePointOnlineCredentials(userName, password);
                 return ctx;
             }
        
             public static ClientContext GetAuthenticatedContext(string siteUrl, string userName, string password)
             {
                 SecureString securePassword = GetPassword(password);
                 return GetAuthenticatedContext(siteUrl, userName, securePassword);
             }
        
             private static SecureString GetPassword(string passwd)
             {
                 var secure = new SecureString();
                 foreach (char c in passwd)
                 {
                     secure.AppendChar(c);
                 }
                 return secure;
             }
        
             public static string EmptyIfNull(object obj)
             {
                 return obj == null ? "" : obj.ToString();
             }
         }
     }  

seem this statement string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken; inside my Helpers.GetAppOnlyContext will raise this error token request failed, as follow:-

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.