How to: Magic with SharePoint 2003, uploading files using a Web service
Some time ago I blogged about my intentions of uploading files to a SharePoint Document Library site from a local folder using some kind of Web service. After doing some research and some tests, I found a very easy way to do that and now I want to share with you the approach I followed since you might find it useful as well.
Prerequisites
To build this solution, you need to install Office SharePoint Portal Server 2003 and follow the next steps:
- Create a Document Library
- Grant access to the users that will upload files to the Document Library:
To install the Web service:
- Download ODC_WritingCustomWebServicesSampleSPPT.EXE.
- Note: It helps a lot to read the following article: Writing Custom Web Services for SharePoint Products and Technologies
- Extract the download contents to your hard drive and Run build.bat.
- Note: This will install the Web service on the _vti_bin virtual directory inside the Default Web Site.
- Open the IIS Management Console (INETMGR) and navigate to the the _vti_bin virtual directory (inside the Default Web Site).
- Find SPFiles.asmx, right click, and Browse.
- Navigate to https://localhost/_vti_bin/SPFiles.asmx to validate if you have installed successfully the Web Service.
To consume the Web Service:
From your managed application, add a reference to the https://localhost/_vti_bin/SPFiles.asmx Web Service.
Call the Web service DocumentLoader.
Create a helper class to upload files. You can use the helper class I created.
using System;
using System.Net;
using System.IO;
using System.Configuration;
using System.Text;
using System.Web;
using System.Security;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DocumentLoader;/// <summary>
/// A sample SharePointHelper class to upload files
/// </summary>
public class SharePointUploadHelper {
private string _sharepointDocumentLibrary;public SharePointUploadHelper() {
_sharepointDocumentLibrary = ConfigurationManager.AppSettings["SharePointDocLibrary"];
}public string UploadDocumentsToSharePoint(string fileName) {
DocumentLoader.SPFiles svcDocLoader = new DocumentLoader.SPFiles();
svcDocLoader.PreAuthenticate = true;
svcDocLoader.Credentials = CredentialCache.DefaultCredentials;string strPath = fileName;
string strFile = strPath.Substring(strPath.LastIndexOf("\\") + 1);
string strDestination = _sharepointDocumentLibrary;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 result = svcDocLoader.UploadDocument(strFile, binFile, strDestination);return (result);
}
}Create a Web Form, Win Form, or console application that will require a user to upload files.
Call the UploadDocumentsToSharePoint method of the SharePointUploadHelper class, for example:
protected void btnLoadFile_Click(object sender, EventArgs e)
{
SharePointUploadHelper fh = new SharePointUploadHelper();
string serverTempFilePath = Server.MapPath(@"/yourApplication");
contentFileUpload.PostedFile.SaveAs(serverTempFilePath);
lblUploadResults.Text = fh.UploadDocumentsToSharePoint(serverTempFilePath);
}Open the configuration file (i.e. Web.config) and turn on impersonation.
<identity impersonate="true" />Add a configuration key that points to the SharePoint Document Library where you will upload files.
<add key="SharePointDocLibrary" value="https://myServerName/myDocumentLibrary"/>Build your application
Run and test the application, and there, a great web service.
I can tell you it works, just keep in mind the download is a code sample.
Enjoy!
-Erika