0

I have the following code inside my remote event receiver:-

  public void ProcessOneWayEvent(SPRemoteEventProperties properties)
         {
             int listItemID = properties.ItemEventProperties.ListItemId;
             var listname = properties.ItemEventProperties.ListTitle;
             string siteUrl = properties.ItemEventProperties.WebUrl;
             using (ClientContext context = Helpers.GetAppOnlyContext(siteUrl))
             {

and the Helpers.GetAppOnlyContext is :-

         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();
         }
     }

now when i add a new item >> the remote event receiver will get fired, but i will get this error :-

 The remote server returned an error: (401) Unauthorized. 

205677-image.png

so can anyone advice on this ? Although i have generated the ClientID & ClientSecret using the sharepoint app registration page @ _layouts/15/AppRegNew.aspx and i added the following permision inside the AppInv.aspx page:-

 <AppPermissionRequests AllowAppOnlyPolicy="true">
   <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
 </AppPermissionRequests>

1 Answer 1

1

For new tenants, apps using an ACS app-only access token is disabled by default.

Granting access using SharePoint App-Only

Try using the Azure AD app-only model instead.

Granting access via Azure AD App-Only

2
  • so can we enable it ?Thanks
    – John John
    May 26, 2022 at 10:09
  • You can but Microsoft recommend using the Azure AD app-only model instead which is modern and more secure. If this answered your question, please make sure to mark it as the answer to help the community. May 26, 2022 at 10:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.