Thursday 5 December 2013

Update document library in sharepoint through c#

using (SPSite psite = new SPSite(SPContext.Current.Site.Url))
                {
                    string sectionurl = SPContext.Current.Web.ServerRelativeUrl;
                    using (SPWeb pweb = psite.OpenWeb(sectionurl))
                    {
                        pweb.AllowUnsafeUpdates = true;
                        SPDocumentLibrary mylist = pweb.Lists[Constants.libraryname] as SPDocumentLibrary;
                        string fileName = System.IO.Path.GetFileName(FileUploadControl.PostedFile.FileName);

                        //Get file extension ( *.doc OR *.docx )

                        string fileExtension = FileUploadControl.FileName.Substring(FileUploadControl.FileName.IndexOf("."));
                        byte[] fileBytes = FileUploadControl.FileBytes;
                        string destUrl = mylist.RootFolder.Url + "/" + fileName;
                        SPFile destFile = mylist.RootFolder.Files.Add(destUrl, fileBytes, true);
                        destFile.Update();

                        //update metadata

                        destFile.Item["File Main Category"] =Convert.ToString( hndBatchID.Value);
                        destFile.Item["File Sub Category"] = Convert.ToString(hndBatchID.Value); ;
                        DateTime txtStartDate = DateTime.Parse(txtactstartdate.Text);
                        DateTime txtEndDate = DateTime.Parse(txtactenddate.Text);
                        destFile.Item["Act Start Date"] = txtStartDate;
                        destFile.Item["Act End Date"] = txtEndDate;
                        destFile.Item["File Description"] = txtactfiledesc.Text;
                        destFile.Item.Update();
                        lblmessage.Text = "Documents Added Successfully. ";
                    }
}

No comments:

Post a Comment