Download SharePoint document library files via WebService

Source: Internet
Author: User

First, the basic principle:

1. Get spitem.file from the object model to get the document library file

2. Return Item.File.OpenBinary () via WebService

3. Save the file to the server

4. Download from server to local

Second, the specific code:

WebService

[WebMethod]    Public byte[]  getattachmentfileflow (string webpath,string list,int fileId)     {        try        {            using ( SPSite site = new SPSite (Webpath))            {                SPWeb web = site. OpenWeb ();                SPList lists = web. Lists[list];                SPListItem item = lists. Items.getitembyid (fileId);                return item. File.openbinary ();            }        }        catch (Exception)        {            return new byte[] {};        }            }

Save to server, download local

protected void Page_Load (object sender, EventArgs e) {file.delete (path);        String Webpath = "http://cntzmsw18:8099/";        String list = "Shared Documents"; localhost. Service Server = new localhost.        Service (); Byte[] B = Server.    Getattachmentfileflow (Webpath, list, 1594);//1594 for file ID through SQL File.writeallbytes (path, b); } protected void Btnclick (object sender, EventArgs e) {FileStream filestrem = new FileStream (path, FileMode.        Open);        Long fileSize = filestrem.length; Context.Response.ContentType = "Application/octet-stream";//Set Message Context.Response.AddHeader ("Content-disposition" , "Attachment;filename=" + httputility.urlencode ("download. docx", Encoding.UTF8));//download file name needs to be changed Context.Response.AddHeader (        "Content-length", filesize.tostring ());        byte[] Filebuffer = new Byte[filesize];        Filestrem.read (Filebuffer, 0, (int) fileSize);        Context.Response.BinaryWrite (Filebuffer); Filestrem.dispose ();//Release capitalSource Context.Response.End (); }

Third, the source code:

Environment: VS2005, sp2007

Note: Server-side object model

http://app.yinxiang.com/l/AAwg2HN1vt9HW78Q29c5fzexeu1Z-DbJh3c/

Download SharePoint document library files via WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.