2

Having recently moved our intranet from SharePoint on-premises to SharePoint Online, I'm now trying to get a bunch of C# Console Apps (.NET Framework) to work with the new version of the sites. I believe I can use the PnP Framework to do this but I'm struggling to connect to SharePoint Online successfully. I've set up an App Registration in Azure and I'm using the ClientID and ClientSecret in my code, which is as follows:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PnP.Framework;

namespace SPOTest3_pnp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = @"https://mycompany.sharepoint.com/sites/mysite"; 
            string clientId = "12345678-1234-1234-1234-123456789012"; 
            string clientSecret = "1234567890123456789012345678901234567890"; 
            using (var clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientId, clientSecret))
            {
                clientContext.Load(clientContext.Web, p => p.Title);
                clientContext.ExecuteQuery();
                Console.WriteLine(clientContext.Web.Title);
            };
        }
    }
}

The call to ExecuteQuery() however return this error:

Microsoft.SharePoint.Client.ClientRequestException: 'Cannot contact site at the specified URL https://mycompany.sharepoint.com/sites/mysite. The app principal does not exist.'

I can't find anything useful to explain what that error refers to in this case or how to fix it. Any help would be gratefully received.

6
  • Did you register your client id in SharePoint site and provide required permissions? Jun 30 at 9:35
  • Hi Dikesh, thanks for your reply. I did that in portal.azure.com App Registrations. Is that what you mean?
    – bob777
    Jun 30 at 11:00
  • Apart from this, it requires to give permissions to your app registration. Please review this reference link: piyushksingh.com/2018/12/26/register-app-in-sharepoint Jun 30 at 11:06
  • Hi Dikesh, brilliant! That worked perfectly. Thank you so much for your help.
    – bob777
    Jun 30 at 13:47
  • 1
    Accepted as answer - thanks again, Dikesh.
    – bob777
    Jul 6 at 8:49

1 Answer 1

3

The error suggests that register add-in is missing in SharePoint site. Please follow below steps to register the add-in in SharePoint site.

Register Add-In using the AppRegNew form

  • Navigate to [Site Collection URL]/_layouts/15/AppRegNew.aspx enter image description here
  • At this point, you’ll need to fill in the following details enter image description here
  • Once you have filled in all the details, hit Create to register the add-in. enter image description here
  • You will be navigated to a page displaying the details of your add-in. Save Client Id & Client Secret for future references. enter image description here

Grant permission to an add-in

Now that the identifier is created, we need to grant the necessary permission for it to perform any action. In this example, I’ll demonstrate how to grant full control access on a Site Collection.

  • Navigate to [Site Collection URL]/_layouts/15/AppInv.aspx enter image description here

  • Enter your Client Id value in the App Id option and hit the Lookup button. Details of your add-in will be displayed automatically. enter image description here

  • This is the crucial part. In the option, Permission Request XML, let’s write the below XML code to grant our add-in full control on the given site collection. enter image description here

  • Just hit the Create button. You will now be prompted to trust the add-in for all the permissions that it requires. enter image description here

  • Hit the Trust It to grant the requested accesses. You can navigate to the following URL to also confirm that the permission has been assigned to the add-in. [Site Collection URL]/_layouts/15/appprincipals.aspx enter image description here

Reference link: https://piyushksingh.com/2018/12/26/register-app-in-sharepoint/

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.

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