2

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

1

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

Demo:

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)
                                {
                                                securePassword.AppendChar(c);
                                }
                                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);
                                ctx.Load(limitedWebPartManager.WebParts,
                                                wps => wps.Include(
                                                wp => wp.WebPart.Title));

                                ctx.ExecuteQuery();

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

                                WebPartDefinition webPartDefinition = limitedWebPartManager.WebParts[0];

                                webPartDefinition.DeleteWebPart();

                                ctx.ExecuteQuery();
                }
}

Refer to:

https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee539301(v%3Doffice.14)#deleting-a-web-part-from-a-page

https://www.c-sharpcorner.com/blogs/how-to-delete-a-webpart-using-client-object-model1

(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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.