Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Sunday, 22 June 2014

Upload Attachments with html upload control when asp.net upoload control presence To SharePoint Document library Programmatically

 private void UploadOtherAttachments()
        {
            try
            {
                    HttpFileCollection hfcFiles = Request.Files;
                    if (hfcFiles.Count > 0)
                    {
                        for (int i = 0; i < Request.Files.Count; i++)
                        {
                            string[] Files;
                            bool Flag = false;
                            HttpPostedFile PostedFile = hfcFiles[i];
                            string RequestFilename = "";
                            if (PostedFile != null && PostedFile.FileName != String.Empty &&PostedFile.FileName!=(fuID.PostedFile.FileName) && PostedFile.ContentLength > 0)
                            {
                                RequestFilename = PostedFile.FileName;
                                string[] Separator = { "\\" };
                                if (PostedFile.FileName.Contains(Separator[0]))
                                {
                                    Files = PostedFile.FileName.Split(Separator, StringSplitOptions.None);
                                    RequestFilename = Files[Files.Length - 1];

                                }

                                string strInputFileName = Path.GetFileNameWithoutExtension(PostedFile.FileName)
                                                          + "_" + Path.GetExtension(PostedFile.FileName);

                                SPList docLibrary = Web.Lists["Library"];
                                SPFolder OAFolder = null;
                                foreach (SPListItem item in docLibrary.Folders)
                                {
                    SPFolder folder = item.Folder;
                                    if (folder.Name == "folder1")
                                    {
                                    OAFolder = "name of the folder";
                    }   
                                }
                                string docLibraryUrl = OAFolder.Url.ToString();
                                string destinationURL = docLibraryUrl + "/" + strInputFileName;

                                SPFile file = null;
                                file = docLibrary.RootFolder.Files.Add(destinationURL, PostedFile.InputStream, true);
                                SPListItem newItem = file.Item;
                                newItem["fld1"] = txt1.Text;
                                newItem["fld2"] = txt2.Text;
                                Web.AllowUnsafeUpdates = true;
                                newItem.Update();
                                file.Update();
                                web.AllowUnsafeUpdates = false;

                            }

                            else
                            {

                                //_Errormsg = "Request created..Relevant document upload failed";
                            }
                        }
                    }
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.