Wednesday 26 October 2011

How to upload documents to sharepoint sites using Web Services

Hi ,

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();
            }
        }
    }
}

I hope this will help you out.

4 comments:

  1. I think you might need to change the naming convention to improve readability

    ReplyDelete
  2. This is what I have been searching for. But I need to upload the docs from VBA Macro. I don't have Visual Studio and .Net Fraemwork stuff and mine is testing environment.
    Could you give any alternate approaches.

    ReplyDelete
    Replies
    1. I am in the same scenario. And how ia can acces to your rahul... namespace?? where do you include the web services object??

      Delete