Tuesday, October 7, 2008

Programatticaly Upload a document to Sharepoint List

Content of Design File:









Content Of Code File:


protected void Button1_Click(object sender, EventArgs e)
{
byte[] contents;
SPSite sp = new SPSite("");
SPWeb site = sp.OpenWeb();
site.AllowUnsafeUpdates = true;
SPList list = site.Lists["Test"];
SPListItem myNewItem;

string strDate = System.DateTime.Now.Date.TimeOfDay.ToString();
myNewItem = list.Items.Add();
string fileName = File1.Value.Substring(File1.Value.LastIndexOf("\\") + 1);

string strFilepath = File1.Value.ToString();
StreamReader sr = new StreamReader(strFilepath);
Stream fStream = sr.BaseStream;
contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
myNewItem["Title"] = "Sendil";


myNewItem.Attachments.Add(fileName, contents);
myNewItem.Update();
// System.IO.File.Delete(strFilepath);---> Deleting a source file.......
}

No comments: