SharePoint Web Service series: Edit custom SharePoint Web Services 2

Source: Internet
Author: User
Tags response code
Next, we will add our web service to the WSS Web Service list, so that we can add reference to this Web Service in vs. net.

1. Open the spdisco. aspx file, which is located in the local_drive: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 60 \ ISAPI directory.

2. Add the following content to the Discovery element at the end of the file and save it.

<Contractref ref = <% response code. writehtmlencodewithquote (response, spweb. url + "/_ vti_bin/service1.asmx? WSDL ", '"'); %> docref = <% response code. writehtmlencodewithquote (response, spweb. URL + "/_ vti_bin/service1.asmx", '"'); %> xmlns =" http://schemas.xmlsoap.org/disco/scl/ "/>
<Soap address = <% response code. writehtmlencodewithquote (response, spweb. url + "/_ vti_bin/service1.asmx", '"'); %>
Xmlns: Q1 = "http://schemas.microsoft.com/sharepoint/soap/directory/" binding = "Q1: service1soap" xmlns = "http://schemas.xmlsoap.org/disco/soap/"/>

Note:In the binding attribute of a soap element, the text before "Soap" (binding = "Q1: service1soap" in this example) specifies the class name used to define the web service.

So far, our custom web service has been deployed. We can call our custom web service just like using the default web service.

Create Document Upload Web Service

We can use the above method to create a document upload Web service to upload documents to a WSS shared document library. The Service uses Service Virtualization to obtain the site context and then uploads documents to the specified document library.

Create a web service project named uploadsvc. Add a new Web service class named uploadfile.

Add a reference to Windows SharePoint Services (Microsoft. Sharepoint. dll.

Add the following web method to the uploadfile class:

[Webmethod]
Public String uploaddocument (string filename, byte [] filecontents, string pathfolder)
{
If (filecontents = NULL)
{
Return "null attachment ";
}
Try
{
Spweb site = spcontrol. getcontextweb (context );
Spfolder folder = site. getfolder (pathfolder );
String fileurl = filename;
Spfile file = folder. Files. Add (fileurl, filecontents );
Return file. timecreated. tolongdatestring () + ":" + file. title;
}
Catch (system. Exception ee)
{
Return ee. Message + ":" + ee. source;
}
}

Add the following namespace reference:

Using system. IO;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;

Compile the Web service project.

Create and modify. disco and. WSDL and modify spdisco. aspx. Replace service1 with uploadfile. Save as uploadfiledisco. aspx and uploadfilewsdl. aspx respectively.

Copy these files to _ vti_bin and copy the corresponding. DLL to _ vti_bin/bin.

Example of calling the Upload File Service

Create a new winform application, add a web reference, and name the reference wssserver.

Add a button and two textbox to the default form. One textbox is used to enter the path of the file to be uploaded, and the other is used to specify the document library to be uploaded. For example, http ://SERVER_NAME/Sites/Target_site/Document_library.

Add the following code to the Click Event of the button.

Wssserver. uploadfile svcdoclib = new wssserver. uploadfile ();
Svcdoclib. Credentials = credentialcache. defaultcredentials;

String strpath = textbox1.text;
String strfile = strpath. substring (strpath. lastindexof ("\") + 1 );
String strdestination = textbox2.text;
 
Filestream fstream = new filestream (strpath, system. Io. filemode. Open );
Byte [] binfile = new byte [(INT) fstream. Length];
Fstream. Read (binfile, 0, (INT) fstream. Length );
Fstream. Close ();
 
String STR = svcdoclib. uploaddocument (strfile, binfile, strdestination );
MessageBox. Show (STR );

Add namespace reference:

Using system. net;
Using system. IO;

Compile and run.
(End)

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.