Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:

Recycle Bin | MMS | ManagedVsCrawledProperties |

Friday, June 29, 2012

Create a list in different ways.

Creating lists using all object models:

Creating a custom list is a common requirement for each SharePoint Projects. So, we need to know about how to create. Here I am sharing most of the ways how to create a custom lists in SharePoint like using SharePoint Object model, Managed .Net Client Object and ECMA Script object model. Below, I am sharing how to create lists in different scenarios using the above mentioned object models.

I keep it all object models in one place to remind and get the difference easily.

SharePoint Client Object Model:
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;

namespace Managed_ClientOM_CreateAList
    class Program
        static void Main(string[] args)

        private static void CreateList()
            string webUrl = "http://nb16";
            ClientContext context = new ClientContext(webUrl);
            Web web = context.Web;
            //Add list to the site
            ListCreationInformation listCreation = new ListCreationInformation();
            listCreation.Title = "My List";
            listCreation.TemplateType = (int)ListTemplateType.GenericList;
            listCreation.Description = "My custom test list";
            List list = web.Lists.Add(listCreation);
            //Add fields to the list
            Field firstName = list.Fields.AddFieldAsXml("", true, AddFieldOptions.DefaultValue);
            Field lastName = list.Fields.AddFieldAsXml("", true, AddFieldOptions.DefaultValue);
            Field branchID = list.Fields.AddFieldAsXml("", true, AddFieldOptions.DefaultValue);
            //Add listitems to the list.
            ListItemCreationInformation listItemCreation = new ListItemCreationInformation();
            ListItem listItem = list.AddItem(listItemCreation);
            listItem["FirstName"] = "Praveen";
            listItem["LastName"] = "Battula";
            listItem["BranchID"] = 1;
            listItem["Title"] = "Praveen Battula";
            listItem = list.AddItem(listItemCreation);
            listItem["FirstName"] = "Rare";
            listItem["LastName"] = "Solutions";
            listItem["BranchID"] = 2;
            listItem["Title"] = "Rare Solutions";


SharePoint ECMAScript Object Model:

SharePoint Server Object Model:

using (SPSite siteCollection = new SPSite(SPContext.Current.Site.Url.ToString()))
using (SPWeb spWeb = siteCollection.OpenWeb())
spWeb.AllowUnsafeUpdates = true;
spWeb.Lists.Add(“ListName”, “List Discription”, SPListTemplateType.PictureLibrary);
//You can change list template type (SPListTemplateType) ” Tasks, Calendar, Document Library etc.” as per your requirements, all OOTB list templates are listed here
spWeb.AllowUnsafeUpdates = false;

No comments:

Post a Comment