How to display List with sharepoint standard view?


  • Hi,

    I am very new in Sharepoint.

    I have a list student as follows:

    And I need to filter/query the above list to only display all the student with StudentID > 777448.

    I have no problem by querying the list, I can use LINQ something like this

    var students = from s in ctx.Student
                    where int.Parse(s.StudentID) < 777448
                    select new { s.FirstName, s.LastName, s.StudentID };

    And I can display the result using ListBox like this

    foreach (var student in students)
          ListBox_View.Items.Add(String.Format("{0} {1}", student.FirstName, student.LastName));

    But I don't want use ListBox as a view for my result. I wanna have display exactly like this

    What should I do? Thanks

    2012年6月21日 下午 03:06


  • Create   a new view as standard and then add columns and their numbering accordingly.

    You can get the new view through list settings.

    2012年6月21日 下午 03:40
  • Hi ,

    If you want to filter record and want to show in similar way ,create a view for list and add filtering condition in newly created view.

    to create view programatically use refer

    For out of the box refer


    • 已編輯 ashar2k11 2012年6月21日 下午 03:44
    2012年6月21日 下午 03:41

  • You have two Options here

    1. Non - Code solution - In your student List Create a View and filter it by student Id "is greater than" 777448. Then you get the resulting view.

    2. By Code - You can create this view and get the view in code and then bind to any control (listview control in your case)

    SPList list = web.Lists["Student"];

    ListViewWebPart lvwp = new ListViewWebPart();

    lvwp.ListName = list.ID.ToString("Student").ToUpper();
    lvwp.ViewGuid = list.Views[ViewOfSourceList].ID.ToString("My student View").ToUpper();

    or you can create view progrmatically like Ashar2k11 said using

    2012年6月21日 下午 03:46
  • Here is step by step

    Create, modify, or delete a view

    You'll get to know about different types of view too

    Akhilesh Nirapure

    2012年6月21日 下午 03:47
  • Hi all,

    Thanks for the reply.

    I have seen the link that Ashar2k11 mentioned above.

    the example in the blog is using CAML. Is it possible to create the similiar way using LINQ?

    2012年6月21日 下午 05:14
  • Wapt,

    Are you looking for API which will create CAML query from LINQ to SharePoint

    using (var dataContext = new PartsSiteDataContext(@http://localhost/sites/parts))
         StringBuilder stringBuilder = new StringBuilder();

         TextWriter textWriter = new StringWriter(stringBuilder);

         dataContext.Log = textWriter;

    var departments = from department in dataContext.Departments
                 where department.Title.StartsWith("deptname1")
                 select department;

    Assert.True(departments.Count() > -1);

    string queryLog = stringBuilder.ToString();


    You'll see underline CAML Query generated from LINQ to SharePoint.

    Akhilesh Nirapure

    2012年6月22日 下午 07:16
  • Hello Akhilesh Nirapure,

    Thanks for the reply but its not what Im looking for. My goal is simple..

    I have list as follows:

    Now I try to create a webpart. In my webpart I have a button. Once I click the button, then this webpart will show me the upper list but only rows which have StudentID > 777448. So as the result I want to have the same as below picture..

    Can you help me the approach to do that programmatically? I tried using ListViewByQuery. it works but I can't use filter and sort :(

    • 已編輯 wapt49 2012年6月23日 下午 06:21
    2012年6月23日 下午 06:20
  • Hi Voctor,

    Thanks for the Reply. I think SPGridView is great if for creating the new list. But how if I only want to update existing list, instead of creating new one?

    • 已編輯 wapt49 2012年6月23日 下午 07:24
    2012年6月23日 下午 07:24
  • But how if I only want to update existing list, instead of creating new one?

    Then you should create (or modify existing) view of list, as almost everyone above adviced you!

    My contributions: SharePoint 2010 Solution Installer

    2012年6月23日 下午 07:54

  • Hi Aviw,

    I am sorry, but I tried the advise from Ashar2k11 from this blog , however the result I can see only if I go to list setting, NOT in webpart. Please see picture below (sorry in german)

    I need the result only updating the list in my webpart (please see the picture below)

    And this is my code

     protected void btn_Show_Click(object sender, EventArgs e)

    SPWeb web = SPContext.Current.Web; SPList list = web.Lists["Student"]; SPViewCollection allviews = list.Views; string viewName = "Test View"; System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); viewFields.Add("First Name"); viewFields.Add("Last Name"); viewFields.Add("StudentID"); string myquery = "<Where><Gt><FieldRef Name='StudentID' /><Value Type='Text'>777446</Value></Gt></Where>"; allviews.Add(viewName, viewFields, myquery, 100, true, false);


    Can you help me what should I do for the next step, so I can see the result directly in the webpart instead of going to the list setting?

    • 已編輯 wapt49 2012年6月23日 下午 08:31
    2012年6月23日 下午 08:30
  • What is your primary task?

    Is that display filtered list view after button clicked?

    If so, you can use your own web part and add ListViewByQuery control. It looks like standard SharePoint list view.

    On button click you have to change query only.

    My contributions: SharePoint 2010 Solution Installer

    2012年6月23日 下午 10:25
  • yes my goal is to display filtered list view after button clicked.

    I tried using ListViewByQuery as well. However the disadvantage by using ListViewByQuery is no paging and sorting. Once I try to filter or sort the resulting list, the list suddenly gone..

    This is my code

    SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["Student"];
    ListViewByQuery lvbq; lvbq = new ListViewByQuery();
    lvbq.List = list;
    SPQuery query = new SPQuery(list.DefaultView);
    query.Query = "<Where><Gt><FieldRef Name='StudentID' /><Value Type='Text'>777444</Value></Gt></Where>";
    lvbq.Query = query;

    So, I am still looking for another approach. Is it possible using SPView or ListViewWebPart to accomplish my goal?

    • 已編輯 wapt49 2012年6月23日 下午 10:59
    2012年6月23日 下午 10:58
  • you can just use the filter in list settings without use any code !!

    you need to add the list to any page like a web part, then edit the web part you add and click on "Edit the current view"

    here you just add the filter you need under Filter group

    why u need to write code here ?

    2012年6月24日 上午 07:47