SharePoint Document Library and List – File Upload

There are different ways you can add documents to a doc library or to list item as an attachment. You could use object model, built-in web service, custom web service that uses object model, or HTTP PUT method.

In this post I will be stepping through them in detail. Let us take the object model approach first. For the sake of simplicity I have created a simple windows forms app that will push documents in to the document library and list

 

Adding documents to a Document Library and updating document attributes :

   1: private void button1_Click(object sender, EventArgs e)

   2:         {

   3:             if (openFileDialog1.ShowDialog() == DialogResult.OK)

   4:             {

   5:                 try

   6:                 {

   7:                     FileStream myStream;

   8:                     string fullName;

   9:                     if ((myStream = (FileStream)openFileDialog1.OpenFile()) != null)

  10:                     {

  11:                         fullName = openFileDialog1.FileName;

  12:                         FileInfo myFileInfo = new FileInfo(fullName);

  13:                         using (myStream)

  14:                         {

  15:                             using (SPSite oSite = new SPSite("http://vslearnmoss/sites/FileUpload"))

  16:                             {

  17:                                 using (SPWeb oWeb = oSite.OpenWeb())

  18:                                 {

  19:                                     SPFolderCollection oFolders = oWeb.GetFolder("MyDocuments").SubFolders;

  20:                                     SPFolder oFolder = oFolders["Folder1"];

  21:                                     SPFileCollection oFileColl = oFolder.Files;

  22:                                     Hashtable oDocAttribs = new Hashtable { { "Field1", "Value1" }, { "Field2", "Value2" } };

  23:                                     SPFile oFile = oFileColl.Add(myFileInfo.Name, myStream, oDocAttribs, true);

  24:                                 }

  25:                             }

  26:  

  27:                         }

  28:                     }

  29:                 }

  30:                 catch (Exception ex)

  31:                 {

  32:                     MessageBox.Show("Error: " + ex.Message);

  33:                 }

  34:             }

  35:  

  36:         }

Adding document attachments to a list item  :

   1: private void button1_Click(object sender, EventArgs e)

   2:         {

   3:             if (openFileDialog1.ShowDialog() == DialogResult.OK)

   4:             {

   5:                 try

   6:                 {                    

   7:                     string fullName;

   8:                     fullName = openFileDialog1.FileName;

   9:                     FileInfo myFileInfo = new FileInfo(fullName);

  10:                     byte[] fileData = System.IO.File.ReadAllBytes(fullName);

  11:  

  12:                     using (SPSite oSite = new SPSite("http://vslearnmoss/sites/FileUpload"))

  13:                     {

  14:                         using (SPWeb oWeb = oSite.OpenWeb())

  15:                         {

  16:                             SPList oList = oWeb.Lists["MyList"];

  17:                             SPQuery oQuery = new SPQuery { Query = "0" };

  18:                             SPListItem oItem = oList.GetItems(oQuery).Add();

  19:                             oItem["Title"] = textBox1.Text;

  20:                             SPAttachmentCollection attachments = oItem.Attachments;

  21:                             attachments.Add(myFileInfo.Name, fileData);

  22:                             oItem.Update();

  23:                         }

  24:                     }

  25:                 }

  26:                 catch (Exception ex)

  27:                 {

  28:                     MessageBox.Show("Error: " + ex.Message);

  29:                 }

  30:  

  31:             }

  32:         }

One thing to note when accessing the list for creating a new list item is to be mindful that when SPList.Items.Add is called it fetches all the items before creating a new item, one way to avoid is to make a blank query that fetches zero results and add a new list item to the collection. [Thanks to Rob Garret]

SPQuery oQuery = new SPQuery { Query = "0" };

SPListItem oItem = oList.GetItems(oQuery).Add();

 

You can look for more info about adding list item efficiently in this article by Rob Garret.

In the next part of this article, I will show you how to utilize web services or HTTP PUT method to push documents into the SharePoint document library.

Advertisements

File Not Found – Assembly Binding Error

 

I am sure many of you might have come across the “File Not Found” error related to the Assembly Binding.

 

FileNotFound Error

 

The best way to diagnose is to use the Fusion Log Assembly Viewer. The tool FUSLOGVW.exe is located at C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

You can also access this tool from your Visual Studio Command Prompt.

CommandPrompt

Fusion Assembly Log Viewer Settings:

FusLogVw Settings

  • Set your Log Settings as per the screenshot above. Set your own custom log path.
  • Close the Fusion Assembly Log Viewer
  • Once you are done with those settings make sure you delete all the Temporary ASP.NET Files from the “Windows\Microsoft.NET\Framework\v2.0.50727” folder.
  • Do an IISRESET
  • Start the Fusion Assembly Log Viewer
  • Open your ASP.Net web application and recreate the erroring scenario.
  • Click Refresh button in the Log Viewer
  • Look for App_Web_error.aspx entry..and move up in the list you will find the dlls that the application is trying to bind. Double click it and you will find the details of the binding error.

FusionLog Error Output

Ref: http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

Unable to Open a CHM file – You get a Unable to display web page error

When you try to open CHM files you might encounter issues. This is primarily due to 2 reasons.

 

  1. Security
  2. File path contains certain characters like #, ?, &, +

 

To overcome the security issue you have to follow the steps below..

  1. When you open the file you will be prompted with a security warning which you can dismiss by un checking the “Always ask…” checkbox and clicking “Open” button.
  2. Right Click the file and select properties from context menu. Click unblock button at the bottom and click Apply, OK

image

image

 

If you still get the error “This program cannot display the webpage” then verify if the path to the file has any invalid characters. For example a  path “C:\My C# Project Documentation\MyHelpFile.chm” will not work !

 

Change your path to “C:\My CSharp Project Documentation\MyHelpFile.chm” and it will work.