Thursday, June 2, 2011

Uploading files to sharepoint site using client mode.

First add two references to your project

Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime

This will enable you to use client calls.

Then you can try the following code to upload.. the code is a bit disarrange cause had little time to post if any problem please let me know.

using System;
using System.IO;
using System.Net;
using Microsoft.SharePoint.Client;
using Form = System.Windows.Forms.Form;
using ListItems = Microsoft.SharePoint.Client.ListItemCollection;
using System.Windows.Forms;

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

   


        private void selcectFile_Click(object sender, EventArgs e)
        {
            fileOpen.ShowDialog();

         
            foreach (string filenames in fileOpen.FileNames)
            {
                UploadFileInLibrary(siteName.Text, siteName.Text, docLibrary.Text, "", filenames);
            }

        }

        public void Uploadfile()
        {
            string documentName = string.Empty;
            byte[] docStream;// = new byte[1000];
            //ListItems listItems = GetListItemCollectionFromSP("Name", documentName, "ppt", 1);
            fileOpen.ShowDialog();
            var fileInfo = new FileInfo(fileOpen.FileName);

            ClientContext context = new ClientContext(siteName.Text);
            NetworkCredential credentials = new NetworkCredential(username.Text, password.Text);
            context.AuthenticationMode = ClientAuthenticationMode.Default;
            context.Credentials = credentials;
            docStream = new byte[fileInfo.Length];
            string fileName = Path.GetFileName(fileOpen.FileName);
            //Microsoft.SharePoint.Client.File.SaveBinaryDirect(context,);
            using (FileStream fs = new FileStream(fileOpen.FileName, FileMode.Open))
            {
                try
                {
                 
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/Documents/" + fileName, fs, true);
                    MessageBox.Show(@"File Uploaded Sucessfully !");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(@"Error Uploading : " + ex.ToString());
                }
            }
            //UploadDocument(siteName.Text, docLibrary.Text, docLibrary + "/", fileOpen.FileName, docStream);
         
        }
        public void UploadFileInLibrary(string siteUrl, string webName, string libraryName, string subfolderPath, string fileName)
        {
         
            try
            {   string uploadLocation = string.Empty;
                using (ClientContext clientContext = new ClientContext(siteUrl))
                {
                    NetworkCredential credentials = new NetworkCredential(username.Text, password.Text);
                    ClientContext context = new ClientContext(siteName.Text);
                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                    clientContext.Credentials = credentials;
                    uploadLocation = Path.GetFileName(fileName);
                    if (!string.IsNullOrEmpty(subfolderPath))
                    {
                        uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation);
                    }
                    uploadLocation = string.Format("{0}{1}/{2}", webName,libraryName, uploadLocation);
                    var list = clientContext.Web.Lists.GetByTitle(libraryName);
                    var fileCreationInformation = new FileCreationInformation();
                    fileCreationInformation.Content = System.IO.File.ReadAllBytes(fileName);
                    fileCreationInformation.Overwrite = true;
                    fileCreationInformation.Url = uploadLocation;
                    list.RootFolder.Files.Add(fileCreationInformation);
                    Status.Text = @"Uploading : " + uploadLocation;
                    clientContext.ExecuteQuery();
                }
                Status.Text = @"Uploaded : " + uploadLocation;
                //MessageBox.Show(@"File Uploaded Sucessfully !");
            }
            catch (Exception ex)
            {

                MessageBox.Show(@"Error Uploading : " + ex.ToString());
            }
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Uploadfile();
        }

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    ClientContext clientContext = new ClientContext("http://blr-vm-spdev01");
        //    clientContext.Credentials = new NetworkCredential("sp-farm01", "PCspFarm!");
        //    Web site = clientContext.Web;
        //    clientContext.Load(site);
        //    clientContext.ExecuteQuery();
        //    MessageBox.Show(site.Title);
         
        //}
    }
}
 You can design a form as follow:


There are two methods to upload using
1. ClientContext.ExecuteQuery();
2. Microsoft.SharePoint.Client.File.SaveBinaryDirect

Just try go through the code and if face any problem please drom me a mail.

Live visitors