SharePoint Document Issues

How to Replace an Existing SharePoint Folder with the CSOM API

Rana GarciaRana Garcia Oct 22, 2026 869 views

Question details

The user needs to overwrite or replace an existing destination folder in SharePoint using the CSOM API, similar to the overwrite functionality found in Power Automate.

How to Replace an Existing SharePoint Folder with the CSOM API
Product
SharePoint
Device & OS
not provided
Scenario
Copying or moving folders programmatically in SharePoint where the destination folder already exists.
Observed behavior
The CSOM MoveCopyUtil.CopyFolderByPath method only provides the KeepBoth option via MoveCopyOptions and lacks a direct parameter to overwrite an existing folder.
Before you start

Ensure you have the latest SharePoint Client-Side Object Model (CSOM) SDK installed and that your API credentials have the necessary permissions to read, write, and delete folders in the target site.

Solution 1Recommended

Use a CSOM Delete and Re-Copy Workaround

Since MoveCopyOptions lacks an overwrite feature, manually check for the folder's existence, delete it if present, and then perform the copy operation.

The CSOM MoveCopyUtil.CopyFolderByPath method natively restricts behavior to KeepBoth. To mimic an overwrite action using purely CSOM, developers must implement a two-step programmatic approach to clear the destination before copying.

1
Check for folder existence

Use the Web.GetFolderByServerRelativeUrl method to check if the destination folder already exists in the target SharePoint library.

2
Delete the existing folder

If the folder exists, call the .DeleteObject() method on the folder object and invoke ExecuteQuery() to remove the existing destination folder.

3
Execute the copy method

Once the destination path is clear, execute MoveCopyUtil.CopyFolderByPath to copy the source folder to the now-empty destination.

Caution on Deletion: Deleting the destination folder will send it to the SharePoint Recycle Bin. Ensure that this behavior aligns with your organization's data retention policies before executing.
Free Microsoft Office alternative

Manage Your SharePoint Documents Seamlessly with WPS Office

While managing SharePoint folder structures programmatically requires developer APIs, editing the actual documents within those folders is effortless with WPS Office. As a lightweight, feature-rich office suite, it provides everything you need to view, create, and edit your professional documents without heavy subscription fees.

Fully compatible with Microsoft Word, Excel, and PowerPoint formats (.docx, .xlsx, .pptx) stored in SharePoint.Lightweight application that opens large documents quickly and efficiently.Familiar tabbed interface makes transitioning from other office software instant.Free core features for your daily document editing and formatting needs.
microsoft office alternative - wps office

Frequently Asked Questions

Does MoveCopyOptions in CSOM support overwriting folders?

No. Currently, the SharePoint CSOM MoveCopyOptions class only exposes the KeepBoth option, meaning it will retain both folders rather than replacing the destination.

Why does Power Automate allow folder overwriting while CSOM does not?

The SharePoint connector in Power Automate uses a different backend abstraction that explicitly handles the deletion or replacement logic for you within its 'Copy folder' action, bypassing the native CSOM limitation.

Where can I find official help for SharePoint REST API endpoints?

For detailed developer guidance, implementation syntax, and API limitations, you should visit the Microsoft Q&A community forums (learn.microsoft.com/en-us/answers) and tag your question with SharePoint Development.