RSS

Programming Lists using Code in SharePoint 2010


Published by Eugene Martynov
Publish date: 11/3/2011 5:33:37 AM

Programming Lists using Code in SharePoint 2010

I continue to examine the SharePoint 2010 and today I want to note how to programming lists using code. There are two approaches: programming lists using code and CAML. CAML will be described in the next article.

Examples listed below are designed for Console Application.

1. Create List:

private static void CreateList(string listName)
		{
			using (var site = new SPSite(SiteUrl))
			{
				var web = site.RootWeb;

				var listId = web.Lists.Add(listName, string.Empty, SPListTemplateType.GenericList);
				var list = web.Lists[listId];

				list.OnQuickLaunch = true;
				list.Update();

				var title = list.Fields["Title"];
				title.Title = "Name";
				title.Update();

				var empFieldName = list.Fields.Add("Employee", SPFieldType.Boolean, false);
				var rateFieldName = list.Fields.Add("Salary/Rate", SPFieldType.Currency, true);
				var bioFieldName = list.Fields.Add("Bio", SPFieldType.Note, false);

				var view = list.DefaultView;
				view.ViewFields.Add(empFieldName);
				view.ViewFields.Add(rateFieldName);
				view.ViewFields.Add(bioFieldName);
				view.Update();
			}

First off all I initialize a new instance of the SPSite class based on the specified absolute URL. SiteUrl is a string that specifies the absolute URL for the site collection. The SPSite instance helps us to get the root Web site of the site collection – web variable. Lists property of the SPWeb gets the collection of all lists that are contained in the Web site. Add method creates a list with the specified title, description, and list definition type and returns a Guid that identifies the new list. Using the Guid I get SPList instance of the just created list and change some properties.

Fields property gets the collection of field objects that represents all the fields in the list.

2. Delete List:

private static void DeleteList(string listName)
		{
			using (var site = new SPSite(SiteUrl))
			{
				var web = site.RootWeb;
				var list = web.Lists.TryGetList(listName);
				if (list != null)
					list.Delete();

				Console.WriteLine("New list has been deleted!"); 
			}
		} 

3. Create Content type

private static void CreateContentType()
	    {
	        using (var site = new SPSite(SiteUrl))
	        {
	            var web = site.RootWeb;

	            var empFieldName = web.Fields.Add("Employee", SPFieldType.Boolean, true);
	            var employee = web.Fields.GetFieldByInternalName(empFieldName);
	            employee.Group = "PluralSight";
                employee.Update();

                var rateFieldName = web.Fields.Add("Salary/Rate", SPFieldType.Currency, false);
                var rate = web.Fields.GetFieldByInternalName(rateFieldName);
                rate.Group = "PluralSight";
                rate.Update();

                var bioFieldName = web.Fields.Add("Bio", SPFieldType.Note, false);
                var bio = (SPFieldMultiLineText) web.Fields.GetFieldByInternalName(bioFieldName);
                bio.Group = "PluralSight";
	            bio.NumberOfLines = 6;
	            bio.RichText = false;
                bio.Update();

	            var item = web.ContentTypes["Item"];
	            var author = new SPContentType(item, web.ContentTypes, "Authors") {Group = "PluralSight"};

	            var title = author.FieldLinks["Title"];
	            title.DisplayName = "Name";

                author.FieldLinks.Add(new SPFieldLink(employee));
                author.FieldLinks.Add(new SPFieldLink(rate));
                author.FieldLinks.Add(new SPFieldLink(bio));

	            web.ContentTypes.Add(author);
                author.Update();
	        }
	    } 

The first step is to create fields for the Content type. At this example I use List property of the SPWeb object instead of SPList. Then the SPContentType object is created that contains the instance of a content type I am going to use as parent one. Also to create a new content type, the collection and title should be specified. The last steps are to add the created fields to the just created content type and add it to the web site.

4. Delete Content type

private static void DeleteContentType()
	    {
            using (var site = new SPSite(SiteUrl))
            {
                var web = site.RootWeb;
                var autor = web.ContentTypes["Authors"];
                if (autor!= null)
                    autor.Delete();

                DeleteField(web, "Employee");
                DeleteField(web, "Salary_x002F_Rate");
                DeleteField(web, "Bio");
            }
	    }

	    private static void DeleteField(SPWeb web, string name)
	    {
	        if (web.Fields.ContainsField(name))
	        {
	            var field = web.Fields.GetFieldByInternalName(name);
                field.Delete();
	        }
	    } 

5. Create List with Content Type

private static void CreateListWithCp()
	    {
	        using(var site = new SPSite(SiteUrl))
	        {
	            var web = site.RootWeb;

	            var listId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
	            var list = web.Lists[listId];
	            list.OnQuickLaunch = true;
	            list.ContentTypesEnabled = true;

	            var author = web.ContentTypes["Authors"];
	            list.ContentTypes.Add(author);
                list.ContentTypes.Delete(list.ContentTypes["Item"].Id);

                list.Update();

	            var view = list.DefaultView;
                view.ViewFields.Add("Employee");
                view.ViewFields.Add("Salary/Rate");

                view.Update();
	        }
	    }

 The examples were taken from PluralSight site. 


Tags: sharepoint 2010

Back to Home
blog comments powered by Disqus

Copyright © 2011 Eugene Martynov, CSS Templates by Inf Design