Download files with accented characters using SharePoint Client Object Model

maxambrogi 6 Reputation points
2022-07-13T13:30:03.473+00:00

Hello,
by using SharePoint Client Object Model I'm not able to execute Microsoft.SharePoint.Client.File.OpenBinaryDirect() to open files files with accented characters (eg https://sharepoint/sites/root/doclib/carta_identità.pdf).

I always get 404 file not found in the following case:

1)use the FileRef property

2)use the value from the LinkingUrl property

3)use the HttpUtility.UrlEncode

Could you kindly help me to execute the download using the sharepoint client object model?

Best regards

Massimiliano

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint Server | Development
Developer technologies | C#
{count} vote

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,476 Reputation points Microsoft External Staff
    2022-07-14T07:31:28.967+00:00

    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

    https://support.microsoft.com/en-us/office/restrictions-and-limitations-in-onedrive-and-sharepoint-64883a5d-228e-48f5-b3d2-eb39e07630fa?ui=en-us&rs=en-us&ad=us#invalidfilefoldernames


    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.



  2. maxambrogi 6 Reputation points
    2022-08-30T13:59:29.167+00:00

    Hi @RaytheonXie_MSFT , sorry for the late feedback, thanks for the clarification, i have solved by search the document by id as the following

    List spList = context.Web.GetList(<libName>);
    context.Load(spList);
    CamlQuery query = new CamlQuery();
    query.ViewXml = String.Format("@<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{0}</Value></Eq></Where></Query></View>", <documentID>);
    ListItemCollection items = spList.GetItems(query);
    context.Load(items);
    context.ExecuteQuery();
    Microsoft.SharePoint.Client.File fileAccented = items[0].File;
    context.Load(fileAccented);
    ClientResult<Stream> stream = fileAccented.OpenBinaryStream();
    context.ExecuteQuery();
    using (var memoryStream = new MemoryStream())
    {
    stream.Value.CopyTo(memoryStream);
    spFiledata = memoryStream.ToArray();
    }

    Best regards
    Massimiliano

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.