Share via

File name is getting changed when set 'FileLeafRef' while creating file in CSOM

Yuvraj Patil 441 Reputation points
2023-08-22T06:09:48.3733333+00:00

Hello,

I have application where I am creating file on SharePoint Doc lib using CSOM. For normal file (a.txt where name exist for file), code works fine. However when file has no extension(e.g. ".abc",".1") then its working differently, after file upload the final file name is ".abc.abc" instead of ".abc". In code I am setting "FileLeafRef" due to some requirement. Why this is working different in case file has no extension? and how to solve it?

Sample Code:

var siteCollUrl = "<SiteCollURL>";

            using (ClientContext clientContext = new ClientContext(siteCollUrl))
            {                
                clientContext.Credentials = new SharePointOnlineCredentials("username", "password as secure string");

                var web = clientContext.Web;
                var site = clientContext.Site;
                clientContext.Load(web, w => w.Webs);
                clientContext.ExecuteQuery();

                var list=web.Lists.GetByTitle("doc lib 5");              

                try
                {
                    var uploadFilePath = @"C:\Users\firstname.lastname\Desktop\.1";
                    var fileCreationInfo = new FileCreationInformation
                    {
                        Content = System.IO.File.ReadAllBytes(uploadFilePath),
                        Overwrite = true,
                        Url = Path.GetFileName(uploadFilePath)
                    };

                    var destFile = list.RootFolder.Files.Add(fileCreationInfo);
                    //Due to some other requirement I need to set FileLeafRef to the same file name.
                    destFile.ListItemAllFields["FileLeafRef"] = Path.GetFileName(uploadFilePath);
                    destFile.ListItemAllFields.Update();
                    clientContext.ExecuteQuery();
                }
                catch(Exception ex)
                {
                }
            }

Output:

Screenshot

Thanks in advance!

Yuvraj.

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments

Answer accepted by question author

Anonymous
2023-08-22T07:56:33.52+00:00

Hi @Yuvraj Patil

When uploading a file without an extension using the SharePoint CSOM (Client Object Model), you may experience issues with SharePoint's behavior of appending the default extension to the file name. To work around this issue, you can use a workaround by temporarily adding a dummy extension during the upload and then removing it after the upload.

Here's how to upload a file without an extension using CSOM:

(This is just a code template, and the code needs to be modified according to your scenario)

using Microsoft.SharePoint.Client;
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite";
        string libraryName = "Documents";
        string uploadFilePath = @"C:\Path\To\Your\File\file_without_extension";

        using (ClientContext context = new ClientContext(siteUrl))
        {
            context.Credentials = new SharePointOnlineCredentials("username", "password");

            Web web = context.Web;
            context.Load(web, w => w.Lists);
            context.ExecuteQuery();

            List library = web.Lists.GetByTitle(libraryName);

            string fileName = Path.GetFileName(uploadFilePath);
            string dummyExtension = ".tmp";

            // Add dummy extension if file has no extension
            if (!Path.HasExtension(fileName))
            {
                fileName += dummyExtension;
            }

            FileCreationInformation fileInfo = new FileCreationInformation
            {
                Content = File.ReadAllBytes(uploadFilePath),
                Overwrite = true,
                Url = fileName
            };

            Microsoft.SharePoint.Client.File newFile = library.RootFolder.Files.Add(fileInfo);
            context.ExecuteQuery();

            // Remove dummy extension from URL if added
            if (!Path.HasExtension(fileName) && newFile.Exists)
            {
                string updatedFileName = Path.ChangeExtension(fileName, null); // Remove the dummy extension
                newFile.MoveTo(updatedFileName, MoveOperations.Overwrite);
                context.ExecuteQuery();
            }

            Console.WriteLine("File uploaded successfully.");
        }
    }
}

Here is a link for your reference:

https://stackoverflow.com/questions/29812845/c-sharp-upload-file-with-no-extension


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.

Best Regards

Cheng Feng

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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