Azure / Microsoft365R

R SDK for interacting with Microsoft 365 APIs
Other
308 stars 41 forks source link

Unable to create embed share link #182

Closed scrapeable closed 10 months ago

scrapeable commented 10 months ago

I just signed up for an MS office business account I'm trying to create a bunch of embedded excel files for a web app. I believe I have all the permissions I need which tends to be the reasoning for errors like this. However I've been messing around with the below code but still can't figure out how to create an embeddable or any type of share link for an excel file.

library(Microsoft365R)
library(openxlsx)

odb <- get_business_onedrive()

# already wrote an excel file with iris dataset
odb$upload_file(
  src = "data/iris.xlsx",
  dest = "public/iris.xlsx"
)

# already created a folder called public using odb$create_folder()
files <- odb$list_files(
  path = "public"
)

odb$create_share_link(
  itemid = files$id[1],
  type = "embed",
  expiry = NULL
)

On my create_share_link chunk of code I'm getting the below error. The file exists and is written to my business one drive, I've messed around with about every possible argument between using path / id for the file, and type of view, edit, and embed. For some reason not working.

Error in odb$create_share_link(itemid = files$id[1], type = "embed", expiry = NULL) : 
  attempt to apply non-function

Feel like maybe I'm missing some operation to make the file public first or something, or it could possibly be a bug, hence posting here. Any help would be much appreciated!

edit: I've messed around with the expiry with no luck too. I want to use NULL so the link does not expire as I'm indexing these URL's elsewhere... for my web app.

118 Ahh just seeing this.

This works and returns a share link

file <- odb$get_item(
  itemid = files$id[1]
)

file$create_share_link(
  type = "view",
  expiry = NULL
)

However this does not work

file$create_share_link(
  type = "embed",
  expiry = NULL
)

I'm getting the error

Error in process_response(res, match.arg(http_status_handler), simplify) : 
  Bad Request (HTTP 400). Failed to complete operation. Message:
Requested sharingLink type is not yet available.

Reading the graph api documentation... looks like maybe this cannot be done on business for some reason (This option is only available for files in OneDrive personal) that's really dumb. https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0&tabs=http

view | Creates a read-only link to the DriveItem.
edit | Creates a read-write link to the DriveItem.
embed | Creates an embeddable link to the DriveItem. This option is only available for files in OneDrive personal.

You've also noted this in the documentation which I missed... However I believe there is some way to do this. As if I can find the sourcedoc id I can embed the file without even creating the share link, was able to do so manually. Going to keep searching.

hongooi73 commented 10 months ago

Yeah, the R package is basically just an interface to the Graph API. So if the API doesn't support it, then there's nothing I can do. Note that if you use the web interface to OD/Sharepoint, it can take advantage of Microsoft-specific magic that isn't generally available.

scrapeable commented 10 months ago

Actually I found a way to do it, the below code will create a share link which can then be adjusted to be embedded in an html page / web app. I believe it might mess things up if you try viewing on mobile but it works great in the browser. Below will create a share link that anyone can view, the only real loss of functionality between this an embed links is that you lose the ability to use wdAllowInteractivity=True. From what I can tell this allows the user to make changes, create calculations...etc but won't save the file. Giving each user their own embedded session to mess with the workbook. I would prefer users could do this but for my use case allowing them to just view is good enough.


library(Microsoft365R)
library(openxlsx)

# successfully connects to one drive
odb <- get_business_onedrive()

# successfully uploads dummy file
odb$upload_file(
  src = "data/iris.xlsx",
  dest = "public/iris.xlsx"
)

# successfully lists dummy file
files <- odb$list_files(
  path = "public"
)

# grabs the dummy file
file <- odb$get_item(
  itemid = files$id[1]
)

# successfully creates a share link but cannot be embedded or I'm missing something
share_link <- file$create_share_link(
  type = "view",
  expiry = NULL,
  scope = "anonymous"
) 

# edit share link which can now be embedded (action=embedview)
share_link <- paste0(share_link, "?action=embedview&wdAllowInteractivity=True&wdHideGridlines=False&wdHideHeaders=False&wdDownloadButton=False&wdInConfigurator=True")