class WebTemplateCollection
A collection class for managing SharePoint site templates (WebTemplate objects), providing methods to retrieve and work with multiple web templates.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webs/template_collection.py
6 - 23
moderate
Purpose
WebTemplateCollection serves as a container and manager for SharePoint web templates. It extends EntityCollection to provide specialized functionality for working with site templates, including retrieving templates by name. This class is used when interacting with SharePoint's template system to list available site templates or access specific templates for site creation or configuration.
Source Code
class WebTemplateCollection(EntityCollection[WebTemplate]):
"""Specifies a collection of site templates."""
def __init__(self, context, resource_path=None, parent=None):
super(WebTemplateCollection, self).__init__(
context, WebTemplate, resource_path, parent
)
def get_by_name(self, name):
"""Returns the SP.WebTemplate (section 3.2.5.151) specified by its name.
:param str name: The name of the WebTemplate that is returned.
"""
return WebTemplate(
self.context,
ServiceOperationPath(
"getByName", ["{name}".format(name=name)], self.resource_path
),
)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
EntityCollection[WebTemplate] | - |
Parameter Details
context: The client context object that provides the connection and authentication information for SharePoint operations. This is required for all API calls and is passed to parent EntityCollection.
resource_path: Optional. The URL path to the resource representing this collection in the SharePoint REST API. If not provided, it will be derived from the parent or context. Defaults to None.
parent: Optional. The parent entity that owns this collection. Used to establish the object hierarchy in the SharePoint object model. Defaults to None.
Return Value
Instantiation returns a WebTemplateCollection object that can be used to query and retrieve WebTemplate objects. The get_by_name method returns a single WebTemplate object corresponding to the specified template name.
Class Interface
Methods
__init__(self, context, resource_path=None, parent=None)
Purpose: Initializes a new WebTemplateCollection instance with the provided context and optional resource path and parent
Parameters:
context: The client context for SharePoint operationsresource_path: Optional URL path to the collection resourceparent: Optional parent entity in the object hierarchy
Returns: None (constructor)
get_by_name(self, name: str) -> WebTemplate
Purpose: Retrieves a specific WebTemplate from the collection by its name identifier
Parameters:
name: The name of the WebTemplate to retrieve (e.g., 'STS#0' for Team Site template)
Returns: A WebTemplate object representing the requested site template. The object needs to be loaded via context.load() and context.execute_query() to populate its properties.
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
context |
ClientContext | The SharePoint client context used for API operations, inherited from EntityCollection | instance |
resource_path |
ResourcePath | The URL path to this collection resource in the SharePoint REST API, inherited from EntityCollection | instance |
parent |
Entity | The parent entity that owns this collection in the SharePoint object model hierarchy, inherited from EntityCollection | instance |
Dependencies
office365.runtime.paths.service_operationoffice365.sharepoint.entity_collectionoffice365.sharepoint.webs.template
Required Imports
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.sharepoint.entity_collection import EntityCollection
from office365.sharepoint.webs.template import WebTemplate
Usage Example
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.webs.template_collection import WebTemplateCollection
# Assuming you have a configured context
ctx = ClientContext(site_url).with_credentials(credentials)
# Create a collection instance (typically obtained from a Web object)
templates = WebTemplateCollection(ctx, resource_path="_api/web/GetAvailableWebTemplates(lcid=1033)")
# Load the collection
ctx.load(templates)
ctx.execute_query()
# Iterate through templates
for template in templates:
print(template.name)
# Get a specific template by name
team_template = templates.get_by_name("STS#0")
ctx.load(team_template)
ctx.execute_query()
print(team_template.title)
Best Practices
- Always ensure the context object is properly authenticated before creating a WebTemplateCollection instance
- Use get_by_name() when you know the specific template name rather than loading the entire collection
- Call ctx.load() and ctx.execute_query() to actually fetch data from SharePoint after creating or retrieving templates
- Template names typically follow the format 'TemplateType#Configuration' (e.g., 'STS#0' for Team Site)
- The collection inherits from EntityCollection, so standard collection operations like iteration and indexing are supported
- Resource paths for template collections often include locale identifiers (lcid) to get templates for specific languages
- Handle potential exceptions when retrieving templates by name, as invalid names will cause API errors
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ListTemplateCollection 85.5% similar
-
class SPOTenantWebTemplateCollection 84.7% similar
-
class WebTemplate 78.4% similar
-
class WebCollection 74.9% similar
-
class WorkflowTemplate 70.8% similar