Upload Document using c# code to Share-Point library

Hello everyone,

I was facing the following exception.So i had searched and got the solution.

Only String, int, and DateTime data types can be used as the value in Properties while adding list item in document library.

I am including following concept with this blog.

1) how to upload document in to SharePoint library from the other library.
2) pass the document metadata using hash table.
3) How to update custom field of SharePoint library with uploaded document.
4) Limitations of Files.Add() methods with hash table.

Key Concept :
What is hash Table?

  • The Hashtable class represents a collection of key-and-value pairs that are
  • organized based on the hash code of the key.
  • It uses the key to access the elements in the collection.
  • A hash table is used when you need to access elements by using key, and you can identify a useful key value.
  • Each item in the hash table has a key/value pair.
  • The key is used to access the items in the collection.

1)How can I upload document in to the Document library from the other document library and update Document library meta data in share point?
2)How can I update document library custom field with user type and Date time type Using c# code?
Let’s take document library:
1) UserInformation(Destination Library)
Custom Fields : 1) DocUpdateDate: DateTime 2)DocAddedBy: User
2) BioData(Source library)


//Get the Source library
 SPList BioData = elevatedWeb.Lists.Cast().FirstOrDefault(l => l.id == listID);
 if (bioData!=null)
 spListItem bioDataItem = bioData.GetItemById(itemID);
 // get file from the item which you want to upload the destination.
 SpFile objFile = Item.File;
 string fileName = objFile.Name;
 //Getting latest published version file content
 byte[] fileContent = null;
 if (objFile.UIVersionLabel.Contains(".0"))
 fileContent = file.OpenBinary();
 string versionString = string.Format("{0}.0", objFile.MajorVersion.ToString());
 SPFileVersion version = objFile.Versions.GetVersionFromLabel(versionString);
 fileContent = version.OpenBinary();
 // Now open the Destination library where you want to upload document.
 SpList UserInformation = elevatedWeb.Lists.Cast().FirstOrDefault(l => l.ID == listID);
 // create a hastable to stored the Document metadata.
 HashTable properties = new HashTable();
 // Add metadata key and value of the document.
 // you can number of properties as per the business requirement.
 // now upload the document in to the library.
 // to upload the file ,open the object of the SpFile.
 elevatedWeb.AllowUnsafeUpdates = true;
 SpFile uploadFile = UserInformation.RootFolder.Files.Add(fileName, fileContent, properties);



Limitations of Files.Add() methods with hash table.
If I add a column of type Currency then the code will break and it won’t add the list item to the library at all.Because the function list.RootFolder.Files.Add() will not accept the properties other than the 3 types mentioned,through HashTable
This is the problem with the function. To avoid that below is the code i used and this problem went away.


// create a SPListItem object from the objFile.
spListItem objListItem = uploadFile.Item;
objListItem[“DocUpdateDate”] = DateTime.Now;
// update list item
elevatedWeb.AllowUnsafeUpdates = false;


Finally, updating the list item and updating the file to reflect the changes to the document library list items.

Please feel free to post your ideas, comments and question here.

Happy Coding 🙂