Forum Discussion
Write Back data from PowerBI to SharePoint
- 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}'." - 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.
Hey, Sharmilshah ,
Sounds like a perfect use case for Fabric's user-defined functions and Translytical task flows.
Power BI now using new Text Slicer as Text Input and you can map it to an action that will write back to your source, it just needs a bit of setup and fabric licence/capacity.
Check out this blog:
https://www.vojtechsima.com/post/native-write-back-in-power-bi-fabric-preview
Thank you vojtechsima for sharing the TTF option. Your details are excellent. In my use case, I have a sharepoint list as a datasource. I can pull data into Fabric (as I have workspace with Fabric capacity) but my intention is not to just write back to Fabric but want to update end source which is SharePoint list also. I tried to figure out a solution but couldn't find a direct way.
- vojtechsima1 year agoSuper User
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}'."