简体   繁体   中英

Cannot create a folder in a SharePoint Online List

Previous question: here closed by our heroic&pesky moderators who rush in to mark everything wrong, but once you edit it to meet their deviant needs, nobody votes to reopen it, because they already did their part...

So please, can someone try to explain me, how to properly create a Folder in SharePoint Online List?

I've tried to create it first using C# SDK, which did not work.

var folder = new DriveItem
{
Name = plan.Title,
Folder = new Folder()
};

await graphServiceClient
.Sites["ourdomain.sharepoint.com:/sites/ITOddeleni:"]
.Lists["Planner"]
.Drive
.Root
.Children
.Request()
.AddAsync(folder);

Then I started to play around with Graph Explorer, found out the ContentType ID for the folder and tried to create it as an item with ContentType specified.

This is the JSON Request Header, that resulted in creation of the folder in the list, but it has not name, as folder behaves differently in SharePoint than the item.

{
"Title" : "Test2",
"contentType": { "id": "0x012000FC4989A03C9F7845AD8C206E2F47A0FD" }
}

I've tried to change the "Title" for "Name", "FileLeafRef" and whatsoever could be Folders internal name for Title, but could not figure it out. It accepted all the request headers, but still the name of the created folder was blank.

Any help is appreciated.

在此处输入图片说明

EDIT:

I've tried Vadim's suggestion, and the folder really got created, and it seems on the 1st look that it carries the name you specify in the code. But also it carries some generated name, see the right side of the picture.

查看创建的文件夹

Once you open the folder, you loose your name until you refresh the page on the level where the folder sits. This is how it looks when you open the folder.

在此处输入图片说明

I have also tried to add name/Name besides the title, but it returns "Message: Field 'Name' is not recognied" both lower and uppercase.

Did You consider CSOM or it is not an option? SharePoint online best option would be to use CSOM PnP nuget link

The code should look something like this:


    try
    {
        string siteUrl = "SiteUrl";
        AuthenticationManager authManager = new AuthenticationManager();
        using (ClientContext context = authManager.GetWebLoginClientContext(siteUrl))
        {
            List list = context.Web.Lists.GetByTitle("LibName");
            context.Load(list);
            context.ExecuteQuery();
            Folder folder = list.RootFolder.CreateFolder("NewFolder");
        }
    }
    catch (Exception ex)
    {
        // log error
        throw;
    }

I already tested the above code with some console app and in my case the result was 在此处输入图片说明

I hope it will be of any help :)

It appears folder in a List could be created via Create item endpoint like this:

POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items
Content-Type: application/json

{
  "fields": {
    "Title": "Folder name goes here",
  },
  "contentType": { "id": "0x0120" }
}

where

  • folder properties such as Title are specified via ListItem.Fields property
  • to create an item of folder type, contentType property set to 0x0120 needs to be explicitly specified

C# example

var folderItem = new ListItem
{
    ContentType = new ContentTypeInfo()
       {
           Id = "0x0120"
       },
       Fields = new FieldValueSet()
               {
                    AdditionalData = new Dictionary<string,object>
                    {
                        {
                            "Title", folderName
                        }
                    }
                }
};

await graphClient
       .Sites.Root
       .Lists["<list name or id>"]
       .Items
       .Request()
       .AddAsync(folderItem);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM