Code: nameAlreadyExists Message: A file with the same name is currently being uploaded. Change the filename and try to save again.

Rakesh Bhavsar 21 Reputation points
2022-11-30T06:49:39.263+00:00

Hello MS Graph API Support Team,

Scenario: Uploading file abc.xlsx from local machine to SharePoint sub directory path where file with same name and extension abc.xlsx is already exists. Trying to replace/overwrite it.

I am trying to upload simple few KB abc.xlsx and or text file into SharePoint using MS Graph API .NET SDK using below line of code and it works fine if I upload file to SharePoint Site and Drive ROOT location, however same line of code is failing when I was trying to upload same file at SharePoint sub directory path where file with exactly same name and extension already exist. I am getting below error. Exactly same exception we have observed into File Upload into OneDrive function as well.

Exception1: "Code: nameAlreadyExists Message: A file with the same name is currently being uploaded. Change the filename and try to save again."

Exception2: "Code: nameAlreadyExists Message: Name already exists"

Exception3: "Code: timeout Message: The request timed out." - This we have handled using retry logic with along with Thread.Sleep(5000);

Line of code has an issue: Kindly suggest why it is intermittently throwing such exception or suggest better approach to do smooth file upload with consideration of large file upload too.

string fileToUploadPath = "D:\Test\";
string fileName = "abc.xlsx";
string destinationSPPath = @"<SharePoint Sub Directory relative path>"; //e.g. @"Test"

var diSearched = await graphClient.Sites[siteID].Drives[driveID].Root.ItemWithPath(destinationSPPath).Request().GetAsync();
if (diSearched != null)
{
var fileItem = new DriveItem
{
Name = fileName,
File = new Microsoft.Graph.File(),
AdditionalData = new Dictionary<string, object>()
{
{ "@microsoft.graph.conflictBehavior", "replace" }
}
};

bool isSPDirFound = false;  
DriveItem diChildren = null;  
try  
{  
    // NOTE: Below linke was working earlier and suddenly throwing "Name Already Exist" exception, hence adding ItemWithPath approach  
    diChildren = await graphClient.Sites[siteID].Drives[driveID].Items[diSearched.Id].Children.Request().AddAsync(fileItem);  
    isSPDirFound = true;  
}  
catch (Exception ex)  
{  
    // ECEPTION: ""  
}  

// Use properties to specify the conflict behavior in this case, replace  
var diUploadProps = new DriveItemUploadableProperties  
{  
    AdditionalData = new Dictionary<string, object>  
    {  
        { "@microsoft.graph.conflictBehavior", "replace" }  
    }  
};  

UploadSession uploadSession = null;  
if (diChildren == null)  
{  
    // NOTE: Below line of code is working fine while uploading abc.xlsx at given SP ROOT as well as SubDir path though file with same name present at that location. However sometime this line of code also throws "Name Already Exist" exception. How to handle this?  
    uploadSession = await graphClient.Sites[siteID].Drives[driveID].Root.ItemWithPath(destinationSPPath + "/" + fileName)  
        .CreateUploadSession().Request().PostAsync();  
}  
else  
{  
    uploadSession = await graphClient.Sites[siteID].Drives[driveID].Items[driveItemToUpload.Id]  
        .CreateUploadSession().Request().PostAsync();  
}  

using (FileStream fileStream = new FileStream(fileToUploadPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))  
{  
    var largeFileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream);        
    bool isResumeUpload = false;  
    try  
    {  
        uploadResult = await largeFileUploadTask.UploadAsync();  
        isStatus = uploadResult.UploadSucceeded;  
    }  
    catch (ServiceException se)  
    {  
        // Resume or retry uploads that fail due to connection interruptions or any 5xx errors, including:  
        // 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable & 504 Gateway Timeout  
        if ("500".Equals(se.StatusCode) || "502".Equals(se.StatusCode)  
            || "502".Equals(se.StatusCode) || "502".Equals(se.StatusCode))  
        {  
            isResumeUpload = true;  
        }  
        else  
            throw (se as Exception);  
    }  

    // Resume upload  
    if (isResumeUpload)  
    {  
        Thread.Sleep(2000);// Litle wait to get Network get up  
        try  
        {  
            uploadResult = await largeFileUploadTask.ResumeAsync();  
            isStatus = uploadResult.UploadSucceeded;  
        }  
        catch (ServiceException se2)  
        {  
            throw (se2 as Exception);  
        }  
    }  
}  

}

Kindly suggest a root cause as well as better approach!

Thank you and regards,
Rakesh

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,582 questions
0 comments No comments
{count} votes