Wednesday, October 29, 2014

Send a file to SharePoint using c#

Its easy:

using System;
using System.IO;
using System.Windows.Forms;
using WindowsFormsApplication7.SharepointServiceReference;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string fullFileName = @"c:\xyz.pdf";
                string fileName = Path.GetFileName(fullFileName);
                byte[] bytes = File.ReadAllBytes(fullFileName);
                string contentType = "application/pdf";

                Uri _sharepointUri = new Uri(@"http://sptest.develop.fcbt/hub/dato/loaniq/_vti_bin/ListData.svc");

                TheHubLoanIQFCBTDevDataContext context = new TheHubLoanIQFCBTDevDataContext(_sharepointUri);
                context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                string slug = "/hub/dato/loaniq/DropOffLibrary/" + fileName;
                Stream fileStream = new MemoryStream(bytes);


                DropOffLibraryItem doli = new DropOffLibraryItem();
                doli.ContentType = contentType;
                doli.Name = Path.GetFileName(fileName);
                doli.Title = fileName;
                doli.Path = fileName;
                context.AddToDropOffLibrary(doli);
                context.SetSaveStream(doli, fileStream, true, contentType, slug);
                context.SaveChanges();
                context.LoadProperty(doli, "DocumentID");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
        }
    }
}


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home