SharePoint 2010 – Create Document Library

In this article we can explore the creation of Document Library through code. Here we are using existing Document Templates inside SharePoint to create a new document library.

Following are the activities involved in Document Library creation:

· Find the List Template

· Find the Document Template

· Create Document Library

Inside Visual Studio

Create a new console application and name it as DocLibCreation. Make sure you change the target framework to .Net 3.5.

Add the following code into the Program.cs file:

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet"))

{

using (SPWeb web = site.OpenWeb())

{

SPListTemplate listTemplate = web.ListTemplates["Document Library"];

SPDocTemplate docTemplate = (from SPDocTemplate dt in web.DocTemplates

where dt.Type == 122

select dt).FirstOrDefault();

Guid guid = web.Lists.Add("My Docs", "My Documents", listTemplate, docTemplate);

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

library.OnQuickLaunch = true;

library.Update();

}

}

Now run the application and you can see the new Document Library created inside SharePoint.

clip_image002


Document Templates

Following are the SharePoint 2010 document templates available:

Template ID

Description

100

No Template

101

Word 2003 document

103

Excel 2003 document

104

PowerPoint 2003 document

121

Word document

122

Excel document

123

PowerPoint document

111

OneNote Notebook

102

SharePoint Designer HTML document

105

ASPX Web Page

106

ASPX Web Part Page

1000

InfoPath document

Uploading Document

Now we can try uploading a document to the above library programmatically. Use the following code to upload an excel file. Make sure the application bin folder contains a file named sample.xlsx.

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet"))

{

using (SPWeb web = site.OpenWeb())

{

SPDocumentLibrary library = web.Lists["My Docs"] as SPDocumentLibrary;

SPFile file = library.RootFolder.Files.Add("sample.xlsx", File.ReadAllBytes("sample.xlsx"));

}

}

You can see that the file is uploaded into SharePoint:

clip_image004

Downloading Document

Now we can try downloading a document from the above library programmatically. Use the following code:

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet"))

{

using (SPWeb web = site.OpenWeb())

{

SPDocumentLibrary library = web.Lists["My Docs"] as SPDocumentLibrary;

SPFile file = web.GetFile(library.RootFolder.Url + "/sample.xlsx");

Stream stream = file.OpenBinaryStream();

FileStream fileStream = new FileStream("out.xlsx", FileMode.OpenOrCreate, FileAccess.Write);

int buffer = 4096;

int read = buffer;

byte[] bytes = new byte[buffer];

while (read == buffer)

{

read = stream.Read(bytes, 0, buffer);

fileStream.Write(bytes, 0, read);

if (read < buffer) break;

}

stream.Dispose();

fileStream.Dispose();

}

}

After running the program, you can see that the file is created in the bin folder.

clip_image006

This concludes the following activities:

1. Create Document Library

2. Upload a Document

3. Download a Document

References

http://www.learningsharepoint.com/2010/06/30/programatically-creating-document-sets-in-sharepoint-2010/

Summary

In this article we have the creation of document library through code. Please note that using the Document Type we can create different types of document library.

6 thoughts on “SharePoint 2010 – Create Document Library

  1. Sonu says:

    Hi Paul,
    when i create a document library and wants to publish an InfoPath form in it, then i get an error as: “InfoPath failed to publis because the default content type in the document library is not based on the form content type.”

    1. Jean Paul says:

      Hello Sonu,

      You can choose the option ‘Create new form library’ in Designer.

      Regards,
      Jean Paul

      1. Sonu says:

        Thanks for reply, but Jean, I want to create a form library in Sharepoint by code and then I want to deploy my Infopath forms on that form library, so your code create a document library, but there is any option available by which i can create a form library in SP site by C# code and I hope that the problem which i described earlier can be solved by form library, so please help me for this.

        Regards
        Sonu

        1. Jean Paul says:

          Hello Sonu,

          You are Welcome!

          As per your question (creating form library through code) you can use the template id for the purpose.

          template ids: http://www.information-worker.nl/2009/01/28/sharepoint-list-template-ids/

          Use 115 for XML Form library in your code.

          Good Luck!

          Regards,
          Jean Paul

      2. Sonu says:

        Hello Jean Paul,

        Thanks for again reply, your article is so much helpful for creating libraries in sharepoint by C# code, according to my question, i was unable to create a form library by your code, as you suggested that i have to use 115 for form library , i used but i was getting some error, so i have investigate for this then i have edited one line of code as:

        public static void CreateDocumentSetContentType(string siteUrl, string libname)
        {
        using (SPSite site = new SPSite(siteUrl))
        {
        using (SPWeb web = site.OpenWeb())
        {
        SPListTemplate listTemplate = web.ListTemplates[“Document Library”];

        SPDocTemplate docTemplate = (from SPDocTemplate dt in web.DocTemplates

        where dt.Type == 115

        select dt).FirstOrDefault();

        Guid guid = web.Lists.Add(libname, libname, SPListTemplateType.XMLForm);

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

        //Guid guid = web.Lists.Add(libname, “”, listTemplate, docTemplate);

        //SPDocumentLibrary library = web.Lists[guid] as SPDocumentLibrary;

        library.OnQuickLaunch = true;

        library.Update();
        Console.WriteLine(“Document library created successful”);
        Console.ReadLine();

        }

        }

        Now in this code , i edited only GUID’s line code with the help of msdn and now this code creates a form library on the given sharepoint site.

        Again very much thank for your help, but now I need your help that i want to publish an Infopath form in this form library by C# code, i tried to search for this, but i am able to upload this form in form library, but i want to publish so there is any method/code for publish an infopath form to given library as we can publish by wizard in Infopath form.

        Regards
        Sonu

        1. Jean Paul says:

          Hello Sonu,

          Happy to see your progress.

          Please find the InfoPath form publishing through C#
          http://tihomirignatov.blogspot.com/2009/06/submit-infopath-form-to-sharepoint-c.html

          Regards,
          Jean Paul

Leave a Reply

Your email address will not be published. Required fields are marked *