Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 http://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

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted
+100

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.

share|improve this answer
    
thanks, do you mean parse out the web part by extracting the source code of entire page via an ajax request ? If so then I think this is prone to error as some web parts will be referencing .css styles so they display properly which is not included in the web part ? –  user470184 May 13 '13 at 8:28
    
Yes, that's what I'm proposing. It really depends on what that Web Part contains. Can you be any more specific? –  Marc D Anderson May 13 '13 at 11:33
    
it could contain an image, or formatted text, tables etc. I think this solution is too 'hacky' as its dependent on the source of the web part never changing. I could extract the webpart using the web part id, but how do I know this will never change in the future. I'm happy to know that the web-part cannot be exposed as a web service, and if it cannot be exposed as a service then a list should be used instead of web-part. –  user470184 May 13 '13 at 11:49
    
It sounds like it is a CEWP? If you want to grab an image or something out of it, you'd still have to parse the content if you did have a Web Service to call. If you write the script right, then you can handle errors gracefully if the Web Part is no longer there or if the element(s) you expect are missing. –  Marc D Anderson May 13 '13 at 14:52
add comment

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:

http://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

share|improve this answer
add comment

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.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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