0

I have the id of a driveitem and the id of the drive it resides in. This file is a simple text file. I would like to update the fields of the listItem but I am having a hard time doing that since I am unable to get the listItem using the driveItem id. According to Microsoft "All items in a SharePoint document library can be represented as a listItem or driveItem resource.". You can only update the fields on a listItem since a listItem has the fields property. But I am only able to get the file as a driveItem using the id.

Is it possible to update the fields of a listItem if the only information you have is the id of the drive and the id of the driveItem?

I can get the fields like this:

var listItem = Task.Run(() => graphClient.Drives[driveId].Items[driveItemItemId].ListItem.GetAsync()).Result;
var fields = listItem.Fields;

and that works. But I am not sure how I can use that to update the values of the field in SharePoint.

I have tried updating the fields like this:

DriveItem item = new DriveItem()
    {
        ListItem = new ListItem()
        {
            Fields = new FieldValueSet()
            {
                AdditionalData = new Dictionary<string, object>
                {
                    {
                        columnKey, columnValue
                    },
                },
            }
        }
    };
        
    var driveItem = Task.Run(() => graphClient.Drives[driveId].Items[driveItemItemId].PatchAsync(item)).Result;

But this gives me an error:

"Cannot update a listItem via driveItem, patch the listItem directly"

Sure, but how do I go about getting the listItem so I can update it using only the drive id and the drive item id?

3 Answers 3

1

I have found a solution which is adequate but uses an extra step that I would think would not be needed.

Here is the solution:

var requestBody = new FieldValueSet
{
    AdditionalData = new Dictionary<string, object>
    {
        {
            columnKey, columnValue
        },
    },
};
    
var listItem = Task.Run(() => graphClient.Drives[driveId].Items[driveItemItemId].ListItem.GetAsync()).Result;
var list = Task.Run(() => graphClient.Sites[siteId].Lists[listTitleOrId].GetAsync()).Result;
var t = Task.Run(() => graphClient.Sites[siteId].Lists[list.Id].Items[listItem.Id].Fields.PatchAsync(requestBody)).Result;

The part that is so awkward is that the last line of the code uses the id of the listItem that is just an integer number that you have no way of knowing unless you retrieve the DriveItem.ListItem.

It is like described here but the confusing part is the "{listItem-id}" part. It is not the unique id of the DriveItem but rather the id of the ListItem which is just a self incrementing integer. To get that value you need to fetch the DriveItem and look at the ListItem.Id property.

2
  • Hi, I have a question, I'm trying the same but the problem is I don't have the listTitleOrId because this is something dynamic. Do you get this data from the graphclient or are you using only one list?
    – Niels
    Commented Jun 30, 2023 at 7:49
  • 1
    I have the id of the list that I use for listTitleOrId. You can try to use "Documents" instead. That should give you the default library list.
    – Sigmundur
    Commented Jul 3, 2023 at 13:57
1

If anyone else but me has landed here, searching for a way to get the associated ListItem for a DriveItem (file or folder), using raw HTTP Graph API, this is the url you need.

GET https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{itemId}/listItem
0

You can use following code. It should work

var updatedFieldValues = new FieldValueSet
                {
                    AdditionalData = new Dictionary<string, object>
                        {
                            {
                                "Title" , "Updated Title"
                            }
                        },
                };

var updatedItem = await graphClient.Drives[{driveId}].Items[driveItemItemId].ListItem.Fields.Request().UpdateAsync(updatedFieldValues);

I have tested it in my environment and it works fine.

Update

Important: This answer is applicable only when you are using Microsoft.Graph version < 5

2
  • It seems that you are using version <5 of Graph api. I am using version 5.9 and the ListItem on the Items does not contain a definition for Fields.
    – Sigmundur
    Commented May 19, 2023 at 10:11
  • You are right. I just updated my answer with mentioning for which version of Microsoft.Graph it's applicable Commented May 19, 2023 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.