Monday, April 4, 2016

SharePoint Upload file with Metadata Programatically



            SPSecurity.RunWithElevatedPrivileges(delegate()
            {              
                Web.AllowUnsafeUpdates = true;
                SPFolderCollection sfo = SPContext.Current.Web.Folders;
                string path = SPContext.Current.Web.Url + "/CustLib/" + txtName.Text;                
                sfo.Add(path);

                //SPFile sfile = SPContext.Current.Web.Files.Add(path + "/" +
                //   txtName.Text + FileUpload1.PostedFile.FileName, FileUpload1.FileContent, true);

                SPFile sfile = SPContext.Current.Web.Files.Add(path + "/" +
                   txtName.Text + System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName),
                   FileUpload1.FileContent, true);

                SPListItem lstItem = sfile.Item;
                lstItem["Title"] = txtTitle.Text;
                lstItem["Name"] = txtName.Text;
                lstItem["Phone"] = txtPhone.Text;
                lstItem["City"] = txtCity.Text;

                lstItem.Update();               
                Web.AllowUnsafeUpdates = false;        
              
                string siteURL = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
             
                String cstext1 = "<script type=\"text/javascript\">" +
                            "alert('Inserted Successfully!'); window.location.href = '" + siteURL + "';</" + "script>";
                Page.RegisterClientScriptBlock("script1", cstext1.ToString());  // Response.Redirect(siteURL);  
               

            });

No comments:

Post a Comment