Here is some quick code I have put together to work with SharePoint Online tenant properties using CSOM.
The important thing to note here is that when setting the properties, we can only use the context of an App Catalog site (either tenant level or site collection level)
When getting the properties, the context of any site can be used.
Set SharePoint Online Tenant Properties in the App Catalog using CSOM:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SharePoint.Client; | |
using OfficeDevPnP.Core; | |
namespace TenantProps | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Url of an app catalog. Either tenant level or site collection level | |
string appCatalogUrl = "https://tenant.sharepoint.com/sites/apps"; | |
var clientContext = GetAppOnlyClientContext(appCatalogUrl); | |
var web = clientContext.Web; | |
web.SetStorageEntity("mykey1", "value1", "description", "comments"); | |
clientContext.ExecuteQuery(); | |
} | |
//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>"); | |
} | |
} | |
} |
Get SharePoint Online Tenant Properties using CSOM:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | |
} | |
} | |
} |
(click to zoom)