A set of technologies in the .NET Framework for building web applications and XML web services.
Using MS Graph API (C# SDK) to edit PDFs stored in OneDrive
I have been using MS Graph SDK in an ASP.NET Core web application and have been very. happy with it so far. However, I have found some inconsistencies with how the Web URL and Share Links work and I am hoping I am just using them incorrectly and this can be fixed. I used the following to retrieve the web URL:
public async Task<DriveItem> DriveItemInfoAsync(string DriveID, string PathOrDriveItemID, string SearchType)
{
DriveItem response = null;
try
{
if (SearchType == "path")
{
response = await _graphServiceClient.Me.Drives[DriveID].Root
.ItemWithPath(PathOrDriveItemID)
.Request()
.GetAsync();
}
else if(SearchType == "id")
{
response = await _graphServiceClient.Me.Drives[DriveID]
.Items[PathOrDriveItemID]
.Request()
.GetAsync();
}
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting file info: {ex.ToString()}");
}
return response;
}
This provides me with the WebUrl which is my first problem. For docx files, xls files, and any other MS product files this link takes you directly to the appropriate office365 editor for live editing to OneDrive. However, if you have a pdf file (even with Adobe Cloud Integration Tools for OneDrive enabled) this link just takes you to open the PDF in the browser and not in the Adobe Cloud editor for saving directly to one drive. However, if you use the following code to create a share link:
public async Task<Permission> DriveItemLinksAsync(string DriveID, string PathOrDriveItemID, string SearchType, string Scope, string Type)
{
Permission response = null;
try
{
if (SearchType == "path")
{
response = await _graphServiceClient.Me.Drives[DriveID].Root
.ItemWithPath(PathOrDriveItemID)
.CreateLink(Type, Scope, null, null, null, true)
.Request()
.PostAsync();
}
else if (SearchType == "id")
{
response = await _graphServiceClient.Me.Drives[DriveID]
.Items[PathOrDriveItemID]
.CreateLink(Type, Scope, null, null, null, true)
.Request()
.PostAsync();
}
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting preview: {ex.ToString()}");
}
return response;
}
This will return the share link and for a docx file (or any other MS file type for this matter) it will take you to the correct Office365 editor similar to the WebUrl however for a pdf file this shared link will take you to a different screen which is a preview of the file which has a toolbar up top that allows you to open it in either the browser or the Adobe Cloud editor to save and edit directly to OneDrive. My users need to be able to edit MS files and Pdf files that are stored on OneDrive so I need to figure out a way to have consistent behavior. So is there any endpoint for MS Graph which returns this preview screen for most file types? Or is there a way to target the link for the adobe cloud editor for pdfs similar to how the docx files automatically open in the office 365 online editor.
Also for reference, this preview screen can also be gotten to by the OneDrive interface itself. If you log into OneDrive and right click the file and hit preview. This will take you to that preview screen/in between page before the online editors. Doing it this way also even lets you do it for docx files as well so again some more inconsistency I cannot figure out. I would just like to target that preview page with the toolbar if possible!
Thanks!
Developer technologies | ASP.NET | ASP.NET Core
Microsoft Security | Microsoft Graph
An API that connects multiple Microsoft services, enabling data access and automation across platforms