Skip to content

Instantly share code, notes, and snippets.

@vman
Last active February 18, 2022 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vman/e0a8d506754287d4c6ca4d8a8cedf2d6 to your computer and use it in GitHub Desktop.
Save vman/e0a8d506754287d4c6ca4d8a8cedf2d6 to your computer and use it in GitHub Desktop.
Get SharePoint Online Tenant Properties using CSOM
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
namespace TenantProps
{
class Program
{
static void Main(string[] args)
{
//This can be any site in the tenant
string siteUrl = "https://tenant.sharepoint.com/sites/intranet";
var clientContext = GetAppOnlyClientContext(siteUrl);
var web = clientContext.Web;
var tenantProperty = web.GetStorageEntity("mykey1");
clientContext.Load(tenantProperty);
clientContext.ExecuteQuery();
Console.WriteLine($"{tenantProperty.Value},{tenantProperty.Comment},{tenantProperty.Description}");
Console.ReadKey();
}
//Using App Only Client Context with SharePoint Authentication. You can use other ways to get the ClientContext as well.
private static ClientContext GetAppOnlyClientContext(string siteUrl)
{
//Part of OfficeDevPnP.Core
var authManager = new AuthenticationManager();
//https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
return authManager.GetAppOnlyAuthenticatedContext(siteUrl, "<client-id>", "<client-secret>");
}
}
}
@srinik16
Copy link

srinik16 commented Feb 18, 2022

Hi, what is the alternate for above. Currently we are getting 'The remote server returned an error: (403) Forbidden.'. We want get context using "client-id", "client-secret", siteurl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment