I have been able to add a web part, thank to this post, using code like: limitedWebPartManager.ImportWebPart(webPartSchemaXml)

However now I'm not able to remove the Web Part anymore by Editing the Page, because the Web Part is not visible in Edit mode (incidentally I don't know why).

So I'm trying to Remove the Web Part programatically, but LimitedWebPartManager doesn't offer a method like RemoveWebPart.

1 Answer 1


Use DeleteWebPart method of WebPartDefinition class to delete web part from the page.


class Program

                private class Configuration
                                public static string ServiceSiteUrl = "https://<tenant>.sharepoint.com/sites/<site>";
                                public static string ServiceUserName = "<user>@<tenant>.onmicrosoft.com";
                                public static string ServicePassword = "<password>";

                static ClientContext GetonlineContext()
                                var securePassword = new SecureString();
                                foreach (char c in Configuration.ServicePassword)
                                var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
                                var context = new ClientContext(Configuration.ServiceSiteUrl);
                                context.Credentials = onlineCredentials;
                                return context;

                static void Main(string[] args)  
                                var ctx = GetonlineContext();
                                var page = ctx.Web.GetFileByServerRelativeUrl("/sites/<SiteCollection>/SitePages/<page>.aspx");
                                LimitedWebPartManager limitedWebPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
                                                wps => wps.Include(
                                                wp => wp.WebPart.Title));


                                if (limitedWebPartManager.WebParts.Count == 0)
                                                throw new Exception("No Web Parts to delete.");

                                WebPartDefinition webPartDefinition = limitedWebPartManager.WebParts[0];



Refer to:



(Though these articles are for SharePoint 2010, they still have good information for SharePoint Online.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.