Prash's Blog

Create SPList grouped by a field in SPView November 13, 2009

Filed under: SharePoint — prazjain @ 3:06 pm
Tags: ,

Here I do a groupby on SPList so that it can be viewed in TreeView UI with collapsible groups.

Step through of code:

  1. Get the reference to SPList
  2. Define a CAML query to group the list by “Title” of the SPListItem. More than one field can be added by adding as many <FieldRef ../> tags inside the same GroupBy Tag.
  3. Add the fields you would want to see in the view, and set the view as default view for the list.

Code snippet:

/// <summary>

/// Creates a new default view for the history list. This default view is grouped by title of the list.

/// </summary>

/// <param name="web"></param>

private void SetupGroupByView(SPWeb web)


SPList historyList = web.Lists["History"];

string viewQuery = @"

<GroupBy Collapse=""True"" GroupLimit=""100"">

<FieldRef Name=""Title"" Ascending=""True""/>


StringCollection viewFields = new StringCollection();


historyList.Views.Add("Grouped By Title", viewFields, viewQuery, 100, true, true, SPViewCollection.SPViewType.Html, false);



Programmatically add fields to SPList and its Default View

Filed under: SharePoint — prazjain @ 2:33 pm
Tags: ,

I would show using a code snippet how to create a new SPList in SPWeb, add new fields to the list and then add those fields in the default view of the list.

Step through of the code:

  1. Create a new list in SPWeb
  2. Add new fields in the list
  3. Add the fields in the default view of the list

I have taken an example of creating a text field, you can as well use any data type i.e Checkbox, Choice etc.

Code Sample:

/// <summary>

/// Sets up the list

/// </summary>

/// <param name="web"></param>

private Guid SetUpHistoryList(SPWeb web)


Guid historyListGuid = web.Lists.Add("History", "History", SPListTemplateType.GenericList);


SPList historyList = web.Lists[historyListGuid];

historyList.Fields.Add("Information", SPFieldType.Text, false);

SPView defaultView = historyList.DefaultView;



return historyListGuid;



Programmatically Add SPList as Webpart on SPWeb using ListViewWebPart

Filed under: SharePoint — prazjain @ 2:11 pm
Tags: ,

Here I would show how to add a list (that you already have in your site), on your default.aspx page for your web.

Step through of the code:

1) Get the file reference to the page where you want to display the webpart.

2) Check if the file exists, if not then we do not make any changes.

3) Get the reference to your list

4) Create a ListViewWebPart

5) Set ZoneID for ListViewWebPart to where you want to display the webpart i.e Left, Top etc.

6) Set ListName to the list you want to display in the webpart.

7) Set the ViewGuid to the guid of the View you are adding in the webpart

8) Get SPLimitedWebPartManager for the web.

9) Add the webpart to the SPLimitedWebPartManager

Code Sample

/// <summary>

/// Sets up the web part to display in default view

/// </summary>

/// <param name="web"></param>

private static void SetUpWebparts(SPWeb web)


SPFile file = web.GetFile("default.aspx");

if (file.Exists)


SPList projectHistoryList = web.Lists[StringConstants.ProjectHistory];

ListViewWebPart wp = new ListViewWebPart();

wp.ZoneID = "Top";

wp.ListName = projectHistoryList.ID.ToString("B").ToUpper();

wp.ViewGuid = projectHistoryList.DefaultView.ID.ToString("B").ToUpper();

SPLimitedWebPartManager coll = web.GetLimitedWebPartManager(web.Url + "default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

coll.AddWebPart(wp, "Top", 1);