XML Reader – Reads XML files from PC and upload to SharePoint document library

Hello there,

In my last blog, we saw how to browse files from PC and upload it to SharePoint document library, check it here.

In today’s blog, we are going to see, how to upload files and also update metadata of the files to be uploaded to SharePoint document library by picking their names from .xml files which are stored in a folder on PC. Scenario is little bit similar to the scenario in the previous blog, just in this case we are not browsing files from PC but the folder path is given in the form of string. Let’s get started!

Prerequisites:

  1. SharePoint Online tenant.
  2. Visual Studio IDE.

Scenario:

  1. Name of folder from which files are to be uploaded – Demo Folder.
  2. .xml files names – File1.xml, File2.xml
  3. Files to be uploaded – File1.docx, File2.docx
  4. xml contains the name and metadata of File1.docx and File2.xml contains the name and metadata of File2.docx
  5. We are going to read File1.xml and File2.xml and get the names and metadata of file to be uploaded one by one.
  6. Important point to note here that .xml files are not to be uploaded.

Important Methods or Classes used:

  1. Class XmlTextReader :-
    1. It can read from a file, from an internet location, or from any other stream of data
    2. Parameter passed is string containing .xml file path
  2. Method SaveBinaryDirect():-
    1. Method with a FileStream object to upload files to a document library.
    2. Parameters passed are – client context object, server relative destination path, file stream object.
  3. Class StreamWriter:-
    1. Class used to write a file (In this case used for error logs).
  4. Detailed code is given below with comments to understand easily.

Code:

App.Config file

Program.cs

References:

https://msdn.microsoft.com/en-us/library/0ax3f4f3(v=vs.110).aspx

https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/upload-large-files-sample-app-for-sharepoint

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.