4

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?

2

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
1

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/

  • Is it possible to download the documents from the sp 2010 site's splits attachments using spservices and copy.asmx – userAZLogicApps Apr 4 '18 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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