Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to retrieve a template document from a Document Libraries, in order to allow a user to edit it, and then upload it back again to the Document Library. I'll probably need to set some metadata info as well. I know this can be done via COM, but how can this be done via webservices?

share|improve this question

2 Answers 2

Use the copy.asmx web services, here's a good article by my colleague: http://ybbest.wordpress.com/2012/03/10/how-to-create-item-in-sharepoint2010-document-library-using-sharepoint-web-service/

share|improve this answer

Here are some C# code to upload and download files using webservice I hope it's useful for you.

        string HttpUrl = "http://sharepointvm";
        string LibUrl = "http://sharepointvm/Shared Documents";
        string FileUrl = "C:/aaa.docx";
        string SaveFileName = "aaa.docx";
        #region uploadfiles
        try
        {
            //SPSecurity.RunWithElevatedPrivileges(delegate()
            //{
                SPSite site = new SPSite(HttpUrl);
                SPWeb web = site.RootWeb;
                SPFolder folder = web.GetFolder(LibUrl);
                byte[] fileContents = System.IO.File.ReadAllBytes(@FileUrl);
                //System.IO.FileInfo myfile = new System.IO.FileInfo(FileUrl);
                //byte[] fileContents = new byte[int.Parse(myfile.Length.ToString())];
                //FileStream fs = File.OpenRead(FileUrl);
                //int n = fs.Read(fileContents, 0, int.Parse(myfile.Length.ToString()));
                try
                {
                    folder.Files.Delete(SaveFileName);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message.ToString());
                    //return 4;
                }
                folder.Files.Add(SaveFileName, fileContents);
                //fs.Close();
                web.Dispose();
                site.Dispose();
           // });

        }
        catch (Exception ex)
        {

            Console.Write(ex.Message.ToString());

        }
        #endregion

      #region downloadfile
        try
        {
            SPSite site1 = new SPSite(HttpUrl);
            SPWeb web1 = site1.RootWeb;
            SPList list = web1.Lists.TryGetList("Shared Documents");
            if (list == null)
            {
                Console.WriteLine("Shared Documents not exist!");
                return;
            }
            foreach (SPListItem item in list.Items)
            {
                if (string.Equals(item["Name"], "aaa.docx"))
                {
                    //SPListItem item = list.GetItemById(1);
                    //SPFile file = item.File;
                    byte[] fileData = item.File.OpenBinary();
                    System.IO.FileStream fstream = System.IO.File.Create("C:\\MyDownloadFolder\\" + item["Name"]);
                    fstream.Write(fileData, 0, fileData.Length);
                }
            }
        }
        catch (Exception ex)
        {

            Console.Write(ex.Message.ToString());

        }
        #endregion
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.