🔍 Code Extractor

class FieldLinkCollection

Maturity: 46

A collection class for managing SharePoint field links, providing methods to add, retrieve, and reorder field links within a content type or similar SharePoint entity.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/contenttypes/fieldlinks/collection.py
Lines:
10 - 66
Complexity:
moderate

Purpose

FieldLinkCollection manages a collection of FieldLink objects in SharePoint. It extends EntityCollection to provide specialized operations for field links including adding new field links (either by field object or internal name), retrieving field links by ID, and reordering the collection. This class is typically used when working with SharePoint content types to manage the fields associated with them.

Source Code

class FieldLinkCollection(EntityCollection):
    """Specifies a Collection for field links."""

    def __init__(self, context, resource_path=None):
        super(FieldLinkCollection, self).__init__(context, FieldLink, resource_path)

    def add(self, field):
        """
        Add a field link with the specified link information to the collection.
        A reference to the SP.Field that was added is returned.

        :param str or office365.sharepoint.fields.field.Field field: Specifies the internal name of the field or type
        """

        def _create_and_add_query(field_internal_name):
            """
            :type field_internal_name: str
            """
            return_type.set_property("FieldInternalName", field_internal_name)
            qry = CreateEntityQuery(self, return_type, return_type)
            self.context.add_query(qry)

        return_type = FieldLink(self.context)
        self.add_child(return_type)
        if isinstance(field, Field):

            def _field_loaded():
                _create_and_add_query(field.internal_name)

            field.ensure_property("InternalName", _field_loaded)
        else:
            _create_and_add_query(field)
        return return_type

    def get_by_id(self, _id):
        """
        Gets the field link with the given id from this collection.<20> If the id is not found in the collection,
        returns null.

        :param str _id: The GUID that specifies the Microsoft.SharePoint.Client.FieldLink (section 3.2.5.46)
            that is returned.
        """
        return FieldLink(
            self.context, ServiceOperationPath("GetById", [_id], self.resource_path)
        )

    def reorder(self, internal_names):
        """
        Rearranges the collection of field links in the order in which field internal names are specified.

        :param list[str] internal_names: Specifies field internal names that are arranged in the order in which the
            collection of field links is reordered.
        """
        payload = {"internalNames": StringCollection(internal_names)}
        qry = ServiceOperationQuery(self, "Reorder", None, payload)
        self.context.add_query(qry)
        return self

Parameters

Name Type Default Kind
bases EntityCollection -

Parameter Details

context: The client context object that provides the connection and authentication to the SharePoint site. This is required for all operations and is passed to child FieldLink objects.

resource_path: Optional. The resource path in the SharePoint REST API that identifies this collection. If not provided, it will be derived from the parent entity. Used for constructing API requests.

Return Value

Instantiation returns a FieldLinkCollection object that can manage field links. The add() method returns a FieldLink object representing the newly added field link. The get_by_id() method returns a FieldLink object for the specified ID. The reorder() method returns self for method chaining.

Class Interface

Methods

__init__(self, context, resource_path=None)

Purpose: Initializes a new FieldLinkCollection instance with the provided context and optional resource path

Parameters:

  • context: The client context object for SharePoint operations
  • resource_path: Optional resource path for the collection in the SharePoint REST API

Returns: None (constructor)

add(self, field) -> FieldLink

Purpose: Adds a new field link to the collection using either a field internal name string or a Field object

Parameters:

  • field: Either a string representing the field's internal name, or a Field object. If a Field object is provided, its InternalName property will be loaded if necessary

Returns: A FieldLink object representing the newly added field link. The object is added to the collection and a create query is queued in the context

get_by_id(self, _id) -> FieldLink

Purpose: Retrieves a field link from the collection by its unique identifier (GUID)

Parameters:

  • _id: A string containing the GUID that uniquely identifies the field link to retrieve

Returns: A FieldLink object with the specified ID. Returns null if the ID is not found in the collection

reorder(self, internal_names) -> FieldLinkCollection

Purpose: Rearranges the field links in the collection according to the order specified by the internal names list

Parameters:

  • internal_names: A list of strings containing field internal names in the desired order. All field links should be represented in this list

Returns: Returns self (the FieldLinkCollection instance) to enable method chaining

Attributes

Name Type Description Scope
context ClientContext The SharePoint client context inherited from EntityCollection, used for executing queries and managing the connection instance
resource_path ResourcePath or None The resource path in the SharePoint REST API that identifies this collection, inherited from EntityCollection instance

Dependencies

  • office365.runtime.paths.service_operation
  • office365.runtime.queries.create_entity
  • office365.runtime.queries.service_operation
  • office365.runtime.types.collections
  • office365.sharepoint.contenttypes.fieldlinks.field_link
  • office365.sharepoint.entity_collection
  • office365.sharepoint.fields.field

Required Imports

from office365.sharepoint.contenttypes.fieldlinks.field_link_collection import FieldLinkCollection
from office365.sharepoint.fields.field import Field

Usage Example

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.contenttypes.fieldlinks.field_link_collection import FieldLinkCollection

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

# Get a content type and its field links
content_type = ctx.web.content_types.get_by_id('0x0101')
field_links = content_type.field_links

# Add a field link by internal name
new_link = field_links.add('Title')
ctx.execute_query()

# Add a field link by Field object
field = ctx.web.fields.get_by_internal_name_or_title('Description')
field_link = field_links.add(field)
ctx.execute_query()

# Get a field link by ID
field_link = field_links.get_by_id('guid-here')
ctx.load(field_link)
ctx.execute_query()

# Reorder field links
field_links.reorder(['Title', 'Description', 'Author'])
ctx.execute_query()

Best Practices

  • Always call ctx.execute_query() after operations like add() or reorder() to commit changes to SharePoint
  • When adding a Field object, ensure the Field's InternalName property is loaded before the add operation completes
  • Use get_by_id() with valid GUIDs to retrieve specific field links
  • The reorder() method requires all field internal names to be specified in the desired order
  • Field links are typically managed within the context of a content type
  • The add() method accepts either a string (field internal name) or a Field object for flexibility
  • Method chaining is supported for reorder() which returns self

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class FieldCollection 80.3% similar

    A collection class for managing SharePoint Field resources, providing methods to create, retrieve, and manipulate various types of fields (columns) in SharePoint lists and libraries.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/fields/collection.py
  • class FieldLink 75.7% similar

    A class representing a reference to a field or field definition for a SharePoint content type, providing access to field metadata and configuration properties.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/contenttypes/fieldlinks/field_link.py
  • class RelatedFieldCollection 74.1% similar

    A collection class that manages and provides access to RelatedField resources in SharePoint, extending EntityCollection with specialized methods for field retrieval.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/fields/related_field_collection.py
  • class ListItemCollection 70.7% similar

    A collection class for managing SharePoint list items, providing methods to retrieve items by various identifiers and URLs.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/listitems/collection.py
  • class FileCollection 68.7% similar

    FileCollection is a class that manages a collection of File resources in SharePoint, providing methods to upload, add, and retrieve files from a folder or document library.

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