How to delete a webpart using client object model


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Microsoft.SharePoint;
using
Microsoft.SharePoint.Client;
using
Microsoft.SharePoint.Client.WebParts;


namespace
ClientObjectModel
{
class Program
{
static void Main(string[] args)
{
//How to delete a webpart using client object model

ClientContext context = new ClientContext("http://serverName:1111");
File file = context.Web.GetFileByServerRelativeUrl("/Shared%20Documents/Forms/AllItems.aspx");
LimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
context.Load(wpm.WebParts,
wps => wps.Include(
wp => wp.WebPart.Title));
context.ExecuteQuery();
foreach (WebPartDefinition wpd in wpm.WebParts)
{
WebPart wp = wpd.WebPart;
if (wp.Title == "My Updated Webpart title")
{
wpd.DeleteWebPart();
context.ExecuteQuery();
}
}
}
}
}





Ebook Download
View all
Learn
View all