5

I have a Custom Webpart deployed in the Site, to which I need to retrieve the XML definition programmatically using Client side object model, can anybody help?

EDIT: I used the following code.

 string strSiteUrl = SiteUrl_txt.Text; using (ClientContext cContext = new ClientContext(strSiteUrl)) { //open Web Web oWeb = cContext.Web; cContext.Load(oWeb); File page = oWeb.GetFileByServerRelativeUrl("MySiteUrl"); LimitedWebPartManager limitedWebPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared); cContext.Load(limitedWebPartManager, wps => wps.WebParts.Include(wp => wp.WebPart.Properties, wp => wp.WebPart.Title, wp => wp.WebPart.TitleUrl)); cContext.ExecuteQuery(); foreach (WebPartDefinition wp in limitedWebPartManager.WebParts) { if (wp.WebPart.Properties.FieldValues.ContainsKey("UniqueValue")) { // Here I need to extract the XML definition, but not sure what should I // add here so that I could get the entire XML definition, like the one // we get when we export the webpart as XML in webpart gallery mannually. } } 
3
  • I have updated the question with code I have tried.CommentedJul 27, 2015 at 11:27
  • This is similar to what you want: stackoverflow.com/questions/11814829/…
    – Akhoy
    CommentedJul 27, 2015 at 12:14
  • @uberz91, Thanks. I did tried the same. I am getting SOAP exceptions. also there is no way we can export the XML programmatically using client side code?CommentedAug 4, 2015 at 13:54

1 Answer 1

1

Try this:

try { using (var clientContext = new ClientContext(siteUrl) { Web web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); var page = clientContext.Web.GetFileByServerRelativeUrl("/sites/sitecollection/site/Pages/Page.aspx"); clientContext.Load(page); clientContext.ExecuteQuery(); var webpartManager = page.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared); clientContext.Load(webpartManager); var webparts = webpartManager.WebParts; clientContext.Load(webparts); clientContext.ExecuteQuery(); foreach (var webpart in webparts) { clientContext.Load(webpart.WebPart.Properties); clientContext.ExecuteQuery(); var propertyValue = webpart.WebPart.Properties.FieldValues; if (propertyValue["Title"].Equals("My List Name")) { var xml = propertyValue["XmlDefinition"]; } } } } catch (Exception ex) { //do something } } 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.