How to Create a Sharepoint in-build Web service that can be called by Silverlight

Source: Internet
Author: User

After Silverlight is introduced into SharePoint development, many times SL needs to call the SP backend Service to achieve data interaction, of course, we can deploy another set of services dedicated to data interaction through the SharePoint Object Model on IIS. The Silverlight client only needs to call this service, but such a service is separated from the SharePoint context, we cannot directly obtain related information through the spcontext object, and maintain a service site separately, which also increases complexity.

This section describes how to configure a Sharepoint built-in Web service that can be called by Silverlight.

First, you must create a Web Service

1. Open Visual Studio 2008 and create a project inbuildcustomizationservice for ASP. NET web service application.

2. Complete your customizationservice. asmx. CS FileCode.

3. Add a strong name for your project DLL. If other assemblies are introduced in your web service, name these assemblies separately.

4. Add all the assemblies to GAC:

In the vs 2008 command window, enterGacutil.exe-if "<full file system path to DLL>".

5. Use refactor and other decompilation software to obtain information about customizationservice. dll, open customizationservice. asmx, and change its content

  <% @ WebService Language = " C # " Class = " Inbuildcustomizationservice. customizationservice, inbuildcustomizationservice, version = 1.0.0.0, culture = neutral, publickeytoken = 30d08361d816be91 "   %>

 

The red part is from refactor, Inbuildcustomizationservice. customizationservice is the class name,InbuildcustomizationserviceIs the Assembly name.

6. copy customizationservice. asmx to "\ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ layouts", this path maps to the http // site/_ layouts /, in the vs 2008 command window, enter

Disco http: // myserver/_ layouts/customizationservice. asmx

Copy the generated customizationservice. disco and customizationservice. WSDL files to "\ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ layouts ."

7. Add some SharePoint class references, that is, <? XML version = "1.0" encoding = "UTF-8"?> Replace

Code

<% @ Page Language = " C # " Inherits = " System. Web. UI. Page "   %>  
<% @ Assembly name = " Microsoft. Sharepoint, version = 12.0.0.0, culture = neutral, publickeytoken = 71e9bce111e9429c "   %>  
<% @ Import namespace = " Microsoft. Sharepoint. Utilities "   %>  
<% @ Import namespace = " Microsoft. SharePoint "   %>
<% Response. contenttype =   " Text/XML " ; %>

 

8. Modify customizationservice. disco and replace contract ref and soap with dynamic URLs.

Code

  < Contractref Ref = <% Sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl (request) +   " ? WSDL " ), Response. output ); %> Docref = <% Sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl (request), response. output ); %> Xmlns = " Http://schemas.xmlsoap.org/disco/scl/ "   />
< Soap address = " <% Sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl (request), response. Output); %> "   Xmlns: Q1 = " Http://tempuri.org/ " Binding = " Q1: customizationservicesoap " Xmlns = " Http://schemas.xmlsoap.org/disco/soap/ "   />
< Soap address = " <% Sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl (request), response. Output); %> "   Xmlns: Q2 = " Http://tempuri.org/ " Binding = " Q2: customizationservicesoap12 " Xmlns = " Http://schemas.xmlsoap.org/disco/soap/ "   />

 

9. Modify customizationservice. WSDL and replace the soap address with a dynamic URL.

 

< Soap: address location = <% Sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl (request), response. output ); %>   />

 

10. Rename customizationservice. WSDL and customizationservice. Disco to. aspx, for example, customizationservicewsdl. aspx. The. WSDL or. disco file type cannot be recognized by Sharepoint in layout.

After completing the preceding operations, you can go to http: // yoursite/_ layouts/customizationservice. the Web Service is called under asmx, but I found that such a WebService still cannot obtain the spcontext object when called by the Silverlight client. the spcontext used in asmx returns a null value, so you can only change the location of the release, just like the Web service-list.asmx for Sharepoint built-in access list. deploy asmx to http: // yoursite/_ vti_bin.

11. copy customizationservice. asmx, customizationservicewsdl. aspx, customizationservicedisco. modify the spdisco In the ISAPI directory under "\ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ ISAPI. add the aspx File

Code

< Contractref Ref = <% Sphttputility. addquote (sphttputility. htmlencode (spweb. url +   " /_ Vti_bin/customizationservice. asmx? WSDL " ), Response. output ); %> Docref = <% Sphttputility. addquote (sphttputility. htmlencode (spweb. url +   " /_ Vti_bin/customizationservice. asmx " ), Response. output ); %> Xmlns = " Http://schemas.xmlsoap.org/disco/scl/ "   />
< Discoveryref Ref = <% Sphttputility. addquote (sphttputility. htmlencode (spweb. url +   " /_ Vti_bin/customizationservice. asmx? Disco " ), Response. output ); %> Xmlns = " Http://schemas.xmlsoap.org/disco/ "   />

 

In this way, you can directly use vs to add this WebService as the Web reference.

After completing the preceding steps, restart IIS and enable "http: // yoursite/_ vti_bin/customizationservice. asmx" to access the deployed service. The spcontext object corresponds to the current context.

12. Add a Silverlight application and add http: // yoursite/_ vti_bin/customizationservice. asmx as the Web reference. An error occurs when the service is accessed asynchronously. Check that Silverlight has cross-origin access problems. Create a cross-origin access protocol file clientaccesspolicy. XML, the content is as follows, copy it to the root directory of yoursite on IIS, similar to "C: \ Inetpub \ wwwroot \ WSS \ virtualdirectories \ yoursite.

Code

<? XML version = " 1.0 " Encoding = " UTF-8 "   ?>
< Access - Policy >
< Cross - Domain - Access >
< Policy >
< Allow - From HTTP - Request - Headers = " * " >
< Domain URI = " * " />
</ Allow - From >
< Grant - To >
< Resource include - Subpaths = " True " Path = " / " />
</ Grant - To >
</ Policy >
</ Cross - Domain - Access >
</ Access - Policy >

 

After completing the preceding operations, we can find that the developed Silverlight client can securely access SharePoint data through a custom in-build SharePoint service.

Add a demo result diagram:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.