Thursday, June 16, 2016

CSOM Managed code implementation on SharePoint Document Library

Below is an example of .net windows application application that performs creating a document library, uploading a text file, retrieving the data present in the file and finally deleting the text file using .Net managed code.
[code]using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientSideApplication
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
        }   

        private void Upload_Click(object sender, EventArgs e)
        {
            ResultListBox.Items.Clear();
            var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
            using (var context = new ClientContext(siteUrl))
            {
                try
                {
                    var web = context.Web;
                    var list = web.Lists.GetByTitle("SharePoint Documents");
                    var filepath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\recurring.txt";
                    var fci = new FileCreationInformation();
                    fci.Content = System.IO.File.ReadAllBytes(filepath);
                    fci.Url = "recurring.txt";
                    fci.Overwrite = true;
                    var file = list.RootFolder.Files.Add(fci);
                    context.ExecuteQuery();
                    ResultListBox.Items.Add("Document Uploaded!");
                }
                catch (Exception ex)
                {
                    ResultListBox.Items.Add(ex.Message);
                }
            }
        }

        private void createLibrary_Click(object sender, EventArgs e)
        {
            ResultListBox.Items.Clear();
            var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
            using (var context = new ClientContext(siteUrl))
            {
                try
                {
                    var web = context.Web;
                    var lci = new ListCreationInformation();
                    lci.Title = "SharePoint Documents";
                    lci.TemplateType = (int)ListTemplateType.DocumentLibrary;
                    var list = web.Lists.Add(lci);                  
                    context.ExecuteQuery();
                    ResultListBox.Items.Add("List Added!");
                }
                catch (Exception ex)
                {
                    ResultListBox.Items.Add(ex.Message);
                }
            }
        }

        private void retrieveDoc_Click(object sender, EventArgs e)
        {
            ResultListBox.Items.Clear();
            var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
            using (var context = new ClientContext(siteUrl))
            {
                try
                {
                    var web = context.Web;
                    var listName = web.Lists.GetByTitle("SharePoint Documents");                  
                  
                    context.Load(listName);
                    context.ExecuteQuery();

                    Folder folder = listName.RootFolder;
                    FileCollection files = folder.Files;
                    context.Load(files);
                    context.ExecuteQuery();

                    foreach (File f in files)
                    {
                        FileInformation fileInformation = File.OpenBinaryDirect(context, (string)f.ServerRelativeUrl);
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
                        {
                            string line = sr.ReadToEnd();
                            ResultListBox.Items.Add(line);
                        }
                    }                 
                }
                catch (Exception ex)
                {
                    ResultListBox.Items.Add(ex.Message);
                }
            }
        }
      
         private void retrieveDoc_Click(object sender, EventArgs e)
        {
            ResultListBox.Items.Clear();
            var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
            using (var context = new ClientContext(siteUrl))
            {
                try
                {
                    var web = context.Web;
                    var listName = web.Lists.GetByTitle("SharePoint Documents");                  
                  
                    context.Load(listName);
                    context.ExecuteQuery();

                    Folder folder = listName.RootFolder;
                    FileCollection files = folder.Files;
                    context.Load(files);
                    context.ExecuteQuery();

                    foreach (File f in files)
                    {
                        FileInformation fileInformation = File.OpenBinaryDirect(context, (string)f.ServerRelativeUrl);
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
                        {
                            string line = sr.ReadToEnd();
                            ResultListBox.Items.Add(line);
                        }
                    }                 
                }
                catch (Exception ex)
                {
                    ResultListBox.Items.Add(ex.Message);
                }
            }
        }
    }
}[/code]

No comments:

Post a Comment