
Hi @massimilianoambrogi-6236 ,
If the file has any of the supported special characters in it, then you will get the "file not found" error. So, a better way to get the reference to a file about whether it contains special characters or not is to use ResourcePath object.
You have to pass the file URL in decoded format. i.e. URLs as is.
Uri ExternalUrl = new Uri(ExternalSiteURL);
string extToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, ExternalUrl.Authority, TokenHelper.GetRealmFromTargetUrl(ExternalUrl)).AccessToken;
using(ClientContext extContext = TokenHelper.GetClientContextWithAccessToken(ExternalUrl.AbsoluteUri, extToken))
{
ResourcePath filePath = ResourcePath.FromDecodedUrl(docURLDecoded);
File newFile = extContext.Web.GetFileByServerRelativePath(filePath);
extContext.Load(newFile);
extContext.ExecuteQuery();
}
" * : < > ? / \ |
aren't allowed in file and folder names in OneDrive for home, OneDrive for work or school and SharePoint in Microsoft 365. Please don't use them in file folder name.
You can also refer to the document for details
https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/supporting-and-in-file-and-folder-with-the-resourcepath-api
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.