admin管理员组

文章数量:1431721

I'm trying to create a sharepoint list following the example on Microsofts documentation

// Dependencies
using Microsoft.Graph.Models;

var requestBody = new List
{
DisplayName = "Books",
Columns = new List<ColumnDefinition>
{
    new ColumnDefinition
    {
        Name = "Author",
        Text = new TextColumn
        {
        },
    },
    new ColumnDefinition
    {
        Name = "PageCount",
        Number = new NumberColumn
        {
        },
    },
},
List = new ListInfo
{
    Template = "genericList",
},
};

var result = await graphClient.Sites["{site-id}"].Lists.PostAsync(requestBody);

However the compiler marks the line

List = new ListInfo

with 'List' does not contain a definition for 'List' (CS0117)

Quickfix suggests changing it to ListProp. That compiles but on runtime that produces an Invalid Request as does ommiting that entry.

I'm trying to create a sharepoint list following the example on Microsofts documentation

// Dependencies
using Microsoft.Graph.Models;

var requestBody = new List
{
DisplayName = "Books",
Columns = new List<ColumnDefinition>
{
    new ColumnDefinition
    {
        Name = "Author",
        Text = new TextColumn
        {
        },
    },
    new ColumnDefinition
    {
        Name = "PageCount",
        Number = new NumberColumn
        {
        },
    },
},
List = new ListInfo
{
    Template = "genericList",
},
};

var result = await graphClient.Sites["{site-id}"].Lists.PostAsync(requestBody);

However the compiler marks the line

List = new ListInfo

with 'List' does not contain a definition for 'List' (CS0117)

Quickfix suggests changing it to ListProp. That compiles but on runtime that produces an Invalid Request as does ommiting that entry.

Share Improve this question asked Nov 19, 2024 at 12:19 RoestRoest 8186 silver badges17 bronze badges 2
  • The issue you're encountering arises from a mismatch between the property name you're trying to use (List) and what the Microsoft Graph API expects for creating a new SharePoint list. Based on the documentation and the structure of ListInfo, the correct property name is actually ListInfo (or ListTemplate in some contexts), not List. – Rukmini Commented Nov 19, 2024 at 12:23
  • Tried them both already. Won't compile either. – Roest Commented Nov 19, 2024 at 12:29
Add a comment  | 

1 Answer 1

Reset to default 0

To create SharePoint list, you need to make use of ListProp instead of List:

class Program
{
    private static async Task Main(string[] args)
    {
        string tenantId = "TenantID";
        string clientId = "ClientID";
        string clientSecret = "ClientSecret";

        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
        var graphClient = new GraphServiceClient(clientSecretCredential);

        var requestBody = new Microsoft.Graph.Models.List
        {
            DisplayName = "Ruklist",
            Columns = new List<ColumnDefinition>
            {
                new ColumnDefinition
                {
                    Name = "Author",
                    Text = new TextColumn { }
                },
                new ColumnDefinition
                {
                    Name = "PageCount",
                    Number = new NumberColumn { }
                }
            },
            
            ListProp = new ListInfo
            {
                Template = "genericList"
            }
        };

        try
        {
            // Replace with your actual Site ID
            var result = await graphClient.Sites["SiteID"].Lists.PostAsync(requestBody);
            Console.WriteLine("List created successfully: " + result.DisplayName);
        }
        catch (ODataError odataError)
        {
            Console.WriteLine($"OData Error Message: {odataError.Error.Message}");
            Console.WriteLine($"OData Error Code: {odataError.Error.Code}");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error creating list: " + ex.Message);
        }

    }
}

I am able to create the list successfully:

本文标签: cAzure function app GraphServiceClient create listStack Overflow