Shantha Kumar T
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,
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);
24,849 Comments
Comments are closed.
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:workbenchTest Document.doc”, “http://sharepointserver/sites/sandbox/Shared Documents/”)
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
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?
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.
Hi Shantha kumar,
Iam getting DestinationInavlid Error code and ErrorMessage=”The Copy web service method must be called on the same domain that contains the destination url.”, when i use copyIntoItems API. I have given he sourceUrl and destinationUrl properly. Can you let me know the cause for this ?
Ensure the Destination url, doesn’t had the file or the path and folder are available.
I think, the file is already present at your destination url.
Hi Kumar, I used the code, but my problem is that throw an error: “Object reference not set to an instance of an object”, and I searched a lot, for answers and nothing work¡¡¡¡. You can help me please, if you known what is wrong. Thanks a lot
Hi Juan,
“Object reference not set to an instance of an object” occurs because of the code not able get the details from SharePoint. So check the source url and field information u are given and also verify other variables getting correct information. If you want more clarification post your code here.
Urgent,
Hi,
I used the code but its showing an error : “Method or Operation Not Implemented” exception at CopyIntoItems() method,
please help me..
Working fine without error but i could see the file uploaded in Share point, what could be the reason?
please help
I’m trying to use your code in a SSIS 2005 script task so I need it in VB.net and I would like to know how your are referenceing WSCopy ??
Here is a blank script:
‘ Microsoft SQL Server Integration Services Script Task
‘ Write scripts using Microsoft Visual Basic
‘ The ScriptMain class is the entry point of the Script Task.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Public Class ScriptMain
‘ The execution engine calls this method when the task executes.
‘ To access the object model, use the Dts object. Connections, variables, events,
‘ and logging features are available as static members of the Dts class.
‘ Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
‘
‘ To open Code and Text Editor Help, press F1.
‘ To open Object Browser, press Ctrl+Alt+J.
Public Sub Main()
‘
‘ Add your code here
‘
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
I added
http://localhost/_vti_bin/copy.asmx as ServiceReference to my windows application project
I gave WSCopy as the name of the service.
There is no ClassName of Copy in WSCopy
ie
WSCopy.Copy c = new WSCopy.Copy()
Error:
The type or namespace name ‘Copy’ does not exist in the namespace ‘testshare.WSCopy’ (are you missing an assembly reference?)
You have to add the web service url in Web Reference, instead of Service Reference.
On Visual Studio Project,
Right click the References, and select the Add Service Reference.
On Add Service Reference popup, click the Advanced button on bottom of the box,
Now the Service Reference Settings popup will open, there we have to click the “Add Web Reference” botton,
Then give the Web Service url and click the “Add Reference” button to include webservice url to the project.
Hi Shantha Kumar,
Thank you very much for your quick replay.
As you said I added the webreference insted of service refesrence and the program runs fine.
i was successful in uploading the files to my local sharepoint document library using http://localhost/_vti_bin/copy.asmx.
But when i try to upload the files to my remote sharepoint site using http://remotesite/_vti_bin/copy.asmx it fails.
I only alter
copyService.Credentials = new NetworkCredential(“username”, “password”, “domainname”);
copyService.CopyIntoItems() is returing zero,but file is not showing uploaded
Code worked Just fine! Thank You
thanks for the post.. can you help me with teh fieldinformation class. i need to have values for more than two fields how do i use,, can you give me the code
I am using same code to upload pdf file to sahrepoint .
But the problem is when ever I am uploading same file in same destination it ‘s not overwriting file.
Can you please tell me How we can overwrite same file using CopyIntoItems method.
I am using java webservice to implement for this…
Thanks in Advance..
Rajesh M
Hi Shantha,
I dont know about scripting but I need a resolution to upload files from local system to sharePoint using SSIS in my project, I use manualy upload files on sharepoint daily. I am trying to do this but not success. Please mail me with the full script so that I can implement in my packages. In above replies, Marzipan also asking for the same thing but you didn’t reply that. Please suggest me.
Thanks in advance for your support….
Dear Sanjay,
The script is available in Codeplex, check the Console application here, http://spuploadsdownloads.codeplex.com/
Hi,
Upload the files to my remote sharepoint site using http://remotesite/_vti_bin/copy.asmx it fails.
If I use
copyService.Credentials = System.Net.CredentialCache.DefaultCredentials
I alter to use below code, its working fine
copyService.Credentials = new NetworkCredential(“username”, “password”, “domainname”);
Please let me know CopyService does not allow to use DefaultCredentials to upload files to remote server.
Hi Seetha,
Yes, CopyService allows to use both DefaultCredentials and NetworkCredentials. All WebServices in SharePoint allows us to use both options.
The DefaultCredentials takes the Current OS Username and domian name to login to the site using WebService.
If we want to give our own credentials, then we can use the option NetworkCredentials.
Hi,
I will develop a web application that uploads document to sharepoint via the webservice. Do I need a CAL license for SharePoint to do this. Or do I need a CAL license for every user of the web application?
string webUrl = “https://amphorainccom-19.sharepoint.microsoftonline.com/”;
WSCopy.Copy copyService = new WSCopy.Copy();
copyService.Url = webUrl + “/_vti_bin/copy.asmx”;
copyService.Credentials = new NetworkCredential(“xyz@xyz.com”, “*****”);
uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);
My code is failing with following Exception:
Object moved
Object moved to here
Hi Kumar,
You are doing good job. Thanks for your information.
Regarding this topic, I have one doubt.
How can i add the reference for “WSCopy”?
Need to install SharePoint Server to my system for uploading/ Downloading file from SharePoint server.
Please advice me. What is the solution for upload the file to SharePoint Server.
WSCopy.Copy copyService = new WSCopy.Copy();
Hi Shantha Kumar
Please ignore my previous comment. I am able to figure out adding reference for “WSCopy”.
Now I have another problem.
I am getting “Object reference not set to an instance of an object” in the cResultArray parameter from your code. Like you replied to Juan I checked source url, and other variables and found nothing wrong with them. I am sending a empty Field Information. Below is the code I am using
//Copy WebService Settings
string webUrl = “http://remotesite”;
Copy copyService = new Copy();
copyService.Url = webUrl + “/_vti_bin/copy.asmx”;
copyService.Credentials = GetCredentials();
//Declare and initiates the Copy WebService members for uploading
string sourceUrl = Server.MapPath(“~/”) + “TestWebService/test.pdf”;
string[] destinationUrl = { webUrl + “/Support_Docs/test.pdf” };
CopyResult cResult1 = new CopyResult();
CopyResult cResult2 = new CopyResult();
CopyResult[] cResultArray = { cResult1, cResult2 };
FieldInformation fFiledInfo = new FieldInformation();
//fFiledInfo.DisplayName = “Description”;
//fFiledInfo.Type = FieldType.Text;
// fFiledInfo.Value = “Sample Description”;
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);
Hi,
I used the same code for my web service. The only difference is I used parameters to pass the sourceurl and destination path. It works properly when I debug it. But doesn’t seem to be working when I hosted the web service. Any idea why?
Thank you.
Hi Shantha…thanks alot …your code is the best code in the net.this works perfect for me. finally after 1 week search in google…
Thank you
Hi…
I get following error
The type or namespace name ‘WSCopy’ could not be found (are you missing a using directive or an assembly reference?)
Appreciate all your help and support
regards
Anil
Hi..
I get following error
The type or namespace name ‘WSCopy’ could not be found (are you missing a using directive or an assembly reference?)
Appreciate all your help and support
I have added the web reference. cheers
Anil
I am trying to call the CopyIntoItems from Javascript, but unable to pass the user credentials to the call. The call is failing with a 401 Unauthorized error. How is this to be resolved? I will not be able to use .Net nor Java to do this.
WSCopy.Copy copyService = new WSCopy.Copy();
i ma not getting WS.Copy …… how can i get Copy
I have successfully uploaded the file to the server but by default its in check out state.
How can I automatically checkin the file ?
Thanks
does anybody have the solution to the question below.
I have successfully uploaded the file to the server but by default its in check out state.
How can I automatically checkin the file ?
Thanks
Hi Santhakumar,
I have a very weird requirement. I am new to programming and do not know of it.
Could you please help me?
My requirement is:-
I have a external system which has lot of documents. It also contains some details related to documents such as date when it was uploaded, ID, document name, etc.
This information is stored in MySQL.
Now my task is to migrate these documents along with its data to our SharePoint library. I am using SharePoint 2013.
Can you please let me know how to achieve this?
One way I think is, export details regarding document into excel and then copy excel info into library. But then how to migrate documents? and how to make sure that proper document is uploaded against proper details of document?
Do I need to do any custom coding? If yes, can you tell me how to do it, since I am not much aware of it.
Please suggest. Your help is highly appreciated.
Thanks & regards,
Manish
Hi There!
I am getting the error Value can not be null parameter name:uriString
The error is coming in WSCopy.Copy copyService = new WSCopy.Copy(); line
I am using this in VB.Net and SP is 2013. Kindly advise.
Very helpful!
HI,
I want to use this code in script task in SSIS.
I copied this code but getting many errors.
I am not that much good in C#
And many doubts I am having
1) what reference and namespace need to be added so that WSCopy don’t shoe error.
2) Double quotes need to be replace by single quotes.
Thanks a lot
Ankur
So what do you do about the copy link that won’t let you archive when you use the copy command?
Hi ,
How we can upload a file using REST API (Postman) into share point ?