1

I am trying to create a new folder within a document library. Actually, the code works well, but when the title of the new folder starts with a blank, I get an exception "File not found" and the folder is not created.

I already tried to encode the title: replaced the blach by "%20" and by "+". In this case the folder is created, but it contains other characters like"+" at the first position in the title.

I tried to create the folder " blankBefore" in the SharePoint application by using the browser - and it works. The folder is create and looks like this " blankBefore".

I can create folders with a blank in the title, but not, if the title of the folder starts with a blank.

public bool CreateFolder(SharePointNode spParentNode, string strFolderName)
{
    ClientContext localCTX = new ClientContext(spParentNode.ParentSite);
    ConectClient(localCTX);

    Folder newFolder = null;

    var folder = localCTX.Web.GetFolderByServerRelativeUrl(spParentNode.URL);
    localCTX.Load(folder);
    localCTX.Load(folder.Folders);
    Folder newFolder = folder.Folders.Add(strFolderName);
    newFolder.Update();
    localCTX.ExecuteQuery();

    return true;
}
MaxEMunich
  • 21
  • 3
  • Possible duplicate of [How to remove illegal characters from path and filenames?](https://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames) – kara Jul 26 '19 at 08:19
  • No, it is not about illegal characters in SharePoint file or folder names. Blank is a valid characters. And as already mentioned, it is possible to create a folder with such a title " blankBefore" in the SharePoint web-application. – MaxEMunich Jul 26 '19 at 08:23
  • Additional info: I am connecting SharePoint Online – MaxEMunich Jul 26 '19 at 08:24

1 Answers1

0

Create folder in SharePoint list (SharePoint 2010, 2013, 2016)

using (var clientContext = new ClientContext("http://sp/sites/test"))
            {
                string folderName = "test";
                var list = clientContext.Web.Lists.GetByTitle("ListBase");
                list.EnableFolderCreation = true;

                clientContext.Load(list);
                clientContext.Load(list.RootFolder);
                clientContext.Load(list.RootFolder.Folders);
                clientContext.ExecuteQuery();

                var folderCollection = list.RootFolder.Folders;

                foreach (var folder in folderCollection)
                {
                    if (folder.Name == folderName)
                    {
                        clientContext.Load(folder.Files);
                        clientContext.ExecuteQuery();
                    }
                    else
                    {
                        var itemCreateInfo = new ListItemCreationInformation
                        {
                            UnderlyingObjectType = FileSystemObjectType.Folder,
                            LeafName = folderName
                        };
                        var newItem = list.AddItem(itemCreateInfo);
                        newItem["Title"] = folderName;
                        newItem.Update();
                        clientContext.ExecuteQuery();
                        break;
                    }
                }
            }
Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19
  • Hi Fox, thank you for this answer. Using **list.AddItem()** and setting the title instead of **Folders.Add(title)** is a good idea. I will try it today. – MaxEMunich Jul 26 '19 at 13:14