SharePoint - Populate data from visual webpart file upload control into custom list

Asked By santhosh reddy on 15-May-12 02:53 AM
I have few controls named by pjectname,code,pject resource,pjectload...For pjctname,code i m using textboxes and for pjctresoure,load i m using fileupload option.I m able to poplate name,code into custom list but i m not able to upload file into custom list.Wat is the code to upload documents into custom list.
Jitendra Faye replied to santhosh reddy on 15-May-12 04:45 AM
Try this code -

private void UploadFile(string FileName, string SiteUrl, string DocLibName)
    {
      try
      {

        if (File1.PostedFile == null)
          return;
        string strFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1);
        string destUrl = SiteUrl + "/" + DocLibName + "/" + strFileName;

        SPWeb site = new SPSite(destUrl).OpenWeb();
        site.AllowUnsafeUpdates = true;

        Stream fStream = File1.PostedFile.InputStream;
        byte[] contents = new byte[fStream.Length];

        fStream.Read(contents, 0, (int)fStream.Length);
        fStream.Close();

        site.Files.Add(destUrl, contents);


      }
      catch (Exception ex)
      {
        Response.Write("UploadDocFile ::" + ex.Message);
      }
    }


Hope this will help you.
Jitendra Faye replied to santhosh reddy on 15-May-12 04:46 AM
Follow this link also , here also you will get example for uploading file to SharePOint list-

http://dishasharepointworld.blogspot.in/2009/08/how-to-attachdeleteupload-files-to_08.html
santhosh reddy replied to santhosh reddy on 15-May-12 05:42 AM
Actaually let me explain you clearly.. I m able to populate data in customlists for textboxes.In the same way i want to populate data when i use fileupload control and should be upload that doc into custom list.When i click on button the data(documents) should be populated into the column which i created in custom list.

below is the code which i used for populating data from textboxes into customlist columns..

In the sameway i should be able to populate from upload control into sharepoint custom list??
 protected void Button1_Click(object sender, EventArgs e)
        {
            if (txttitle.Text.Trim() == "" || txtcode.Text.Trim() == "" || txtpjctname.Text.Trim() == ""||pjctdescrip.Text.Trim()==""||txtmanager.Text.Trim()==""||txttm.Text.Trim()==""||txtcc.Text.Trim()=="")
            {
                lblmess.Text = "Data Entry undone";


            }
            else
            {
                SPSite mysite = SPContext.Current.Site;
                SPWeb myweb = SPContext.Current.Web;
                SPList mylist = myweb.Lists["entrieslist"];
                SPListItem mylistitem = mylist.Items.Add();
                mylistitem["Title"] = txttitle.Text.ToString();
                mylistitem["ProjectCode"] = txtcode.Text.ToString();
                mylistitem["ProjectName"] = txtpjctname.Text.ToString();
                mylistitem["ProjectDescription"] = pjctdescrip.Text.ToString();
                mylistitem["ProjectManager"] = txtmanager.Text.ToString();
                mylistitem["ProjectTeam Members"] = txttm.Text.ToString();
                mylistitem["ClientContact Details"] = txtcc.Text.ToString();
                
                myweb.AllowUnsafeUpdates = true;
                mylistitem.Update();
                myweb.AllowUnsafeUpdates = false;
                lblmess.Text = "Data Entered Successfully";
            }