Graph API : Get Document Library Custom Columns using GraphServiceClient

Soni Samuel Panackacheril 61 Reputation points
2022-08-15T11:44:58.983+00:00

Hi,

I am trying to get Custom Columns in a Sharepoint Online Document Library using GraphService Client.

I am able to get the Files in a Drive (Document Library) using the above approach after creating an IConfidentialClientApplication and getting the Access Token. Even the default columns are not being fetched when I pass the queryOptions into the Request.

var site = await graphServiceClient.Sites[$"{SiteId}"].Request().GetAsync();
var queryOptions = new List<QueryOption>()
{
new QueryOption("expand", "fields(select=Id,Name)")
};
var driveItems = await graphServiceClient.Sites[site.Id].Drives[$"{DocLibraryId}"]
.Root.Children
.Request(queryOptions)
.GetAsync();

Appreciate some feedback on this.

Regards,

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,685 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,708 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Srinivasa Rao Darna 6,696 Reputation points Microsoft Vendor
    2022-08-15T13:13:56.92+00:00

    Hi @Soni Samuel Panackacheril ,

    Driveitem doesn't have fields relationship only listItem has.
    You can use driveitem relationship listItem this example will get drives root children listitem.

    GET /sites/{siteId}/drives/{driveId}/root/children?$expand=listItem

    Your Graph SDK code would look as below,

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
    var children = await graphClient.Sites[{siteId}].Drives[{driveId}].Root.Children  
     .Request()  
     .Expand("listItem")  
     .GetAsync();  
    

    To get a driveItems column metadata properties GET /sites/{siteId}/drives/{driveId}/items/{driveItemId}/listItem.

    Also, additionally we can use Sites-Lists to get column values of document library, please note that driveId and listIdOfDocLibr will be different.

    GET /sites/{siteId}/lists/{listIdOfDocLib}/items?$expand=fields

    References:
    https://learn.microsoft.com/en-us/graph/api/resources/listitem?view=graph-rest-1.0#relationships
    https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#relationships

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.