Today we will see how to upload document to the document library using its web services. May times we need this feature from any ASP.NET application or just any other java application to upload document to the document library.
That is why SharePoint web services come into the picture.
Here is a simple way you can upload the document.
I have one windows application created and for demo purpose, I am writing a code directly in button click event.
You need to reference copy.aspx web service. Here DownLoadSharePointDocument is the name of web reference name.
private void button5_Click(object sender, EventArgs e)
{
DownloadSharePointDocument.Copy copyService = new EncryptAndDecrypt.DownloadSharePointDocument.Copy();
copyService.Url = "{url}/" + "_vti_bin/copy.asmx";
copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;
string[] DestinationUrl = (“url goes here” };
string strSource = @"C:\SmartSurvey.pdf";
DownloadSharePointDocument.CopyResult FirstResult = new DownloadSharePointDocument.CopyResult();
DownloadSharePointDocument.CopyResult SecondResult = new DownloadSharePointDocument.CopyResult();
DownloadSharePointDocument.CopyResult[] ArrayOfResult = { FirstResult, SecondResult };
DownloadSharePointDocument.FieldInformation fFiledInfo = new DownloadSharePointDocument.FieldInformation();
fFiledInfo.DisplayName = "Description";
fFiledInfo.Type = DownloadSharePointDocument.FieldType.Text;
fFiledInfo.Value = "This is the test description";
DownloadSharePointDocument.FieldInformation[] fFiledInfoArray = { fFiledInfo };
FileStream objStream = new FileStream(strSource, FileMode.Open, FileAccess.Read);
byte[] Contents = new Byte[objStream.Length];
byte[] r = new Byte[objStream.Length];
int Read= objStream.Read(Contents, 0, Convert.ToInt32(objStream.Length));
objStream.Close();
uint copyresult = copyService.CopyIntoItems(strSource, DestinationUrl, fFiledInfoArray, Contents, out ArrayOfResult);
}
Make sure that you pass proper administrative credentials while passing the network credentials.
return value of uint should be zero to indicate a success. if it is not zero, then something is wrong and document will not upload successfully.
That is it. You have just uploaded the document to the document library.
6 comments:
can this webservice be used to upload a whole folder ?
can string strSource = @"C:\SmartSurvey.pdf" be changed by ... c:\*.*
sorry i am not a programmer
Jef
Hi,
There is a way to do this. However first you need to check if the folder name exists in the document library or not using web services only.
If not create one folder with the same name of your local machine folder.
And then you can just modify the shown code to recursively fetch each file from folder using System.IO class and then call that function which copies the file to document library.
Let us know if this answers your query.
Hi,
I am using copy webservice to upload several docs in library.
I have a lookup column in my document library and I am not able to set value for the same. Can you suggest?
Hi Monali,
Thank you for your query.
You can refer to this post.
http://www.sharepointkings.com/2010/05/adding-list-item-to-list-using-web.html
Let me know in case of any query.
And what do you thing about this error :
"The Copy web service method must be called on the same domain that contains the destination url"
But i want to upload file ower internet from different domain.
wow,
we had not tried from another domain yet so this one is new for us.
we will try this as soon we get chance.
Post a Comment