Forum Discussion

Sharmilshah's avatar
Sharmilshah
Frequent Visitor
1 year ago
Solved

Write Back data from PowerBI to SharePoint

Hi all, I'm working on a Power BI report where users need to update a SharePoint list by clicking a Power Automate button. I have everything working when values are selected from slicers or table vi...
  • vojtechsima's avatar
    vojtechsima
    1 year ago

    hey, Sharmilshah ,

    You can still use user defined function, you just need to call graph api with delegated access to sharepoint list.

     

    However, you essentially need a registered app with permission to access SharePoint's Graph API to share with the sites you need. Should be delegated per site, it can look like this:

    Depends on your admins, but they can grant you this.

     

    And once you have this, you can call graph's rest api for SharePoint, I don't have code at end, but I vibe coded something like this (please note, it can be wrong, just an idea):

    import fabric.functions as fn
    import logging
    import requests
    
    udf = fn.UserDataFunctions()
    
    @udf.connection(argName="sharepoint", alias="mysharepoint")
    @udf.function()
    def UpdateSharePointComment(
        sharepoint: fn.FabricGenericConnection,  # Use the right connection type for your setup
        site_url: str,
        list_name: str,
        item_id: int,
        comment: str,
        access_token: str  # Ideally, get this securely or use Managed Identity if possible
    ) -> str:
        logging.info("Processing UpdateSharePointComment")
    
        # Validation
        if not comment or not comment.strip():
            raise fn.UserThrownError("Comment cannot be empty.")
    
        # Compose SharePoint API URL
        url = f"{site_url}/_api/web/lists/getbytitle('{list_name}')/items({item_id})"
    
        headers = {
            "Authorization": f"Bearer {access_token}",
            "Accept": "application/json;odata=verbose",
            "Content-Type": "application/json"
        }
    
        # SharePoint expects a PATCH for updates
        data = {
            "__metadata": {"type": "SP.Data.YourListNameListItem"},  # You must specify the internal list item type
            "Comment": comment  # Change to your column's internal name
        }
    
        # PATCH request
        response = requests.post(
            url,
            headers=headers,
            json=data,
            params={"$select": "Id"}
        )
    
        if response.status_code not in (200, 204):
            raise fn.UserThrownError(f"SharePoint update failed: {response.text}")
    
        return f"SharePoint item {item_id} updated with comment '{comment}'."
    



  • v-veshwara-msft's avatar
    1 year ago

    Hi Sharmilshah ,

    Thanks for posting in Microsoft Fabric Community.

    Just checking in to see if have you had a chance to review the information provided by vojtechsima.

    As mentioned, since you're using a workspace with Fabric capacity, you're in a good position to take advantage of the Text Slicer (preview). This allows users to enter free text directly in Power BI, which can then be passed into a User-Defined Function and used within a Translytical Task Flow to update the SharePoint list.

     

    The blog shared earlier by vojtechsima walks through how to set up native writeback using Fabric, which is relevant for your scenario.

    Also as suggested, to write back directly to a SharePoint list, you can use the Graph API. This does require registering an app with the necessary delegated permissions to access the SharePoint site, but once configured, it allows you to update list items, including custom fields like comments.

     

    Please reach out for further assistance.

     

    Also thanks speedramps for suggesting Power Apps and providing helpful resources. This helps others with similar requirement.

     

    Regards.