Uploading file to sharepoint list.

Last post 08-28-2008 9:13 PM by rootsilver. 8 replies.

Sort Posts:

  • Uploading file to sharepoint list.

    09-29-2007, 10:30 PM
    • Member
      5 point Member
    • Jay59
    • Member since 09-19-2007, 6:12 PM
    • Posts 50

    This is a snippet that was suggestions by a friend, but it seems to be incomplete. I don't get any errors, but it doesn't upload file, what am I missing? What do I do with NewFile to make it work??

     

                     Dim FileContents As Byte
                    Dim Site As New SPSite("http://sharepointsite.com/qc/")
                    Site.AllowUnsafeUpdates = True
                    Dim Web As SPWeb = Site.OpenWeb()
                    Web.AllowUnsafeUpdates = True
                    Dim DocumentLibrary As SPList = Web.Lists("System")
                    Dim DocumentLibraryFolder As SPFolder = Web.Folders("System")
                    Dim NewFile As SPFile = DocumentLibraryFolder.Files.Add(System.IO.Path.GetFileName(NewFileName), FileContents)
                    Site.AllowUnsafeUpdates = False
                    Web.AllowUnsafeUpdates = False

  • Re: Uploading file to sharepoint list.

    10-13-2007, 2:45 AM
    • Member
      4 point Member
    • arungp
    • Member since 07-31-2007, 10:50 AM
    • Posts 27

    Hi Jay

     

    i think u should call Update() method. If u want I can post C# code, ut logic is same.

     regards

    Arun
    OptimusBT

  • Re: Uploading file to sharepoint list.

    10-15-2007, 3:27 PM
    • Member
      2 point Member
    • GopiEga
    • Member since 10-15-2007, 7:20 PM
    • Posts 1

    Hi Arun,

    Can i get the C# Code to Upload a file to SharePoint List

    I want to add the file to a any folder which I have to decided on run time (seleccting form list of folders etc)

    and How can I manage the Uploaded files permissions through code?

    Thanks In Advance

    Regards

    Gopi

  • Re: Uploading file to sharepoint list.

    10-17-2007, 1:31 AM
    • Member
      4 point Member
    • arungp
    • Member since 07-31-2007, 10:50 AM
    • Posts 27

     Hi Gopi,

    here i am giving the code. Please remember this is not optimized. This just uploads a file to a doc library.

    private void UploadDocFile(string FileName, string SiteUrl, string DocLibName)
        {
            try
            {


                if (File1.PostedFile == null)
                    return;
                string strFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1);
                string destUrl = SiteUrl + "/" + DocLibName + "/" + strFileName;

                SPWeb site = new SPSite(destUrl).OpenWeb();
                site.AllowUnsafeUpdates = true;

                Stream fStream = File1.PostedFile.InputStream;
                byte[] contents = new byte[fStream.Length];

                fStream.Read(contents, 0, (int)fStream.Length);
                fStream.Close();

                site.Files.Add(destUrl, contents);


            }
            catch (Exception ex)
            {
                Response.Write("UploadDocFile ::" + ex.Message);
            }
        }

     

    Regards

    Arun Dutt

    OptimusBT -India 

  • Re: Uploading file to sharepoint list.

    10-17-2007, 5:47 PM
    • Member
      5 point Member
    • Jay59
    • Member since 09-19-2007, 6:12 PM
    • Posts 50

     Ok I had to set this project aside and do something else, I'm back to this one and at this problem still.

    Looking at the code you posted I converted it to vb. Like so

          Try
          
             If File1.PostedFile Is Nothing Then
                 Return
             End If
             Dim strFileName As String = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\") + 1)
             Dim destUrl As String = SiteUrl + "/" + DocLibName + "/" + strFileName
            
             Dim site As SPWeb = New SPSite(destUrl).OpenWeb()
             site.AllowUnsafeUpdates = True
            
             Dim fStream As Stream = File1.PostedFile.InputStream
             Dim contents As Byte() = New Byte(fStream.Length - 1) {}
            
             fStream.Read(contents, 0, CInt(fStream.Length))
             fStream.Close()
            
                
                
             site.Files.Add(destUrl, contents)
         Catch ex As Exception
             Response.Write("UploadDocFile ::" + ex.Message)
         End Try

     

    So I would put this data after

    System.IO.File.Move(savePath, savePathMain & NewFileName)

     I used this to rename the file..

     

    My question is what is the File1.Postedfile mean?

     

    Should I just change it to this

    Dim strFileName As String = (savePathMain & NewFileName)
     

     

     

  • Re: Uploading file to sharepoint list.

    07-08-2008, 6:57 PM
    • Member
      2 point Member
    • boczek
    • Member since 07-08-2008, 10:54 PM
    • Posts 1

    Does anybody know how to upload multiple files (not only one file)?

    Thank you very much for quick reply.

     regards,

    Jakub

     

  • Re: Uploading file to sharepoint list.

    07-18-2008, 7:25 AM

     Hi,

     

    Please follow this link for multiple files upload

    http://www.c-sharpcorner.com/UploadFile/sarav82/MOSS11072007065009AM/MOSS.aspx 

     

    Thanks and Regards

    Rajan

  • Re: Uploading file to sharepoint list.

    08-08-2008, 6:36 AM
    • Member
      402 point Member
    • subhashdike
    • Member since 05-13-2005, 10:32 AM
    • India
    • Posts 84

     The following code has worked for me. See if this helps you...

            /// <summary>
            /// Uploads Document to the Site
           ///foldername: Name of folder to which the document is to be uploaded. documentFIleName: FileName of the file to be uploaded. Typically FileUpload control's output
            /// </summary>
            public void UploadDocumentToSite(string foldername, string documentFileName)
            {
                SPSite site = SPControl.GetContextSite(HttpContext.Current);
                SPWeb myWeb = site.AllWebs[“123”]; //123 is the site id.
                try
                {
                    site.AllowUnsafeUpdates = true;
                    myWeb.AllowUnsafeUpdates = true;
    
                    string folder = “MyCustomDocLib”;
    
                    SPFolder destFolder = myWeb.GetFolder(folder);
                    SPUser user = myWeb.Users[“Domain\\UserName”]; 
        //This may not be required always. I have used this so that the Created By/Modified By data is set with my userid instead of the imperosated account
    
                    destFolder.Files.Add(documentFileName, content, user, user, DateTime.Now, DateTime.Now);
    
                }
                catch (Exception ex)
                {
                    //Log the error here
                }
                finally
                {
                    //Dispose the web and site 
                }
            }
     
    Let me know if you see any issue with this 
    Thanks,.
    Subhash
    ===============
    Subhash Dike
    http://coolsubhash-tech.blogspot.com

    Make sure you Mark a reply as Answer if it helps.. It will help others as well

    ===================
  • Re: Uploading file to sharepoint list.

    08-28-2008, 9:13 PM
    • Member
      284 point Member
    • rootsilver
    • Member since 08-21-2007, 5:08 PM
    • NYC
    • Posts 52
    C'mon guys, if you're going to post code, at least make your functions complete, not requiring mystery global variables !
            /// <summary>
            /// Reference: Microsoft.SharePoint.dll
            /// using Microsoft.SharePoint
            /// Upload a file to a SharePoint document library. Many examples on the web seem to first
            /// convert a filestream to a byte array (?). You can save yourself some trouble by just 
            /// passing in the stream.
            /// Note: if you're confused about the arguments here, just browse to your document library
            /// and look at the URL. If it's "http://mysite/Test/My Doc Lib", then the SiteUrl is as below
            /// </summary>
            /// <param name="strInputFileName">local path to the file, i.e. c:\temp\README.txt</param>
            /// <param name="SiteUrl">the url of your SharePoint site, i.e. http://mysite</param>
            /// <param name="DocLibName">the *name* of your doc lib, i.e. Test/My Doc Lib</param>
            private void UploadFileToSharePoint(string strInputFileName, string siteUrl, string docLibName)
            {
                string destUrl = siteUrl + "/" + docLibName;
                string destFileUrl = destUrl + "/" + strInputFileName.Substring(strInputFileName.LastIndexOf("\\") + 1);
    
                SPWeb site = new SPSite(destUrl).OpenWeb();
    
                site.AllowUnsafeUpdates = true;
    
                FileStream fileStream = File.Open(strInputFileName, FileMode.Open);
    
                site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);
    
                fileStream.Close();
            } 
     
    Jeffrey Knight | MCTS .NET 2.0 | RHCT | http://www.rootsilver.com
Page 1 of 1 (9 items)