The process of building custom applications and tools that interact with Microsoft SharePoint, including SharePoint Online in Microsoft 365.
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