Introduction

Web parts are the building blocks of the pages by which you can modify the content, appearance and behavior of pages of a SharePoint site.
SharePoint-2013-Hosting1
In this blog we are going to discuss how to Add, Update and Remove the web part using csom. Here I have used Content Editor Web Part in Wiki page.

Adding the web part in a wikipage

Step 1
First set the credentials by providing the user credentials.
Step 2
Using the client context, load the web.
Step 3
Get the object of the file using server relative url of the wiki page in which you want to add the web part. Example (/sites/Team Site/Site Pages/wikiPage.aspx).
Step 4
Get the ‘limitedWebPartManager‘ by using object file.
Step 5
Design the Xml string for importing the web part as definition of web part in ‘limitedWebPartManager’. In xml I have added the Title of web part as ‘My Web Part’.
Step 6
Then add the web part order and left zone of the page.
Follow the below code snippet,

In the below image webpat is added in a wikipage,

Updating the web part in a wikipage

Step 1
First set the credentials by providing the user credentials.
Step 2
Using the client context, load the web.
Step 3
Get the object of the file using the server relative URL of the wiki page in which you want to add the web part. Example (/sites/team Site/Site Pages/wikiPage.aspx).
Step 4
Get the ‘limitedWebPartManager‘ by using object file.
Step 5
Load the web part using LINQ query expression to return only the title of each Web Part.
Step 6
Check the page having at least one web part for updating.
Step 7
Get the web part definition by using the order value.
Step 8
Change the web part Title then save the web part by calling SaveWebPartChanges()‘.
Follow the below code snippet,

In the below image web pat title is update in a wikipage,

Removing the web part from a wikipage

Step 1
First set the credentials by providing the user credentials.
Step 2
Using the client context and, load the web.
Step 3
Get the object of the file using server relative URL of the wiki page in which you want to add the web part. Example (/sites/team Site/Site Pages/wikiPage.aspx).
Step 4
Get the ‘limitedWebPartManager‘ by using object file.
Step 5
Load the web part.
Step 6
Check the page having at least one web part for deleting.
Step 7
Get the web part definition by using the order value.
Step 8
Delete the web part by calling ‘DeleteWebPart ()‘.
Follow the below code snippet,
In the below image web pat is deleted from the wikipage,