Programmatically adding document to SharePoint Document library

Objective

In this article, I am going to add a document through code to a SharePoint document library. I am going to use SharePoint object model to add document.

Assumption

I do have a document library called “My Documents”. I am going to add document to this library. Currently the documents in library are as below.


Working Procedure

  1. User will enter full path of the file to be uploaded in the textbox.
  2. File will get uploaded with the name uploadaedfromcode.doc in the document library.
  3. Window form contains one textbox and one button.
  4. Add the reference of Microsoft.SharePoint.dll in the window application.

     


Design


Code

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Microsoft.SharePoint;

using System.IO;

namespace FileUploadinSharePoint

{


public
partial
class
Form1 : Form

{


public Form1()

{

InitializeComponent();

}


private
void Form1_Load(object sender, EventArgs e)

{

}


private
void btnUpload_Click(object sender, EventArgs e)

{


string filename = txtUpload.Text;


SPSite _MySite = new
SPSite(http://adfsaccount:2222/”);


SPWeb _MyWeb = _MySite.OpenWeb();


FileStream fstream = File.OpenRead(txtUpload.Text);


byte[] content = new
byte[fstream.Length];

fstream.Read(content, 0, (int)fstream.Length);

fstream.Close();

_MyWeb.Files.Add(http://adfsaccount:2222/My%20Documents/uploadedfromcode.doc”, content);


MessageBox.Show(“File Saved to name called uploadedfromcode”);

}

}

}

Explanation

  1. SPSite is returning the site collection. URL of the site collection is being passed to the constructor.
  2. SPWeb is returning the top level site.
  3. FileStream is reading the file from the path provided in the text box.
  4. I am reading the filestream in the byte.
  5. I am adding the file in the document library. There are two parameter passed. First parameter is URL of the document library. Second parameter is byte read from the file stream.
  6. I am giving name of the file is uploadedfromcode.doc

Output



 

=

Conclusion

In this article, I have shown how to upload a file in SharePoint document library. Thanks for reading.

Happy Coding

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

One thought on “Programmatically adding document to SharePoint Document library”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s