I get the following error when trying to create a folder:

Invalid file name.

The file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file.

I am trying to run the following code:

string folderName = "teststring";
SPList oList = web.Lists[Constants.List];
if (!oList.EnableFolderCreation)
    oList.EnableFolderCreation = true;
SPListItem folder = oList.Items.Add(List.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);

This seems like pretty basic code and has worked for me in the past. I am running this on my dev machine as the system account. Is there anything basic I'm missing? I'm very confused as to why I can't make a simple folder anymore.

  • The list variable changing midway is a typo I persume? Oct 10 '13 at 5:19
  • Yes, sorry, I tried to change it to make it more readable. I changed everything to oList.
    – user11457
    Oct 10 '13 at 13:55
SPListItem folder = oList.Items.Add(List.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);

should be

SPListItem folder = oList.Items.Add(oList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
  • Well, this goes to show that I should just leave my messy variable names in when pasting code here. By a weird stroke of luck, this is actually the problem. I was going to say that this was a typo again, but I looked at my code and I was accidentally referencing a different list.
    – user11457
    Oct 10 '13 at 14:36
  • I suggest you to name your variables like this; SPList listRegions; SPList listCities; etc..
    – ozdogan
    Oct 10 '13 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.