Powered By Blogger

Jul 4, 2012


Create a SharePoint Document Library using c#

This blog post describes how to create a SharePoint Document Library using c#.  Its a very simple process.


First you should create a SharePoint console application project using Microsoft Visual Studio 2010.Name the project as 'DocumentLibraryCreation'.





Then insert the below code within the main method. Make sure to input the correct 'Site name'. You can and a name for the Document Library and a template type as preferred.


using (SPSite site = new SPSite("http://sp-sohani:8118/sites/Gouri_Sohani/"))
            {
 
                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 Document Library""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 called ' My Document Library' has created inside the SharePoint site under Document Libraries.




This is a list of Document Templates available in SharePoint 2010.



















No comments:

Post a Comment