🔍 Code Extractor

class ListTemplate

Maturity: 59

Represents a SharePoint list definition or list template that defines the fields and views for a list, providing access to list schema and metadata.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/lists/template.py
Lines:
7 - 43
Complexity:
moderate

Purpose

This class encapsulates SharePoint list templates and definitions, which are either stored in the SharePoint server's TEMPLATE\FEATURES directory or created through the UI/object model when saving a list as a template. It provides methods to retrieve the global schema XML and access template properties like internal name. The class is part of the Office365 SharePoint client library and inherits from Entity, providing standard entity operations for interacting with SharePoint list templates.

Source Code

class ListTemplate(Entity):
    """
    Represents a list definition or a list template, which defines the fields and views for a list.
    List definitions are contained in files within
    \\Program Files\\Common Files\\Microsoft Shared\\Web Server Extensions\\12\\TEMPLATE\\FEATURES,
    but list templates are created through the user interface or through the object model when a list is
    saved as a template.
    Use the Web.ListTemplates property (section 3.2.5.143.1.2.13) to return a ListTemplateCollection
    (section 3.2.5.92) for a site collection. Use an indexer to return a single list definition or
    list template from the collection.
    """

    def get_global_schema_xml(self):
        """Retrieves the global schema.xml file."""
        return_type = ClientResult(self.context)
        qry = ServiceOperationQuery(
            self, "GetGlobalSchemaXml", None, None, None, return_type
        )
        self.context.add_query(qry)
        return return_type

    @property
    def internal_name(self):
        """Gets a value that specifies the identifier for the list template.

        :rtype: str or None
        """
        return self.properties.get("InternalName", None)

    def set_property(self, name, value, persist_changes=True):
        super(ListTemplate, self).set_property(name, value, persist_changes)
        if self._resource_path is None:
            if name == "Name":
                self._resource_path = ServiceOperationPath(
                    "GetByName", [value], self._parent_collection.resource_path
                )
        return self

Parameters

Name Type Default Kind
bases Entity -

Parameter Details

__init__: Inherits constructor from Entity base class. The constructor typically accepts a context parameter (ClientContext) and optionally a resource_path parameter to identify the specific list template resource in SharePoint.

Return Value

Instantiation returns a ListTemplate object that represents a SharePoint list template. The get_global_schema_xml() method returns a ClientResult object containing the global schema XML as a string. The internal_name property returns a string identifier for the list template or None if not set. The set_property() method returns self for method chaining.

Class Interface

Methods

get_global_schema_xml() -> ClientResult

Purpose: Retrieves the global schema.xml file for the list template

Returns: ClientResult object that will contain the schema XML string after context.execute_query() is called

@property internal_name() -> str | None property

Purpose: Gets the identifier for the list template

Returns: String identifier for the list template, or None if not set or not loaded

set_property(name: str, value: Any, persist_changes: bool = True) -> ListTemplate

Purpose: Sets a property value on the list template and optionally updates the resource path

Parameters:

  • name: The name of the property to set (e.g., 'Name')
  • value: The value to assign to the property
  • persist_changes: Whether to persist the changes immediately (default True)

Returns: Returns self for method chaining

Attributes

Name Type Description Scope
context ClientContext The SharePoint client context used for executing queries and operations (inherited from Entity) instance
properties dict Dictionary containing the list template properties loaded from SharePoint (inherited from Entity) instance
_resource_path ServiceOperationPath | None The resource path identifying this list template in SharePoint, automatically set when Name property is assigned (inherited from Entity) instance
_parent_collection ListTemplateCollection Reference to the parent collection that contains this list template (inherited from Entity) instance

Dependencies

  • office365.runtime.client_result
  • office365.runtime.paths.service_operation
  • office365.runtime.queries.service_operation
  • office365.sharepoint.entity

Required Imports

from office365.runtime.client_result import ClientResult
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity import Entity

Usage Example

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.listitems.listitem import ListTemplate

# Authenticate and get context
ctx = ClientContext(site_url).with_credentials(credentials)

# Get list templates collection
list_templates = ctx.web.list_templates
ctx.load(list_templates)
ctx.execute_query()

# Access a specific template
template = list_templates.get_by_name('Custom List')
ctx.load(template)
ctx.execute_query()

# Get internal name
print(template.internal_name)

# Retrieve global schema XML
schema_result = template.get_global_schema_xml()
ctx.execute_query()
print(schema_result.value)

# Set a property
template.set_property('Name', 'MyTemplate')
ctx.execute_query()

Best Practices

  • Always call ctx.execute_query() after operations to commit changes or retrieve data from SharePoint
  • Use ctx.load() before accessing properties to ensure they are populated from the server
  • Access list templates through Web.ListTemplates property rather than instantiating directly
  • The internal_name property may return None if not loaded; always check for None before using
  • When using set_property(), the resource_path is automatically set when the 'Name' property is assigned
  • The get_global_schema_xml() method returns a ClientResult that must be executed via context.execute_query() before accessing the value
  • List templates are read-only in most cases; modifications should be done through proper SharePoint APIs
  • Ensure proper authentication and permissions to access list templates in the SharePoint site

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class ListTemplateCollection 77.9% similar

    A collection class that manages and provides access to SharePoint list templates, extending EntityCollection to handle multiple ListTemplate instances.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/lists/template_collection.py
  • class WebTemplate 73.6% similar

    Represents a SharePoint site definition or site template that is used to instantiate a site, providing access to template metadata and configuration properties.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webs/template.py
  • class WebTemplateCollection 67.3% similar

    A collection class for managing SharePoint site templates (WebTemplate objects), providing methods to retrieve and work with multiple web templates.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/webs/template_collection.py
  • class ListTemplateType 64.3% similar

    Specifies the type of a list template. A set of predefined values are specified in [MS-WSSTS] section 2.7.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/lists/template_type.py
  • class SPOTenantWebTemplateCollection 63.6% similar

    A collection class that represents a set of SharePoint Online tenant web templates, inheriting from Entity and providing access to template items.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/tenant/administration/webs/templates/collection.py
← Back to Browse