5

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.

0

1 Answer 1

12

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);
clientcontext.ExecuteQuery();

Hope this helps you :)

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.