My knowledge of the Client Object model is fairly limited, although I have been able to remove and move existing web-parts on a particular page. But I'm now struggling to find and then add a custom web part programmatically using the SharePoint Client Object Model.

The web-part I'm trying to find resides in the Miscellaneous category. I know this by clicking "Page"-->"Edit Page"-->then clicking "Add a Web Part" in one of the zones. Two list boxes appear on the page: one with a heading Categories and another with a heading Web Parts.

So I'm trying to get a web-part in a category named "Miscellaneous" and then I would like to add it to the right hand side of the default.aspx of my sharepoint site.


We can do it using Client Object model, to do it, we need the xml file of the webpart you need to add. You can get it by the following method,

Step 1: Goto Site settings-> Web part Gallery.

Step 2: Then choose the needed web part, click edit -> click view XML -> copy the XML, then put it in the below code.

ClientContext clientconteext = newy ClientContext("http://servername/sites/sitename/");
Microsoft.SharePoint.Client.File page = clientcontext.Web.GetFileByServerRelativeUrl("/Sites/sitename/SitePages/default.aspx");
LimitedWebPartwrtManager lwp= page.GetLimitedWebPartManager(PersonalizationScope.Shared);

string webpartxml = /*The webpart XML */;
WebPartDefinition wpd = lwp.ImportWebPart(webpartxml);
wpm.AddWebPart(wpd.WebPart, "Right", 1);

Hope this helps you :)

  • Was it not the answer.? Oct 17 '13 at 12:49
  • 1
    It was _I'm sure I ticked it a yesterday
    – Paul
    Oct 17 '13 at 14:07
  • I can't see 'Web Part Gallery' when I look in site settings
    – Bassie
    Feb 12 '18 at 1:06
  • 1
    Look for “Web parts” Jan 13 '19 at 6:58

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