How to read WebPart Content using SharePoint Client OM

comsharepointweb-parts

I'm pretty new in SharePoint development and trying to read the content of a WebPart using SharePoint 2010 Client Object Model. Here is what I'm doing to query the WebPart content:

ClientContext ctx = new ClientContext("http://foo");
File home = ctx.Web.GetFileByServerRelativeUrl("/SitePages/Page.aspx");
var wpm = home.GetLimitedWebPartManager(PersonalizationScope.Shared);

var query = wpm.WebParts.Include(wp => wp.Id, wp => wp.WebPart));
var webPartDefenitions = ctx.LoadQuery(query);

ctx.ExecuteQuery();

The problem is, what I get as result is the list of all the WebParts which simply includes ID and some basic information of that WebPart (such as Title), but I need to read the XML content of the WebPart itself instead of reading basic definition of that WebPart.

What is the best way to read a WebPart's content?

Thanks for your help in advance.


PS.

According to Trikks' comment, I checked the webpart content. It seems that the content is not bound to any sort of list or library. There are some custom data properties in there. Here is the sample:

<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="NS.SPS2010.Internet.WebParts.VenueDetails.VenueDetails, NS.SPS2010.Internet.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=89259c78cb06b3885" />
    </metaData>
    <data>
      <properties>
        <property name="WhenDetail" type="string">Monday 12:PM</property>
        <property name="WhereDetail" type="string">London</property>
      </properties>
    </data>
  </webPart>

What I'm really looking for, is accessing the values of WhenDetail and WhereDetail properties.

Best Answer

According to Wictor Wilén,

The Client Object Model is fairly limited when it comes to working with Web Parts. Basic operations such as adding and removing Web Parts can be done as well as changing some default properties of the Web Part (such as Title). There’s no access to custom Web Part properties.

There’s no access to custom Web Part properties. There’s no way to access the custom properties or methods on a Web Part. If you need to do this kind of customization with remote clients, you have to add your own remote API to SharePoint.

For more details please click Here.

To get the required details of the webpart I used a combination of both methods. First, I fetched the list of web part details using the following code:

ClientContext ctx = new ClientContext("http://foo");
File home = ctx.Web.GetFileByServerRelativeUrl("/SitePages/Page.aspx");
var wpm = home.GetLimitedWebPartManager(PersonalizationScope.Shared);

var query = wpm.WebParts.Include(wp => wp.Id, wp => wp.WebPart));
var webPartDefenitions = ctx.LoadQuery(query);

ctx.ExecuteQuery();

Then called the GetWebPart2 method of Webpartpages web service to download the content:

var client = new WebPartPagesWebService();
client.Url = siteRootAddress+"/_vti_bin/Webpartpages.asmx";
client.Credentials = credential;
// webPartId is a property of WebPart Defenition from the above code
var webPartXmlString = client.GetWebPart2(pageAddress, 
                                               webPartId, 
                                               Storage.Shared,
                                               SPWebServiceBehavior.Version3);

var webPartNode = XElement.Parse(webPartXmlString);
Related Topic