in SharePoint, SharePoint 2007

Code Snippets: Upload and Download a file from Sharepoint Document Library

Here are sample code snippets to upload and download a file from Sharepoint Document Library

Upload a File:

   1: public static void UploadDocument(string siteCollectionUrl,
   2:                             string siteUrl,string libraryName)
   3: {
   4:     using (SPSite hostSite = new SPSite(siteCollectionUrl))
   5:     {
   6:         #region Reading a file
   7: 
   8:         string filename = "C:\TFS.doc";
   9:         FileInfo fInfo = new FileInfo(filename);
  10:         long numBytes = fInfo.Length;
  11:         FileStream fStream =
  12:             new FileStream(filename,
  13:                            FileMode.Open,
  14:                            FileAccess.Read);
  15:         BinaryReader br = new BinaryReader(fStream);
  16:         byte[] fileBuffer = br.ReadBytes((int)numBytes);
  17:         br.Close();
  18:         fStream.Close();
  19: 
  20:         #endregion
  21: 
  22:         #region Fields
  23: 
  24:         Dictionary<string, string> DocumentFields =
  25:                           new Dictionary<string, string>();
  26:         DocumentFields.Add("Status", "Received");
  27: 
  28:         #endregion
  29: 
  30:         #region Upload File into the Document Library
  31: 
  32:         using (SPWeb targetWeb = hostSite.OpenWeb(siteUrl))
  33:         {
  34:             DateTime submissionDate = DateTime.Now;
  35:             SPList submissionsList = targetWeb.Lists[libraryName];
  36:             SPFolder folder = submissionsList.RootFolder;
  37: 
  38:             string fileName = "Sample.doc";
  39: 
  40:             SPFile newSubmissionFile = folder.Files.Add(fileName,fileBuffer);
  41: 
  42:             SPListItem submissionItem = newSubmissionFile.Item;
  43: 
  44:             foreach (KeyValuePair<string, string> documentField in DocumentFields)
  45:             {
  46:                 submissionItem[documentField.Key] = documentField.Value;
  47:             }
  48: 
  49:             submissionItem.Update();
  50:         }
  51: 
  52:         #endregion
  53:     }
  54: 
  55:     Console.WriteLine("File Uploaded Successfully!");
  56: }

Download a File:

   1: public static void DownloadDocument(string siteCollectionUrl,
   2:                     string siteUrl, string libraryName, string fileName)
   3: {
   4:     using (SPSite hostSite = new SPSite(siteCollectionUrl))
   5:     {
   6:         #region Download the File from the Document Library
   7: 
   8:         using (SPWeb targetWeb = hostSite.OpenWeb(siteUrl))
   9:         {
  10:             DateTime submissionDate = DateTime.Now;
  11:             SPList submissionsList = targetWeb.Lists[libraryName];
  12:             SPFolder folder = submissionsList.RootFolder;
  13:             SPFile targetFile=targetWeb.GetFile(folder.Url + "/" + fileName);
  14: 
  15:             if (targetFile.Exists)
  16:             {
  17:                 byte[] fileContents = targetFile.OpenBinary();
  18: 
  19:                 if (!File.Exists(fileName))
  20:                 {
  21:                     File.Create(fileName)
  22:                         .Write(fileContents, 0,
  23:                                fileContents.Count());
  24: 
  25:                     Console.WriteLine("File saved successfully");
  26:                 }
  27:                 else
  28:                 {
  29:                     Console.WriteLine("File Already Exists");
  30:                 }
  31:             }
  32:         }
  33: 
  34:         #endregion
  35:     }
  36: }

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.