Tuesday, January 3, 2012

Programatically adding a document to a document library.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using System.IO;
namespace FileUploadinSharePoint
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void btnUpload_Click(object sender, EventArgs e)
        {
            string filename = txtUpload.Text;
            SPSite _MySite = new SPSite("http://agmsm:9999/");
            SPWeb _MyWeb = _MySite.OpenWeb();
            FileStream fstream = File.OpenRead(txtUpload.Text);
            byte[] content = new byte[fstream.Length];
            fstream.Read(content, 0, (int)fstream.Length);
            fstream.Close();
            _MyWeb.Files.Add("http://agmsm:9999/My%20Documents/uploadedfromcode.doc", content);
            MessageBox.Show("File Saved  to name called uploadedfromcode");
        }
    }
}

No comments:

Post a Comment