GarudaLead
GarudaLead

Reputation: 479

Sharepoint List Items values with Graph API

I have a SharePoint List that I am able to successfully connect to via the Graph API. It pulls the list items but seems only to be showing the base data. How to I get to the fields and the data that is contained in those fields so that I can display / update that data?

Also, I assume the SPO Graph API enpoint is: https://graph.microsoft.com/v1.0/

var assetlist = await graphClient
    .Sites[AllCompanySiteID]
    .Lists[CurrentBuildsListID]
    .Items
    .Request()
    .GetAsync();

totalitems.AddRange(assetlist.CurrentPage);

while (assetlist.NextPageRequest != null)
{
    assetlist = assetlist.NextPageRequest.GetAsync().Result;
    totalitems.AddRange(assetlist.CurrentPage);
}

Upvotes: 2

Views: 4489

Answers (1)

Antonio Leonardo
Antonio Leonardo

Reputation: 1862

It's unclear whether you want to (1) Get Data Fields only and Update Data Fields only or (2) you need to Update Data Fields from Selection - when in doubt I'll write both approaches. If your requirement is item (2), you may want to map/model the business rule to develop the routine. According to Microsoft Docs, the following examples:

(1) Get Data Fields only:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var queryOptions = new List<QueryOption>()
{
    new QueryOption("expand", "fields(select=Field1,Field2,Field3)")
};

var items = await graphClient.Sites["{site-id}"].Lists["{list-id}"].Items
    .Request( queryOptions )
    .GetAsync();

(1) Update Data Fields only:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var fieldValueSet = new FieldValueSet
{
    AdditionalData = new Dictionary<string, object>()
    {
        {"Field1", "Test1"},
        {"Field2", "Test2"}
    }
};

await graphClient.Sites["{site-id}"].Lists["{list-id}"].Items["{listItem-id}"].Fields
    .Request()
    .UpdateAsync(fieldValueSet);

(2) Update Data Fields from Selection example:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    
    var queryOptions = new List<QueryOption>()
    {
        new QueryOption("expand", "fields(select=Field1,Field2,Field3)")
    };
    
    var items = await graphClient.Sites["{site-id}"].Lists["{list-id}"].Items
        .Request( queryOptions )
        .GetAsync();
foreach(var item in items)
{
    //an example of Business Rule: Update item based by some Select into current item
    if(null != item.Fields.AdditionalData["Field1"] && !string.IsNullOrWhiteSpace(item.Fields.AdditionalData["Field1"].ToString()))
    {
        var fieldValueSet = new FieldValueSet
        {
            AdditionalData = new Dictionary<string, object>()
            {
                {"Field2", "Test2"},
                {"Field3", "Test3"}
            }
        };
        await graphClient.Sites["{site-id}"].Lists["{list-id}"].Items[item.Id.ToString()].Fields
        .Request()
        .UpdateAsync(fieldValueSet);
    }
}

Upvotes: 3

Related Questions