Sunday, August 12, 2012

Create List Definition for Picture Library - C#


If you want to do it in Visual Studio, you're unable to see this definition in the drop down list. However it is very easy to do it:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint; 

namespace Console
{
    class Program
    {
        string listName = "Picture Library";
        string description = "My  Picture Library "; 

        public static void Main(string[] args)
        {
            using (SPSite oSite = new SPSite("http://contoso.com"))
            {
                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 "Picture Library" list template
      
                        SPListTemplate oTemplate = oWeb.ListTemplates["PictureLibrary"];

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

                        oWeb.Lists.Add(listName, description, oTemplate);
                    }
                    else
                    {
                        System.Console.WriteLine(listName + " already exists in the " + oWeb.Title + " site");
                        System.Console.ReadLine();
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment