none
issue with document upload code RRS feed

  • Question

  • I want to upload multiple documents with same metadata.i have created a custom webpart with my metadata and fileupload controls. On click of ok button all the uploaded documents (from fileupload controls) will be added to the document library with metadata. Am using below function to add the documents to document library.I will call below function for each fileupload control.

            private static void insert(SPList docLibrary, FileUpload Fu)
            {
                Stream fStream = Fu.PostedFile.InputStream;
                byte[] contents = new byte[fStream.Length];
                fStream.Read(contents, 0, (int)fStream.Length);
                fStream.Close();
               
                SPFile destFile = docLibrary.RootFolder.Files.Add(docLibrary.RootFolder.Url + "/" + Fu.FileName, contents, true);
                SPListItem destItem = destFile.Item;                    
                destItem.File.CheckIn(string.Empty, SPCheckinType.MinorCheckIn);
            }
    If I add same same document to 2 file upload controls then first file is getting added properly but for 2nd file am getting “file must be checked out ” error. Any idea what can be the issue ?
    Thursday, January 14, 2016 9:42 AM

Answers

  • Hi 

          destItem.File.CheckIn(string.Empty, SPCheckinType.MinorCheckIn); 

    is there any Checkout method also?

     using (SPSite siteColl = new SPSite(“http://ServerName”))
    
     {
    
     //Get SPWeb object representing a particular website
    
     SPWeb webSite= siteColl.AllWebs[“WebsiteName”];
    
     webSite.AllowUnsafeUpdates = true;
     webSite.Lists.IncludeRootFolder = true;
    
    //Get the document library in which documents will be uploaded
    
    SPList docLibrary = webSite.Lists[“DocLibName”];
    
     // objFileUpload is asp.net FileUpload Object
    
     Stream FStream; string fname;
    
     FStream = objFileUpload.PostedFile.InputStream;
    
     int basenamestart = objFileUpload.PostedFile.FileName.LastIndexOf(@”\”);
     fname = objFileUpload.PostedFile.FileName.Substring(basenamestart + 1);
    
    //Upload files into SharePoint document library
    
     SPFile file = docLibrary.Files.Add(fname, FStream, metadata, true);
    
     file.Update();
    
     }

    https://rameshreddy2010.wordpress.com/2011/07/25/upload-multiple-files-into-sharepoint-document-library-programmatically-using-c/


    Rajendra Singh
    sharepoint Adda
    Walking on water and developing software from a specification are easy if both are frozen
    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful If a post answers your question, please click Mark As Answer on that post and Vote as Helpful

    • Marked as answer by Nikhil0041 Friday, January 15, 2016 1:33 PM
    Thursday, January 14, 2016 9:51 AM