Posted by: Shantham | March 3, 2009

Upload document from Local Machine to SharePoint Library using WebService


In my previous post i’m explained about the members of Copy Web Service. For uploading a document from any local machine to sharepoint document library is possible by using CopyInToItems method.

Though, the syntax for that method is,

public uint CopyIntoItems (string SourceUrl,string[] DestinationUrls,FieldInformation[] Fields,byte[] Stream,out CopyResult[] Results)

Parameters,

SourceUrl:
A String that contains the absolute source URL of the document to be copied.
DestinationUrls:
An array of Strings that contain one or more absolute URLs specifying the destination location or locations of the copied document.
Fields:
An array of FieldInformation objects that define and optionally assign values to one or more fields associated with the copied document.
Stream:
An array of Bytes that contain the document to copy using base-64 encoding.
Results:
An array of CopyResult objects, passed as an out parameter.
Return Value:
A UInt32 that returns 0 to indicate that the operation has completed. (There is also an out parameter containing an array of CopyResult objects.)

Below i’m providing a code for uploading a document to SharePoint Library,

//Copy WebService Settings
string webUrl = “http://localhost:1000”;
WSCopy.Copy copyService = new WSCopy.Copy();
copyService.Url = webUrl+”/_vti_bin/copy.asmx”;
copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

//Declare and initiates the Copy WebService members for uploading
string sourceUrl = “C:\\Sample.doc”;
string[] destinationUrl = { “http://localhost:1000/Shared Documents/Sample.doc” };
WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };
WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();
fFiledInfo.DisplayName = “Description”;
fFiledInfo.Type = WSCopy.FieldType.Text;
fFiledInfo.Value = “Sample Description”;
WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo };

//Reading the document contents in to stream
FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
byte[] fileContents = new Byte[strm.Length];
byte[] r = new Byte[strm.Length];
int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
strm.Close();

//Copy the document from SourceUrl to destinationUrl with metadatas
uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);

* Where WSCopy is the WebReference Name and the document is uploaded successfully, copyresult returns 0.

Read more…

Advertisements

Responses

  1. Thanks for the code! Helped me get to where I needed to be… here:

    Private Sub Upload(ByVal sourceFullPath As String, ByVal destinationDirectory As String)

    Using copyService As New SharePointSandboxCopyService.Copy
    copyService.Credentials = System.Net.CredentialCache.DefaultCredentials

    Dim destinationUri As New Uri(New Uri(destinationDirectory, UriKind.Absolute), Path.GetFileName(sourceFullPath))
    Dim destinationUrls As String() = {destinationUri.ToString}
    Dim results() As CopyResult = {}
    Dim fields() As FieldInformation = {}
    Dim fileContents() As Byte = File.ReadAllBytes(sourceFullPath)
    Dim copyresult As System.UInt32 = copyService.CopyIntoItems(sourceFullPath, destinationUrls, fields, fileContents, results)
    End Using
    End Sub

    Upload(“C:\workbench\Test Document.doc”, “http://sharepointserver/sites/sandbox/Shared Documents/”)

  2. Hi , its good work you’ve done there. My only trouble at the moment is getting comments added to each of the files I upload, anyway to do that, im starting to bald already , and its not helping that im tearing my hair out with this dilema!
    Thanks

  3. This is great, and while I have gotten it to work from one document library to another, it will not take a local file, and I am suspecting that is because it has to be on the same server ie, the C: drive of the server, noit the client machine.

    Can you confirm this and/or suggest a way to accomplish this from a client machine?

  4. You can copy the local file from any machine, don’t bother about the machine is client or Server.
    Check the local file and Destination file path is correct. Then only the document is uploaded to the SharePoint Library.

    • Hello Shantha,

      I am using WCF service to upload the file from local machine to Sharepoint. WCF service is running on different server than local machine and sharepoint server. In this case file is not uploaded on the sharepoint from local machine e.g. C:\TestUpload.txt file .

      Am I missing something or is there other way to achieve this?

      Thanks

      • Hello ,
        I am looking for something same, uploading document from unix server to sharepoint but initially i can test it from local machine to sharepoint,
        I am in very very urgent need of this code,please please if you have running code for this please forward the same to me on my email id ankush.raina1@gmail.com.
        I will test it on mu personal machine

  5. THe file size I am uploading is 30MB. It is giving the exception “System.EventArgs” and the file is not getting uploading to the document library.

    Pls. let me know your suggestion at the earliest.

  6. Hi, I´ve got a problem when the client is located in a local machine, meaning to say that if the client is located in the machine where sharepoint is installed all it’s ok, but if i move the client to another machine an error occur. Could you help me?

  7. I forgot to tell you i´m working with java client in axis 1.4

  8. Hi Shantha,

    I have managed to get the code working above however I encounter problems when I attempt to insert a Boolean field type as FieldInformation.

    fFiledInfo.DisplayName = “True or False”;
    fFiledInfo.Type = WSCopy.FieldType.Boolean;
    fFiledInfo.Value = True;

    I get errors on the last line stating it can not convert from bool to string. Any ideas?

  9. Hi,
    I am having trouble using this WSCopy. It says that i am missing a reference.
    Cant find which namespace to refer to.
    Please help
    thanks

  10. WSCopy is the Web Reference, which refers the SharePoint Copy WebService.
    That webservice is available on
    http://%5BServer%5D/_vti_bin/Copy.asmx .

    To know more, just visit my new blog http://www.ktskumar.com/blog

  11. Above code is working for Uploading File in Folder.
    But Files are not getting Uploaded in “List”.

    Can you help me out in this?

    Regards,
    Ane

  12. Hi

    I need upload a batch of documents, how I can do this with Lists Web Service?

    Could help me please.

    Regards,
    Ricardo

  13. is there any way to upload/download a file from client to server/viceversa without using webservice.

  14. using php+soap+NTLM am able to call this webservice and able to get the functions.But when I am trying to use the copyintoitems throwing some exception…

    can u able to help me regard this?

  15. Shantha, you are heaven-sent. You saved my neck!! I mean, my job…

  16. Hi,
    How do I create the stubs for my copy web service?
    The web service is hosted on a remote machine and i have its URL .
    i tried to use wsdl2java.bat but couldnt create it.

    Any help would be appreciated.

    Thanks,
    Arunraj

  17. hello,

    thanks for the code.

    the problem im facing is that i have no errors in my code but when i exesute it i get “Could not establish connection to network”. My scenario is different im using a mobile device to achieve an image file upload to my sharepoint document library through the web service code which you have given here. any ideas about the error ? thanks

  18. ive resolved the connection error by inserting the IP address instead of the local domain name, but now the file is not uploading it says Success in the end but i cannot see the file in my document library…any idea ? thanks

  19. Thank you so much the code helped me a lot 🙂

    • Hey, can u forward me the same code, i also need to upload the file to share point..ur help is much much appreciated, if u can forward me the code on ankush.raina1@gmail.com…thankyou so much…!

    • Hello san,
      As this is very old post , i am sending messages to multiple people & hope that i get a single reply,
      I am in need of this working code asap,
      Need to upload text file frm local/ unix to Sharepoint,
      I need code immediately,
      If u have alrdy compltd, pleas forward the code to “ankush.raina1@gmail.com”
      Any help is highly appreciated..!

  20. Hi,

    I tried with the code that you have provided. Am unable to upload the file/document to the library. code is executing properly.

    Can you please help me on this.

  21. OS using:Windows Server 2008
    Sharepoint:WSS3.0

    Application Name:testshare

    I added
    http://localhost/_vti_bin/copy.asmx as ServiceReference to my windows application project

    I gave “Sharepoint” as the name of the service.

    There is no ClassName of Copy in Sharepoint
    ie

    Sharepoint.Copy service = new Sharepoint.Copy();

    Error:
    The type or namespace name ‘Copy’ does not exist in the namespace ‘testshare.Sharepoint’ (are you missing an assembly reference?)

    Please help me

    • Hello tvs,

      Have you added Service Reference or Web Referencein the project? Add web refrence in the project and check there is method

      Web_ref_Name.Copy = new …()

  22. Thank you very much for providing this code.

  23. How can i i add more filed values while publishing documents.Including custom fields

  24. thanks for ur Nice code
    Hi,

    I tried with the code that you have provided
    my file gets overwrite….

    so how to avoid this overwritting of file or any way to give return message that “File alreaday Uploaded”

    please help me

  25. Hi,
    Im uploading the MSP 2010 file to the sharepoint, code is working fine but not able to read the file back from the sharepoint document library.

  26. How to prevent file overwrite

  27. Awesome blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks would really make my blog stand out.
    Please let me know where you got your design. Cheers

  28. The dashboard is meant to show the status of your project at a glance and may
    consist of the project’s SPI, CPI (Cost Performance Index), SV
    (Schedule Variance), CV (Cost Variance), PV (Planned Value), AC (Actual Cost), and EV (Earned Value).

    This option allows you to sync with your mobile device.
    In certain situations the amount of risk may be negligible whereas it
    may be highest at other.

  29. Pls suggest some ideas to upload the local file system doc to share point list using VBA Macro.

  30. I have followed all the suggestions made above and still running into the same issue of ‘Object reference not set to an instance of an object’ with errorCode: Unknown.

    The workflow works fine on localhost with

    copyService.Credentials = CredentialCache.DefaultCredentials;

    But throws this ‘Object reference not set to an instance of an object’ with errorCode: Unknown when running on server. It works fine when I use new NetworkCredentials on the remote server, but I want to the default login credentials.

    Please let me know if you have any thoughts regarding this.

    • This error usually means that the path you are trying to upload to is incorrect. Try loading the file to a different path and see if you hit the same issue or not.

  31. Nice one! Helped my very quick

  32. Hey suggestions , I was fascinated by the details ! Does anyone know where my business might locate a sample Custom Pay Check Stubs form to type on ?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: