Microsoft Graph .NET Client Library: Upload file to document center - c#

I want to create a file in a Sharepoint document center using Microsoft Graph .NET Client Library. It seems that I can successfully create the file but it is not visible to anyone except the app which created it. When the app creates a file in an "ordinary" document library with exactly the same code then the file is visible to other users (as expected).
Here is the code I use:
var result = await client.Drives[documentCenterDriveId]
.Items[subfolderId]
.ItemWithPath(fileName)
.Content
.Request()
.PutAsync<DriveItem>(new MemoryStream(buffer));
Afterwards I can query the folder and see that the file is there:
var result = await client.Drives[documentCenterDriveId]
.Items[subfolderId]
.Children
.Request()
.GetAsync();
However when go to the Microsoft Graph Explorer, sign in with my personal credentials (i.e. not the same as the app) and issue the following GET request
https://graph.microsoft.com/v1.0/drives/documentCenterDriveId/items/subfolderId/children
I get an empty list. If I do the same for the "ordinary" document library it works as expected.
I checked the permissions of the file in the document center with
var perms = await client.Drives[documentCenterDriveId]
.Items[fileId]
.Permissions
.Request()
.GetAsync();
and the read role is granted to a group my account belongs to. This means I should be able to see it.
How can I diagnose the source of this issue? Are there logs somewhere in Sharepoint where I could find out more? There are no error messages or exceptions when I run my code.

I found out that I also have to checkin the file.
await client.Drives[documentCenterDriveId].Items[result.Id].Checkin().Request().PostAsync();
I don't know why I do not need to checkin the file after upload in a "ordinary" document library but in the document center I have to.

Related

OneDrive SDK (Graph API) Determine if file has been checked out

I am looking for the best way to check if a file has been checked out by a user using the OneDrive SDK. This is just a layer on the Graph API. I wish to prevent downloads in my application if the file is checked out.
I am getting the contents of a folder using this
var results = await graphClient.Drives[driveId].Items[folderId].Children.Request().GetAsync();
However there does not appear to be any properties in this response around if the item is checked out?
From my testing I can attempt to checkout the file anyway and handle an exception is returned if the file is already checked out, but this is not something I would want to do for multiple items for obvious performance reasons.
await graphClient.Drives[driveId].Items[documentId].Checkout().Request().PostAsync();
Is anyone aware of a more efficient way to determine if files are checked out?
You need to read publication property of publicationFacet type. It provides details on the published status of a driveItem resource.
publicationFacet type has property level which describes the state of publication for this document. Either published or checkout.
publication property is not returned by default and must be specified in Select method.
var results = await graphClient.Drives[driveId].Items[folderId].Children
.Request()
.Select("id,...,publication")
.GetAsync();
Documentation
publicationFacet

Why does MS Graph Thumbnail URL change after document changes

I am using MS Graph c# SDK to pull the thumbnail links for some docx and pdf files. The issue I was planning to store these links in my db to call them quickly for the front end. However, I noticed anytime a document is saved the link changes. However, the old URL still works and it also includes the latest changes. Is it safe to store the URLs and reuse them or does it not matter as my current experiment is showing:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var thumbnails = await graphClient.Me.Drive.Items["{driveItem-id}"].Thumbnails
.Request()
.GetAsync();
The URLs change to ensure that browser will invalidate their cache and render an updated thumbnail. We generally don't recommend caching the URLs yourself because there may be scenarios that trigger an expiration of the URL, at which point what you have cached will fail to work. However, as long as you handle failures of cache URLs by falling back to getting a new thumbnail URL maybe it'll be ok for your scenario.

SharepointDriveItem Preview URL to pen the document preview in SharePoint

I need to generate the preview URL of a document in SharePoint. For that I'm using the following code:
GraphServiceClient graphClient = await graphProvider.GetGraphServiceClient(authObject);
var preview = await graphClient.Sites[siteId].Drives[driveId].Items[graphId].Preview().Request().PostAsync();
return preview.GetUrl;
But the problem is that the above code is returning the embedded URL ( https://myDomain.sharepoint.com/sites/***********/_layouts/15/embed.aspx?uniqueId=2436cab2-0727-4480-9750-*******&access_token=****) which is opening in OneDrive instead of SharePoint.
As per MSDNThe preview action is currently only available on SharePoint and OneDrive for Business. So My question is, What's the change we need to do to get the preview in SharePoint?

Using MS Graph to Read Excel Contents Through Sharepoint

I'm attempting to use MS Graph to parse the contents of excel files and sync the data with other business-line applications. The issue I'm running into is actually getting at the data in excel.
I'm using a ClientCredentialProvider authenticating to an Azure AD App Registration which has FullControl and Read.All permissions in my tenant to create my GraphServiceClient, and can successfully query the Site via ID, and even see the document library I'm trying to access, but the Items comes up empty. See below
var result = _graphService.Client
.Sites["my-site-id"] // This works
.Drives["site-drive-id"] // This works to find the document library
//.Lists["list-id-corresponding-to-folder"] // This also works to find that folder
.Items // ****This is null****
.Request()
.GetAsync()
.Result;
I tried using the Lists property (as noted above) because as I understand it document libraries in SharePoint are really just lists, but again the Items enumeration yields no results.
I can't find documentation on Microsoft's site on this use case, and I notice using the MS Graph Explorer that the returned object doesn't have an "items" field (or many properties in the Microsoft.Graph.Site class), but I would think there is some way to get this field populated since it's implemented in the Microsoft.Graph namespace. Not sure if I'm missing some permissions step or what, but if I can access the site with FullControl I should be able to access all its contents...
I can't use Client.Me (personal drive) as a source location because 1) the application doesn't run under my permissions and 2) there is a need to read multiple files; the idea would be just provide the needed site/drive/item ID's and get the documents.
Is there some other way I should be going about this?
#broccoli_rob,
MS Graph has not exposed the functionality of enumerating all items in a drive. Instead, you can only list children or get item by id.
We suggest you use /drive/root:/{item-path} to get items in a folder. And you can vaild the endpoint in Graph explorer:
BR

Dropbox copy file to another account not working in C# using Dropbox.NET SDK API v2

I'm trying to copy a file from a dropbox account to another account. I'm trying the below code-
var client = new DropboxClient("c4zvSWqce");
await client.Files.CopyReferenceSaveAsync(strCopyRef, Path);
The CopyReference value is looking correctly but calling CopyReferenceSaveAsync showing error "no_permission". Please see the attachment. I couldn't find the reason for this error.
Any help will be appreciated.
The documentation gives the following definition for that error:
no_permission Void You don't have permission to save the given copy reference. Please make sure this app is same app which created the copy reference and the source user is still linked to the app.
Perhaps the access tokens you're using (for source and destination) are not for the same app?

Categories

Resources