Upload A File To SharePoint Library Using Silverlight (Client Object Model)

HOW TO Upload A File To SharePoint Library Using Silverlight

DESIGN PAGE : 
Its a simple page, which contains one text box to insert other column details, and one browse button.From button click event we upload the file to sharepoint library

USE FOLLOWING CODE TO UPLOAD FILES IN SHARE POINT LIBRARY

C# CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;

namespace FileUpload
{


public partial class MainPage : UserControl
{
ClientContext site;

List LibraryName;
ListItemCollection _icollListcoll;  

//Load the site and library here=============================
public  MainPage()
{

InitializeComponent();

site = new ClientContext(ApplicationContext.Current.Url);
site.Load(site.Web);
LibraryName = site.Web.Lists.GetByTitle("Your Library Name");
site.Load(LibraryName);

CamlQuery qry = new CamlQuery();
qry.ViewXml = "<View/>";

 _icollListcoll = LibraryName.GetItems(qry);
 site.Load(_icollListcoll);

site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);
}

private void success(object sender,ClientRequestSucceededEventArgs arg)
{
         Dispatcher.BeginInvoke(datacon);
}

//Browse button click event(Here we get the file as Stream using c#)==============
private void btnBrowse_Click(object sender, RoutedEventArgs e)

{

            OpenFileDialog openFileDialog1 = new OpenFileDialog();


            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.Multiselect = true;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            try
            {
                if (userClickedOK == true)
                {
                    // Open the selected file to read.
                    System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
                    FileInfo FileToUpload = openFileDialog1.File;
                    UploadFile(FileToUpload);
                }

            }
            catch { }
 }

//This is the function upload the files to library in SharePoint=========================
 private void UploadFile(FileInfo fileToUpload)
 {
            FileCreationInformation FciFileToUpload = new FileCreationInformation();
            Stream streamToUpload = fileToUpload.OpenRead();
            int length = (int)streamToUpload.Length;  // get file length
            FciFileToUpload.Content = new byte[length];
            int count = 0;                        // actual number of bytes read
            int sum = 0;                          // total number of bytes read
            while ((count = streamToUpload.Read(FciFileToUpload.Content, sum, length - sum)) > 0)
                sum += count;  // sum is a buffer offset for next reading
            streamToUpload.Close();
            FciFileToUpload.Url = fileToUpload.Name;
            FciFileToUpload.Overwrite = true;       //it update file if already present
            Microsoft.SharePoint.Client.File clFileToUpload = LibraryName.RootFolder.Files.Add(FciFileToUpload);
            site.Load(clFileToUpload);
          
           //insert other columns of the file
            ListItem litem = clFileToUpload.ListItemAllFields;
            litem["Details"] = textBox1.Text;
            litem.Update();
            site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(suc), null);
  }

private void suc(object sender, ClientRequestSucceededEventArgs arg)
{

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

A type named 'SP.Data. could not be resolved by the model error

Add content type to SharePoint List/Library using REST API