SharePoint Client Object Model Error: The file or folder name contains characters that are not permitted. Please use a different name.

One of the big drawbacks of the SharePoint Client Object Model (CSOM) is that it has very little documentation. This error was thrown when I tried to create a new List using the class ListCreationInformation for a Developer Site. ListCreationInformation class has one property, Url. Now msdn says, it Gets or sets value that specifies the site-relative URL of the location for the new list. http://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.listcreationinformation.url.aspxI thought as with other cases, I am supposed to enter the serverRelativeUrl here. Hence I set a value something like sites/ParentWebUrl/ListRootFolderName. Now when I tried to run the code I got this error:: The file or folder name contains characters that are not permitted. Please use a different name.

Next, just for the sake of testing, I passed only the ListRootFolderName and, the code ran successfully! The List was successfully created with proper values.

I have only faced this issue for a Developer Site. I have tested this condition on Publishing Site, Blog, & Developer Site only.

Still following is a short list of characters that cannot be set as a file/folder names in SharePoint

Site names, subsite names, or site group names

You cannot use the following characters anywhere in a site name, in a subsite name, or in a site or Active Directory group name:

  • Tilde (~)
  • Number sign (#)
  • Percent (%)
  • Ampersand (&)
  • Asterisk (*)
  • Braces ({ })
  • Backslash (\)
  • Colon (:)
  • Angle brackets ()
  • Question mark (?)
  • Slash (/)
  • Plus sign (+)
  • Pipe (|)
  • Quotation mark (“)
  • You cannot start a site name, a subsite name, or a site group name with an underscore (_) character or with the period (.) character.
  • When you create a site name, a subsite name, or a site group name, you cannot use strings that were already used to name managed paths.
  • You cannot use the period character consecutively in the middle of a site name, a subsite name, or a site group name.
  • You cannot use the period character at the end of a site name, a subsite name, or a site group name.

Folder names

You cannot use the following characters anywhere in a folder name or a server name:
  • Tilde
  • Number sign
  • Percent
  • Ampersand
  • Asterisk
  • Braces
  • Backslash
  • Colon
  • Angle brackets
  • Question mark
  • Slash
  • Pipe
  • Quotation mark
  • You cannot use the period character consecutively in the middle of a folder name.
  • You cannot use the period character at the end of a folder name.
  • You cannot start a folder name with a period character.

File names

You cannot use the following characters anywhere in a file name:
  • Tilde
  • Number sign
  • Percent
  • Ampersand
  • Asterisk
  • Braces
  • Backslash
  • Colon
  • Angle brackets
  • Question mark
  • Slash
  • Pipe
  • Quotation mark
  • You cannot use the period character consecutively in the middle of a file name.
  • You cannot use the period character at the end of a file name.
  • You cannot start a file name by using the period character.
  • In addition, file names and folder names may not end with any of the following strings:
    1. .files
    2. _files
    3. -Dateien
    4. _bestanden
    5. _file
    6. _archivos
    7. -filer
    8. _tiedostot
    9. _pliki
    10. _soubory
    11. _elemei
    12. _ficheiros
    13. _arquivos
    14. _dosyalar
    15. _datoteke
    16. _fitxers
    17. _failid
    18. _fails
    19. _bylos
    20. _fajlovi
    21. _fitxategiak

Full details can be found here.

The remote server returned an error: (409) Conflict from SharePoint ClientObjectModel

Problem

I got this issue for ListItem having attachments. We’re initially creating a ListItem using the Update() & ExecuteQuery() method. Next when we tried to create an attachment for the same ListItem using ExecuteQuery(), this error was thrown.

Solution

Solution was to remove the ExecuteQuery() after the ListItem Update() and use a single ExecuteQuery() for both the ListItem and its Attachment.

using System;
using IO = System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;


string siteUrl = "http://servername:12345/";
SP.ClientContext ctx = new SP.ClientContext(siteUrl);
SP.List list = ctx.Web.Lists.GetByTitle("ListTitle");

SP.ListItemCreationInformation listItemCreationInfo = new SP.ListItemCreationInformation();
SP.ListItem oListItem = list.AddItem(listItemCreationInfo);
                
//This will create ListItem before adding attachments
oListItem.Update();

//The following line should be removed
//ctx.ExecuteQuery();


string strFilePath = "FullFilePath";

byte[] bytes = IO.File.ReadAllBytes(strFilePath);

using (IO.MemoryStream mStream = new IO.MemoryStream(bytes))
{
    SP.AttachmentCreationInformation aci = new SP.AttachmentCreationInformation();
    aci.ContentStream = mStream;
    aci.FileName = IO.Path.GetFileNameWithoutExtension(strFilePath);

    oListItem.AttachmentFiles.Add(aci);

    oListItem.Update();
    ctx.ExecuteQuery();
}

The error was thrown at the last line (ctx.ExecuteQuery() in bold). Actually the List was already created above (commented ctx.ExecuteQuery()). The solution was to remove that ExecuteQuery() and bundle both the requests, create ListItem & upload attachment, in a single ExecuteQuery().

NOTE:- Do not forget to mention the oListItem.Update() as the ListItem has to be created first before adding attachments to it. Just don’t immediately call the Execute method