Object reference not set to an instance of an object error in OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext

Suresh S 96 Reputation points
2021-12-27T10:51:21.003+00:00

Hi All,

I am using the App Only feature to authenticate the SharePoint O365. I am using the below line of code.

SharePoint O365 App only OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext({siteURL},{Client_ID},{Client_Secret_ID}).

I have multiple site collection and created different Client_ID and Client_Secret_ID for each site collection. At first,It was working fine but I am getting "Object reference not set to an instance of an object" error created more than one Client_ID and Client_Secret_ID.

Please suggest me. I am not using Azure. The requirement is to authenticate the SharePoint O365 using the App only feature.

Thanks & Reagards
S. Sankar

Microsoft 365 and Office SharePoint Server For business
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. sadomovalex 3,636 Reputation points
    2021-12-27T14:56:34.273+00:00

    do you have stack trace of the error? You may check source code of https://github.com/pnp/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/AuthenticationManager.cs > GetAppOnlyAuthenticatedContext method and check why error can be thrown.

    Also as an option instead of adding reference to OfficeDevPnP.Core.dll you may download source code of the library from github, add it as project to your solution (add .csproj) and debug the code in Visual Studio.

    Alternatively since OfficeDevPnP.Core is deprecated nowdays you may try to switch to latest PnP.Framework and try to run your code with it.

    0 comments No comments

  2. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-12-28T06:39:23.06+00:00

    Hi @Suresh S ,
    I have tested following steps and it runs well.
    In a first step, you add the SharePoint PnP Sites Core library nuget package: https://www.nuget.org/packages/SharePointPnPCoreOnline. Once that’s done you can use below code construct:

    string siteUrl = "https://contoso.sharepoint.com/sites/demo";  
    using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))  
    {  
        cc.Load(cc.Web, p => p.Title);  
        cc.ExecuteQuery();  
        Console.WriteLine(cc.Web.Title);  
    };  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.