Share via


SharePoint 2010: How to upload documents to SharePoint sites using web services

Recently someone requested me to prepare something that will allow uploading of files to sharepoint document libraries from their machine using web services.  Hence I have created the following application. It works in both versions of sharepoint MOSS 2007 and SPS 2010. This takes following 3 input values during runtime:

  1. FilePath - This is the first input value to be provided. This is the complete file path including your file name and extension.
  2. Site URL- Here you need to provide the URL of the site where you want to upload your document. A point to remember here is that while providing URL make sure that you enter a valid URL and do not enter "/" at the end of the site. For example it should in the format http://webApplication/Site/Web and not in http://webApplication/Site/Web.
  3. DocumentLibrary - Here you need to enter the name of the document library in the site where you want to upload the document. If you want to upload it in any folder inside the document library then it should be in the format dLibrary/FolderName.

At the end of the execution you will get a message regarding the process completion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
 
namespace RahulUploadFileInSharepointWebService
{
    class Program
    {
        static void  Main(string[] args)
        {
            try
            {
                Console.WriteLine("This tool will upload the file from your PC to the Site");
                Console.WriteLine("Please enter the path of your file includin file name and extension");
                String sourceFilePath = Console.ReadLine();
                Console.WriteLine("Please enter the URL of the Site where you want to uplaod this file");
                String destinationSite = Console.ReadLine();
                Console.WriteLine("Please enter the Name of the document library where you want to upload the file");
                String destinationLibrary = Console.ReadLine();
                RahulCopy.Copy rahulCopy = new  RahulUploadFileInSharepointWebService.RahulCopy.Copy();
                rahulCopy.UseDefaultCredentials = true;
                rahulCopy.Url = destinationSite + "/" + "_vti_bin/" + "Copy.asmx";
                FileInfo fInfo = new  FileInfo(sourceFilePath);
                String dest = destinationSite + "/" + destinationLibrary + "/" + fInfo.Name;
                RahulCopy.CopyResult result1 = new  RahulUploadFileInSharepointWebService.RahulCopy.CopyResult();
                RahulCopy.CopyResult result2 = new  RahulUploadFileInSharepointWebService.RahulCopy.CopyResult();
                RahulCopy.CopyResult[] ResultCopy = { result1, result2 };
                RahulCopy.FieldInformation FieldInformation = new  RahulCopy.FieldInformation();
                RahulCopy.FieldInformation[] fieldsInformation = { FieldInformation };
                String[] destinationURLS = { dest };
                FileStream fileStream = new  FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);
                byte[] fileContent = new  byte[fileStream.Length];
                int length = (Int32)fileStream.Length;
                fileStream.Read(fileContent, 0, length);
                fileStream.Close();
                int outcome = (int)rahulCopy.CopyIntoItems(sourceFilePath, destinationURLS, fieldsInformation, fileContent, out  ResultCopy);
                if (outcome == 0)
                {
                    Console.WriteLine("The Operation is completed successfully");
                }
                else
                {
                    Console.WriteLine("The Operation is not completed successfully");
                }
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}