Programmatically create Digital Asset Library in SharePoint 2010

Steps Involved:

1.       Open Visual Studio 2010 by going Start | All Programs | Microsoft Visual Studio 2010 | Right click on Microsoft Visual Studio 2010 and click on Run as administrator.

2.       Go to File tab, click on New and then click on Project.

3.       In the New Project dialog box, expand the Visual C# node, and then select the Windows node.

4.       In the Templates pane, select Console Application.

5.       Enter the Name and then click OK.

6.       In the solution explorer, right click on the solution and then click on Properties.

7.       Select the Application tab, check whether “.Net Framework 3.5” is selected for Target Framework.

8.       Select the Build tab, check whether “Any CPU” is selected for Platform Target.

9.       In the solution explorer, right click on the References folder and click on Add Reference.

10.    Add the following references.

1.       Microsoft.SharePoint.dll (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll)

11.    Double click on Program.cs and add the following Namespaces.

1.       using Microsoft.SharePoint; 

12.    Replace Program.cs with the following code snippet.


 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

 

namespace Console

{

    class Program

    {

        string listName = "Digital Assets";

        string description = "My Digital Asset Library for image, video and audio";

 

 

        public static void Main(string[] args)

        {

            using (SPSite oSite = new SPSite("http://serverName/sites/VJTesting/Asset/"))

            {

                using (SPWeb oWeb = oSite.OpenWeb())

                {









                    if (oWeb.Lists.TryGetList(listName) != null)

                    {

                        // SPListTemplate class - Used to represent the list definition or a list template

                        // oWeb.ListTemplates - Used to get the "Asset Library" list template

                        SPListTemplate oTemplate = oWeb.ListTemplates["Asset Library"];

                        //Creates a new list with Title, Descrription and List Template "Asset Library" type

                        oWeb.Lists.Add(listName, description, oTemplate);

                    }

                    else

                    {

                        System.Console.WriteLine(listName + " already exists in the " + oWeb.Title + " site");

                        System.Console.ReadLine();

                    }

                }

            }

        }

    }

}

 


13.       In the solution explorer, right click on the solution and click on Build.

14.       Hit F5.