Skip to content

sharepoint

sharepoint

update_resultat_sharepoint_rest

update_resultat_sharepoint_rest(item_id, link)

Updates the 'Resultat' hyperlink field in a SharePoint list item using SharePoint REST API. Reads configuration from your secret store.

Source code in source/src/sharepoint.py
def update_resultat_sharepoint_rest(item_id, link):
    """
    Updates the 'Resultat' hyperlink field in a SharePoint list item using SharePoint REST API.
    Reads configuration from your secret store.
    """
    # Load secrets
    sharepoint_domain = read_secret("SHAREPOINT_DOMAIN")
    site_name = read_secret("SITE_NAME")
    list_name = read_secret("SHAREPOINT_LIST_NAME")

    # Get token
    token_manager = get_token_manager()
    access_token = token_manager.get_token()

    # Step 1: Get ListItemEntityTypeFullName
    meta_url = f"https://{sharepoint_domain}/sites/{site_name}/_api/web/lists/getbytitle('{list_name}')"
    meta_headers = {
        "Authorization": f"Bearer {access_token}",
        "Accept": "application/json;odata=verbose"
    }

    meta_resp = requests.get(meta_url, headers=meta_headers)
    meta_resp.raise_for_status()
    entity_type = meta_resp.json()["d"]["ListItemEntityTypeFullName"]

    # Step 2: Update the item
    update_url = f"https://{sharepoint_domain}/sites/{site_name}/_api/web/lists/getbytitle('{list_name}')/items({item_id})"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Accept": "application/json;odata=verbose",
        "Content-Type": "application/json;odata=verbose",
        "IF-MATCH": "*",
        "X-HTTP-Method": "MERGE"
    }

    payload = {
        "__metadata": { "type": entity_type },
        "Resultats": {
            "__metadata": { "type": "SP.FieldUrlValue" },
            "Url": str(link),
            "Description": "Link a la carpeta de la justificacio"
        }
    }

    response = requests.post(update_url, headers=headers, json=payload)
    response.raise_for_status()
    print("✅ Successfully updated 'Resultat' field via SharePoint REST API.")

get_sharepoint_web_url

get_sharepoint_web_url(token_manager, site_id, drive_id, folder_path)

Given a folder path inside the drive, returns its webUrl for user access. Example path: Shared Documents/_output/amarine@iciq.es

Source code in source/src/sharepoint.py
def get_sharepoint_web_url(token_manager, site_id, drive_id, folder_path):
    """
    Given a folder path inside the drive, returns its webUrl for user access.
    Example path: Shared Documents/_output/amarine@iciq.es
    """
    url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/root:/{folder_path}"
    headers = {
        "Authorization": f"Bearer {token_manager.get_token()}",
    }

    response = requests.get(url, headers=headers)
    response.raise_for_status()
    item = response.json()
    return item.get("webUrl")