🔍 Code Extractor

class SharedInsight

Maturity: 54

A class representing insights about files shared with or by a specific user in Microsoft 365, including email attachments, OneDrive for Business, and SharePoint files.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/insights/shared.py
Lines:
8 - 54
Complexity:
moderate

Purpose

SharedInsight extends the Entity class to provide access to information about shared files in Microsoft 365 environments. It tracks files shared directly via email/meeting invites and modern attachments from OneDrive for Business and SharePoint. The class provides read-only access to sharing details, resource references, the actual shared resource, and complete sharing history. It is designed to be used within the Microsoft Graph API context for retrieving and analyzing file sharing patterns and activities.

Source Code

class SharedInsight(Entity):
    """
    An insight representing files shared with or by a specific user. The following shared files are supported:

      Files attached directly in an email or a meeting invite.
      OneDrive for Business and SharePoint modern attachments - files stored in OneDrive for Business and SharePoint
      that users share as a links in an email.
    """

    @property
    def last_shared(self):
        """Details about the shared item. Read-only"""
        return self.properties.get("lastShared", SharingDetail())

    @property
    def resource_reference(self):
        # type: () -> ResourceReference
        """Reference properties of the used document, such as the url and type of the document. Read-only"""
        return self.properties.get("resourceReference", ResourceReference())

    @property
    def resource(self):
        # type: () -> Entity
        """Used for navigating to the item that was shared. For file attachments, the type is fileAttachment.
        For linked attachments, the type is driveItem."""
        return self.properties.get(
            "resource",
            Entity(self.context, ResourcePath("resource", self.resource_path)),
        )

    @property
    def sharing_history(self):
        # type: () -> ClientValueCollection[SharingDetail]
        """Details about the sharing history. Read-only"""
        return self.properties.get(
            "sharingHistory", ClientValueCollection(SharingDetail)
        )

    def get_property(self, name, default_value=None):
        if default_value is None:
            property_mapping = {
                "lastShared": self.last_shared,
                "resourceReference": self.resource_reference,
                "sharingHistory": self.sharing_history,
            }
            default_value = property_mapping.get(name, None)
        return super(SharedInsight, self).get_property(name, default_value)

Parameters

Name Type Default Kind
bases Entity -

Parameter Details

context: The client context object required for making API calls to Microsoft Graph. Inherited from Entity base class.

resource_path: The resource path identifying this specific SharedInsight entity in the Microsoft Graph API hierarchy. Inherited from Entity base class.

Return Value

Instantiation returns a SharedInsight object that provides access to sharing information through its properties. The properties return various types: last_shared returns a SharingDetail object, resource_reference returns a ResourceReference object, resource returns an Entity object (fileAttachment or driveItem), and sharing_history returns a ClientValueCollection of SharingDetail objects. The get_property method returns the requested property value or a default value if not found.

Class Interface

Methods

@property last_shared() -> SharingDetail property

Purpose: Retrieves details about when and how the item was last shared

Returns: A SharingDetail object containing information about the most recent sharing event, including who shared it and when. Returns an empty SharingDetail object if not yet populated.

@property resource_reference() -> ResourceReference property

Purpose: Retrieves reference properties of the shared document such as URL and type

Returns: A ResourceReference object containing metadata about the shared resource including its web URL and resource type. Returns an empty ResourceReference object if not yet populated.

@property resource() -> Entity property

Purpose: Provides navigation to the actual item that was shared

Returns: An Entity object representing the shared item. For file attachments, returns a fileAttachment type. For linked attachments, returns a driveItem type. Returns a base Entity object if not yet populated.

@property sharing_history() -> ClientValueCollection[SharingDetail] property

Purpose: Retrieves the complete history of all sharing events for this item

Returns: A ClientValueCollection containing SharingDetail objects representing each time the item was shared. Returns an empty collection if not yet populated.

get_property(name: str, default_value=None) -> Any

Purpose: Safely retrieves a property value by name with fallback to default values

Parameters:

  • name: The name of the property to retrieve (e.g., 'lastShared', 'resourceReference', 'sharingHistory')
  • default_value: Optional default value to return if the property is not found. If None, uses predefined property mappings.

Returns: The value of the requested property, or the default value if not found. For mapped properties (lastShared, resourceReference, sharingHistory), returns the corresponding property object.

Attributes

Name Type Description Scope
properties dict Dictionary storing the entity's property values, inherited from Entity base class. Contains raw data for lastShared, resourceReference, resource, and sharingHistory. instance
context ClientContext The client context object used for making API requests to Microsoft Graph, inherited from Entity base class. instance
resource_path ResourcePath The path identifying this entity's location in the Microsoft Graph API hierarchy, inherited from Entity base class. instance

Dependencies

  • office365

Required Imports

from office365.directory.insights.shared_insight import SharedInsight
from office365.directory.insights.resource_reference import ResourceReference
from office365.directory.insights.sharing_detail import SharingDetail
from office365.entity import Entity
from office365.runtime.client_value_collection import ClientValueCollection
from office365.runtime.paths.resource_path import ResourcePath

Usage Example

from office365.graph_client import GraphClient
from office365.directory.insights.shared_insight import SharedInsight

# Authenticate and create client context
client = GraphClient.with_token(lambda: 'your_access_token')

# Get shared insights for a specific user
user_id = 'user@example.com'
shared_insights = client.users[user_id].insights.shared.get().execute_query()

# Iterate through shared insights
for insight in shared_insights:
    # Access sharing details
    last_shared = insight.last_shared
    print(f"Last shared by: {last_shared.shared_by}")
    print(f"Last shared date: {last_shared.shared_date_time}")
    
    # Access resource reference
    resource_ref = insight.resource_reference
    print(f"Resource URL: {resource_ref.web_url}")
    print(f"Resource type: {resource_ref.type}")
    
    # Access the actual resource
    resource = insight.resource
    
    # Access sharing history
    history = insight.sharing_history
    for detail in history:
        print(f"Shared on: {detail.shared_date_time}")

Best Practices

  • This class is read-only; all properties provide access to data but do not support modification
  • Always ensure proper authentication and permissions are configured before accessing SharedInsight objects
  • The class is designed to be instantiated through the Microsoft Graph API client context, not directly
  • Use execute_query() on the parent collection to populate the properties with actual data from the API
  • Properties use lazy loading - they return default empty objects if data hasn't been fetched yet
  • The resource property can return different entity types (fileAttachment or driveItem) depending on the shared item type
  • Access properties through the property decorators rather than directly accessing the properties dictionary
  • The get_property method provides a safe way to access properties with fallback to default values

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class UsedInsight 81.2% similar

    A class representing insights about documents used by a specific user, tracking the most relevant documents viewed or modified in OneDrive for Business and SharePoint.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/insights/used.py
  • class TopFilesSharingInsights 78.1% similar

    A SharePoint entity class representing insights about the most shared files in a tenant, providing access to file sharing analytics data.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/tenant/administration/insights/top_files_sharing.py
  • class OfficeGraphInsights 76.2% similar

    A class representing Office Graph Insights that provides access to calculated relationships for documents shared, trending, or used by a user through advanced analytics and machine learning.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/insights/office_graph.py
  • class InsightIdentity 73.2% similar

    InsightIdentity is a data class that represents the identity properties of sharedInsight items in Microsoft Office 365 services.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/insights/identity.py
  • class SharingDetail 69.7% similar

    A data class representing detailed information about how a document or resource was shared in Microsoft 365, including who shared it, when, and through what method.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/insights/sharing_detail.py
← Back to Browse