Question

List data can be exposed via a web service but how can web parts be also exposed via a web service ?

I think this question is similar to https://stackoverflow.com/questions/7115858/sharepoint-2010-web-part-expose-web-service , the difference being I want to expose the content of the web part via a sharepoint web service call, is this possible ?

e.g : if the webpart contains an image, expose this image via sharepoint web service

Was it helpful?

Solution

I'm not sure how you would do this via a Web Service, if you even can. However, you can easily do it with client side script using AJAX. Just get the page with the Web Part in it, parse out the contents, and display it where you want in the current page.

OTHER TIPS

If its just about getting webpart content from an accessible page within same domain, I dont think you will need a webservice for it.

You can place your webpart within a div (id=container) and use below Jquery :

$('#result').load('sitepages/test.aspx #container');

When this method executes, it retrieves the content of sitepages/test.aspx, and then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

If you need to access content of webpart on page outside the domain, You use contents() jquery function as mention here:

https://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery

In any case, if you are determined to go for the webservice, you have to code for this. In the webmethod, you can parse the page and return the content of the html sections you need by using HTMLAgilityPack

A web part is for displaying content, not to provide a content repository for other components. A few options:

  1. Perhaps you don't need a web part at all, just create a list and store the image there.

  2. Perhaps you also need a web part. Create a list, store the image there, and reference that image from the web part. Other components could then also reference the same list.

  3. Perhaps there are multiple images available, but the web part is doing some logic to determine which is the "active" image to be displayed. In this case, either A) Move this logic to a custom web service and call this same web service from both the web part as well as other components, or B) Have the web part also update a custom list with the name/path of the current "active" image. Other components could read the custom list to get the image. (I like the idea of a custom web service better from an architectural basis, but it would be quite a bit more difficult to set up in SharePoint. Having a custom list that the web part updates seems to be not as clean, but would be far easier to set up.)

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top