Friday, 22 June 2012

Add File to SharePoint document Library

if you are using the object model you can use this as an example:



using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                 
                    using (FileStream fs = (new FileInfo("c:\\windows\\gone fishing.bmp")).OpenRead())
                    {
                        SPList list = web.Lists["tester3"];
                      
                        //add metadata
                        Hashtable ht = new Hashtable();
                        ht.Add("testcol", "myfile");
                        ht.Add("vti_title", "mytitle");

                        SPFile destfile = list.RootFolder.Files.Add(strDocName, fs, ht, true);

                    }


                }

            }




------------------
 if (fileUpload.HasFile)
                {
                    string mode = ViewState["Mode"].ToString();
                    if (string.Compare(WebPartMode.Update, mode, true) == 0)
                    {
                        DeleteDocument(attachementType);
                    }

                    byte[] filebytes = fileUpload.FileBytes;
                    SPList lst = default(SPList);
                    SPWeb wb = default(SPWeb);
                    wb = SPContext.Current.Web;
                    lst = wb.Lists["Documents"];
                    SPFolder myLibrary = wb.Folders["Documents"];
                    string Requestno = GetRequestNumber();
                    string fileName = string.Concat(fileUpload.FileName, "_", Requestno); ;
                    Hashtable objPrperties = new Hashtable();
                    objPrperties.Add("ID", GetLookFieldIDS(Requestno));
                    objPrperties.Add("AttachmentFor", attachementType);

                    SPFile spfile = myLibrary.Files.Add(fileUpload.FileName, filebytes, objPrperties);
                    myLibrary.Update();
                }

No comments:

Post a Comment