2

Trying to upload a file to a folder in a SP Online where the title is "Forms - May" and set some of the fields. I can't seem to get the list or any of the fields. The list and the folder definitely exist. I can get other lists and folders without hyphens which is why I'm assuming this is the problem - is there a special way I need to handle this? I have tried to load the list into the client context which also breaks it. The specific error occurs when I try to get the fields in the list. This is wrapped in a try/catch and the exception I get is File Not Found

Web web = clientContext.Web;
List forms = web.Lists.GetByTitle("Forms - May");
string destination = "Forms - May/Folder1";

FileCreationInformation flciNewFile = new FileCreationInformation();
var targetFolder = clientContext.Web.GetFolderByServerRelativeUrl(dest);    
var uFolder = targetFolder.Files.Add(flciNewFile);

Microsoft.SharePoint.Client.File uploadFile = targetFolder.Files.Add(flciNewFile);

var listItem = uploadFile.ListItemAllFields;
Field field = forms.Fields.GetByInternalNameOrTitle("FieldInternalName");
clientContext.Load(field, f => f.TypeAsString);
clientContext.ExecuteQuery();  //breaks
2
  • try using relative path(url) of list ctx.Web.GetList("sites/dev/lists/Forms - May").Also where are you loading the list before access it?
    – user13408
    Jun 2, 2017 at 14:15
  • Does encoded list name work? i.e. Forms%20-%20May Jun 2, 2017 at 14:21

1 Answer 1

2

The error was in the destination. Editing the following lines worked:

List forms = web.Lists.GetByTitle("Forms - May");
string destination = "Forms  May/Folder1"; //kept 2 spaces

Looks like you can have a hyphen in the folder though... The following also works

List forms = web.Lists.GetByTitle("Forms - May");
string destination = "Forms  May/Folder1 - May";
2
  • Hi @cooper If you solved the problem stated in your original question, you should mark this answer as accepted answer to help future visitors. Jun 2, 2017 at 16:32
  • Hi @cooper i'm facing the same issue , cant able to add - symbol in folder name as well as cant get the folder by serverrelativeurl where the folder has the - symbol , i tried your code but it doesn't help me out can you give the clear idea on this
    – Esaki
    Jan 23, 2018 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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