Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

How to Create a Document Library & Insert a new File?

Posted by Paul on December 4, 2012


Dear Friends, I was searching for a quick code to create a document library & insert a file into it. 

Following is the code that accomplishes the same.

using System;
using Microsoft.SharePoint;
using System.IO;
using System.Linq;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string title = "Document Library";

                    if (!web.Lists.Cast<SPList>().Any(l => l.Title == title))
                    {
                        web.Lists.Add(title, string.Empty, SPListTemplateType.DocumentLibrary);
                    }

                    SPDocumentLibrary library = web.Lists[title] as SPDocumentLibrary;

                    library.RootFolder.Files.Add("MyFile.txt", new byte[3] { 65, 66, 67 }, true);
                }
            }
        }
    }
}

 

After running the code you can see that:

  1. New Document Library created
  2. New File created inside it

You have to use the All Site Content to view this document library as the Quick Launch property is false.

image

 

image

Advertisements

2 Responses to “How to Create a Document Library & Insert a new File?”

  1. kumar said

    Hi Jean ,

    i am planning to develop one document management system.
    i need your guidelines for how to do it..

    thanks
    kumar

    • Jean Paul said

      Hello Kumar,

      I am ready to help you.. Before starting with I would like you to go through the following document management features of SharePoint:

      – Document Site template
      – Records Center
      – Document, Record
      – Document Set, Document ID Service
      – How SharePoint stores documents?

      Regards,
      Jean Paul

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 )

Connecting to %s