Skip to content

Assign a license to a user and switch between service plans.

Shows how to add, remove, and switch licenses using SKU IDs.

Permissions

Requires delegated permission User.ReadWrite.All, Organization.Read.All.

Reference

View source

import sys

from office365.directory.licenses.assigned_license import AssignedLicense
from office365.graph_client import GraphClient
from tests.settings import (
    client_id,
    client_secret,
    tenant,
    user_principal,
)

client = GraphClient(tenant=tenant).with_client_secret(client_id, client_secret)

# Find the first available license SKU
skus = client.subscribed_skus.get().execute_query()
if len(skus) == 0:
    sys.exit("No license SKUs found")

sku = skus[0]
print(f"Using SKU: {sku.sku_part_number}  (id: {sku.sku_id})")

# Assign license to a user
user = client.users.get_by_principal_name(user_principal)

try:
    result = user.assign_license(
        add_licenses=[AssignedLicense(skuId=sku.sku_id)],
        remove_licenses=[],
    ).execute_query()
    print(f"License assigned to {user.display_name}")
except Exception as e:
    print(f"License assignment skipped (may already be licensed): {e}")

← Back to Licensing