Ways to add an item to SharePoint lists
Explained the same in Youtube Video : https://youtu.be/xioKl4KrlLo
To use Graph API to add content to a SharePoint list, you need to have the appropriate permissions, the site ID, the list ID, and the JSON representation of the list item you want to create. You can use the following HTTP request to create a new list item:
POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items
Content-Type: application/json
{
"fields": {
// your list item properties
}
}
To get site ID, use below URL
https://TenantName.sharepoint.com/sites/SiteName/_api/site
Here at the ID after HUBSiteID tag is actually your Site ID as shown below.
For example, if you want to create a list item with the title “Item2”, the color “Pink”, and the Number 1, you can use this JSON body:
{
"fields": {
"Title": "Item2",
"Color": "Pink",
"Number": 1
}
}
If the request is successful, you will get a response with the created list item and its properties.
You can also use the Graph SDKs to simplify the process of creating list items in different programming languages. For example, here is how you can create a list item in C# using the Microsoft Graph SDK:
using Microsoft.Graph;
using Microsoft.Identity.Client;
// Create a public client application
var app = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.Build();
// Get the access token
var result = await app.AcquireTokenInteractive(scopes)
.ExecuteAsync();
// Create a Graph client
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", result.AccessToken);
return Task.CompletedTask;
}));
// Create a dictionary of fields for the list item
var fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>()
{
{"Title", "Item"},
{"Color", "Pink"},
{"Number", 1}
}
};
// Create the list item
var listItem = await graphClient.Sites[siteId].Lists[listId].Items
.Request()
.AddAsync(fields);