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.
                }
            }
  • I have updated the question with code I have tried. – Shashikant K Jul 27 '15 at 11:27
  • This is similar to what you want: stackoverflow.com/questions/11814829/… – Akhoy Jul 27 '15 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? – Shashikant K Aug 4 '15 at 13:54
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
        }

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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