Customize SharePoint WebServices to retrieve attachments

Source: Internet
Author: User

WebServices provided by Sharepoint do not directly obtain the attachment content. They both provide the attachment address and use WebClient to carry the identity. This may cause some IIS configurations to be blocked.

Create a website using the ASP. NET web service template, delete the file under app_code, and then add a class library project. The CS file code is as follows:

Code
1 using system;
2 using system. Web;
3 using system. Web. Services;
4 using system. Web. Services. Protocols;
5 using Microsoft. SharePoint;
6 using Microsoft. Sharepoint. utilities;
7 using system. IO;
8 [WebService (namespace = "http://tempuri.org/")]
9 [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
10 public class service: system. Web. Services. WebService
11 {
12 Public Service ()
13 {}
14
15 [webmethod]
16 public object [] getattachments (string listname, int Itemid)
17 {
18 spweb myweb = spcontext. Current. Web;
19 splist mylist = myweb. Lists [listname];
20 splistitem myitem = mylist. getitembyid (Itemid );
21 int attcount = myitem. attachments. count;
22 string [] myatturl = myitem. attachments. urlprefix. Split ('/');
23 string attachurl = string. empty;
24 For (INT I = 3; I <myatturl. length; I ++)
25 {
26 attachurl + = "/" + myatturl [I];
27}
28
29 object [] attachmentsobject = new object [attcount * 2];
30 For (INT I = 0; I <attcount; I ++)
31 {
32 attachmentsobject [I] = myitem. attachments [I];
33 spfile myfile = myweb. GetFile (attachurl + myitem. attachments [I]);
34 attachmentsobject [attcount + I] = myfile. openbinary ();
35}
36 return attachmentsobject;
37}
38}

After compiling the class library, copy the DLL file to GAC and change the asmx file content to <% @ WebService Language = "C #" class = "service, spattachments, version = 1.0.0.0, culture = neutral, publickeytoken = 890647b58f8f1295 "%>

In this step, WebServices should be able to call for trial run in the solution.

Use the Vs. Net command line tool to generate the disco and WSDL files. The command is as follows:

Disco http ://Localhost:Port/Project_name/Service1.asmx

The port is automatically allocated by. Net debugging, and then the disco and WSDL files are found in the command line tool directory.

The two files are changed to attachmentsdisco. aspx and attachmentswsdl. copy aspx to the \ 12 \ ISAPI directory and attachments. the asmx page is also copied to this directory. The code in disco can directly copy other similar webpages in this directory. aspx will set the header and <WSDL: service name = "service"> the code segment can be changed to a similar webpage format. Find the spdisco. aspx file in the directory and add the following two lines before </discovery>:

<Contractref ref = <% sphttputility. addquote (sphttputility. htmlencode (spweb. url + "/_ vti_bin/attachments. asmx? WSDL "), response. output); %> docref = <% sphttputility. addquote (sphttputility. htmlencode (spweb. URL + "/_ vti_bin/attachments. asmx "), response. output); %> xmlns = "http://schemas.xmlsoap.org/disco/scl/"/>
<Discoveryref ref = <% sphttputility. addquote (sphttputility. htmlencode (spweb. url + "/_ vti_bin/attachments. asmx? Disco "), response. Output); %> xmlns =" http://schemas.xmlsoap.org/disco/ "/>
Restart IIS. The problem I encountered is that sometimes the method in the class library is not updated when it is re-deployed ...... Still under study

Source code

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.