|
| 1 | +# SharePoint site theming: CSOM development |
| 2 | + |
| 3 | +The SharePoint Client Side Object Model (CSOM) provides .NET developers with access to the SharePoint object model from code that is running locally or on a different server than SharePoint. This topic covers the site theming functionality available in CSOM. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | +This topic assumes you are familiar with the topics [Using the Client Object Model](https://msdn.microsoft.com/en-us/library/ff798388.aspx) and [Common Programming Tasks in the Managed Client Object Model](https://msdn.microsoft.com/en-us/library/ee537013.aspx), which cover how to work with the SharePoint CSOM. |
| 7 | + |
| 8 | +Install the following components for CSOM development: |
| 9 | + |
| 10 | +* [SharePoint Server 2016 Client Component SDK](https://www.microsoft.com/en-us/download/details.aspx?id=51679) |
| 11 | +* [Microsoft.SharePointOnline.CSOM](https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/) NuGet package (version 16.1.6906.1200 or later) |
| 12 | + |
| 13 | +## Sample code |
| 14 | + |
| 15 | +The following sample shows how to create a __Microsoft.Online.SharePoint.TenantAdministration.Tenant__ object and call the __GetAllTenantThemes__ method to return a list of themes. Notes on this sample: |
| 16 | + |
| 17 | +* The URL used for creating the context object includes the _-admin_ suffix, because TenantAdministration methods work with the admin site. |
| 18 | +* Create a __Tenant__ instance with the [Tenant constructor](https://msdn.microsoft.com/en-us/library/dn174852.aspx), and then call the methods on that instance. |
| 19 | +* The same approach can be used for calling other theme management methods. |
| 20 | + |
| 21 | +```C# |
| 22 | +using System.Security; |
| 23 | +using SPClient=Microsoft.SharePoint.Client; |
| 24 | +using Microsoft.Online.SharePoint.TenantAdministration; |
| 25 | +using Microsoft.Online.SharePoint.TenantManagement; |
| 26 | + |
| 27 | +SPClient.ClientContext ctx = new SPClient.ClientContext("https://mysite-admin.sharepoint.com/"); |
| 28 | +var pwd = "mypassword"; |
| 29 | +var passWord = new SecureString(); |
| 30 | +foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c); |
| 31 | +ctx. Credentials = new SPClient. SharePointOnlineCredentials( "[email protected]", passWord); |
| 32 | +Tenant tenant = new Tenant(ctx); |
| 33 | +SPClient.ClientObjectList<ThemeProperties> themes = tenant.GetAllTenantThemes(); |
| 34 | +``` |
| 35 | + |
| 36 | +## Sample theme definition |
| 37 | + |
| 38 | +For methods that take a theme argument, the following code defines an __SPOTheme__ class that you can use to create custom themes. |
| 39 | + |
| 40 | +```C# |
| 41 | +/// <summary> |
| 42 | +/// Properties defining a theme in SharePoint Online. |
| 43 | +/// </summary> |
| 44 | +public class SPOTheme |
| 45 | +{ |
| 46 | + /// <summary> |
| 47 | + /// Specifies the name of the theme. This must uniquely identify the theme. |
| 48 | + /// </summary> |
| 49 | + public string Name |
| 50 | + { |
| 51 | + get; private set; |
| 52 | + } |
| 53 | + /// <summary> |
| 54 | + /// Specifies the palette of colors in the theme, as a dictionary of theme slot values. |
| 55 | + /// </summary> |
| 56 | + public IDictionary<String, String> Palette |
| 57 | + { |
| 58 | + get; private set; |
| 59 | + } |
| 60 | + /// <summary> |
| 61 | + /// Specifies whether the theme is inverted, with a dark background and a light foreground. |
| 62 | + /// </summary> |
| 63 | + public bool IsInverted |
| 64 | + { |
| 65 | + get; private set; |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Applying a theme |
| 71 | + |
| 72 | +The methods covered below are used to manage the available themes in your tenant. To _apply_ a theme to a particular SharePoint web site, use the [SPWeb.ApplyTheme](https://msdn.microsoft.com/en-us/library/office/jj251358.aspx) method of the [Microsoft.SharePoint.Client.Web](https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.web.aspx) class. |
| 73 | + |
| 74 | +## Methods/properties of the Microsoft.Online.SharePoint.TenantAdministration.Tenant class |
| 75 | + |
| 76 | +These methods can be used to customize a SharePoint tenant administration site's set of available themes. You can add a new custom theme, update an existing theme, or delete a theme, and you can retrieve a specific theme or all themes. You can also hide or restore the default themes that come with SharePoint. |
| 77 | + |
| 78 | +### AddTenantTheme public method |
| 79 | +Add a theme to the tenant. |
| 80 | + |
| 81 | +__Namespace:__ Microsoft.Online.SharePoint.TenantAdministration.Tenant<br/> |
| 82 | +__Parameters:__ string name, string themeJson<br/> |
| 83 | +__Return type:__ ClientResult<bool> |
| 84 | + |
| 85 | +### DeleteTenantTheme public method |
| 86 | +Delete a theme from the tenant. |
| 87 | + |
| 88 | +__Namespace:__ Microsoft.Online.SharePoint.TenantAdministration.Tenant<br/> |
| 89 | +__Parameters:__ string name<br/> |
| 90 | +__Return type:__ void |
| 91 | + |
| 92 | +### GetAllTenantThemes public method |
| 93 | +Retrieve all of the themes currently available in the tenant, including any custom themes that have been added. Default themes are only included if the __HideDefaultThemes__ property is __false__ (the default value). |
| 94 | + |
| 95 | +__Namespace:__ Microsoft.Online.SharePoint.TenantAdministration.Tenant<br/> |
| 96 | +__Parameters:__ none<br/> |
| 97 | +__Return type:__ ClientObjectList<ThemeProperties> |
| 98 | + |
| 99 | +### GetTenantTheme public method |
| 100 | +Retrieve a theme by name. |
| 101 | + |
| 102 | +__Namespace:__ Microsoft.Online.SharePoint.TenantAdministration.Tenant<br/> |
| 103 | +__Parameters:__ string name<br/> |
| 104 | +__Return type:__ ThemeProperties |
| 105 | + |
| 106 | +### HideDefaultThemes public property |
| 107 | +This property indicates whether the default themes ("out of the box" themes) are hidden in the theme picker user interface for modern pages. You might want to set this property to __true__ after defining custom themes, to allow only specific themes to be used. |
| 108 | + |
| 109 | +__Namespace:__ Microsoft.Online.SharePoint.TenantAdministration.Tenant<br/> |
| 110 | +__Type:__ Boolean |
| 111 | + |
| 112 | +### UpdateTenantTheme public method |
| 113 | +Update the settings for an existing theme. |
| 114 | + |
| 115 | +__Namespace:__ Microsoft.Online.SharePoint.TenantAdministration.Tenant<br/> |
| 116 | +__Parameters:__ string name, string themeJson<br/> |
| 117 | +__Return type:__ ClientResult<bool> |
| 118 | + |
| 119 | +## Methods of the Microsoft.Online.SharePoint.TenantManagement.Tenant class |
| 120 | + |
| 121 | +These methods can be used to manage themes in a multi-tenant environment. |
| 122 | + |
| 123 | +### AddTenantTheme public method |
| 124 | +Add a theme to the tenant. |
| 125 | + |
| 126 | +__Namespace:__ Microsoft.Online.SharePoint.TenantManagement.Tenant<br/> |
| 127 | +__Parameters:__ string name, string themeJson<br/> |
| 128 | +__Return type:__ ClientResult<bool> |
| 129 | + |
| 130 | +### GetAllTenantThemes public method |
| 131 | +Retrieve all of the themes currently available in the tenant, including any custom themes that have been added. Default themes are only included if the __HideDefaultThemes__ property is __false__ (the default value). |
| 132 | + |
| 133 | +__Namespace:__ Microsoft.Online.SharePoint.TenantManagement.Tenant<br/> |
| 134 | +__Parameters:__ none<br/> |
| 135 | +__Return type:__ ClientObjectList<ThemeProperties> |
| 136 | + |
| 137 | +### GetHideDefaultThemes public method |
| 138 | +Read the current setting for whether to hide default themes in the theme picker user interface. |
| 139 | + |
| 140 | +__Namespace:__ Microsoft.Online.SharePoint.TenantManagement.Tenant<br/> |
| 141 | +__Parameters:__ none<br/> |
| 142 | +__Return type:__ ClientResult<bool> |
| 143 | + |
| 144 | +### GetTenantTheme public method |
| 145 | +Retrieve a theme by name. |
| 146 | + |
| 147 | +__Namespace:__ Microsoft.Online.SharePoint.TenantManagement.Tenant<br/> |
| 148 | +__Parameters:__ string name<br/> |
| 149 | +__Return type:__ ThemeProperties |
| 150 | + |
| 151 | +### SetHideDefaultThemes public method |
| 152 | +Specify whether to hide default themes in the theme picker user interface. |
| 153 | + |
| 154 | +__Namespace:__ Microsoft.Online.SharePoint.TenantManagement.Tenant<br/> |
| 155 | +__Parameters:__ Boolean<br/> |
| 156 | +__Return type:__ void |
| 157 | + |
| 158 | +### UpdateTenantTheme public method |
| 159 | +Update the settings for an existing theme. |
| 160 | + |
| 161 | +__Namespace:__ Microsoft.Online.SharePoint.TenantManagement.Tenant<br/> |
| 162 | +__Parameters:__ string name, string themeJson<br/> |
| 163 | +__Return type:__ ClientResult<bool> |
| 164 | + |
| 165 | +## Additional resources |
| 166 | + |
| 167 | +* [SharePoint site theming overview](sharepoint-site-theming-overview.md) |
| 168 | +* [SharePoint site theming: JSON schema](sharepoint-site-theming-json-schema.md) |
| 169 | +* [SharePoint site theming: PowerShell commands](sharepoint-site-theming-powershell.md) |
| 170 | +* [SharePoint site theming: REST API](sharepoint-site-theming-rest-api.md) |
| 171 | +* [Using the Client Object Model](https://msdn.microsoft.com/en-us/library/ff798388.aspx) |
| 172 | +* [Common Programming Tasks in the Managed Client Object Model](https://msdn.microsoft.com/en-us/library/ee537013.aspx) |
0 commit comments