1

Is it possible to get the content of a content webpart by using the client object model of SharePoint?

The usage scenario: I want to copy a site page (with content in content editor webparts) from one Sharepoint site to anonther site. That is why I use the client object model. Everything works fine, he copies the page to the other site; but when you open the aspx page in a browser, it is empty. Reason: because the content is in the database and not in the page.

EDIT: If someone knows how to export the page with the content in it; you may let me know also. If you export the aspx page with SharePoint Designer, you can get also the content of the content webparts in it. And if i am not wrong, SharePoint Designer also uses the Client Object Model.

  • To do what exactly, please clarify. – Eric Alexander Jun 19 '12 at 14:11
  • I want to copy a site page (with content in content editor webparts) from one sharepoint site (can be on premise or office 365) to anonther site (can also be on premise or office 365). That is why I use the client object model. Everything works fine, he copies the page to the other site; but when you open the aspx page in a browser, it is empty. Reason: because the content is in the database and not in the page... – Davy Jun 19 '12 at 14:16
  • I've edited your post to include that. – Eric Alexander Jun 19 '12 at 14:19
1

For the ContentEditorWebPart, the HTML content is actually an XmlElement called Content.

I found this post: http://blogs.technet.com/b/speschka/archive/2009/11/01/using-the-sharepoint-2010-client-object-model-part-6.aspx

Which talks about how to get webparts using the Client Object Model. Hopefully it can help you get going in the right direction.

EDIT:

I'm not sure that SP Designer exclusively uses the client object model. Using fiddler while editing a page in SP Designer, it seems that it is calling /_vti_bin/webpartpages.asmx and that web service is returning the html of the page including the properties of the webparts in xml format.

  • Thanks, but I already explored the LimitedWebPartManager and WebPartDefinition, but you can add new WebParts and check which webparts are on your page. But it is not possible to extract the content of the webpart. I am still wondering how Microsoft did it with SharePoint Designer. – Davy Jun 19 '12 at 15:08
  • I've updated my answer above with some new information about SP Designer. – Steve Lineberry Jun 19 '12 at 17:08
0

If this helps someone,

using(ClientContext ctx = new ClientContext("siteURL")) {  
  SecureString password = new SecureString();
  ctx.Credentials = new SharePointOnlineCredentials("onlineEmail", password);
  File file = ctx.Web.GetFileByServerRelativeUrl("/pages/default.aspx");
  LimitedWebPartManager wpManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
  Guid wpID= new Guid("{GUID}");
  ClientResult<string> wpXml= wpManager.ExportWebPart(wpID);
  ctx.ExecuteQuery();

  //Parse wpXml.Value to get Content Element
}

You can get WebPartID "GUID" from following code

var webParts = wpManager.WebParts; 
ctx.Load(webParts, wp => wp.Include(w => w.Id)); 
ctx.ExecuteQuery(); 
foreach (var wp in webParts) { Guid guid = new Guid(wp.Id ); } 

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.