Working with SharePoint collection objects

Goldytech
Posted by in SharePoint category on for Beginner level | Views : 6589 red flag

This article will teach you how to work with SharePoint collection objects that are based on hirearchy in SharePoint architecture.
Introduction
Continuing further from my last post of Programming SharePoint Lists , in this post I shall be talking about the SharePoint collection objects. Though there are lot of collection objects available with SharePoint object model. I shall be introducing to you on the collection objects which comes under the hierarchy of the SharePoint architecture. The below diagram represents the hierarchy SharePoint objects
SharePoint collection objects
The solution that I will be showing in this post , is a simple web page which has a text box to show the site url and two cascading dropdownlists for Webs and Lists. When the user selects the item from Web dropdown list the Lists dropdown gets populated with items corresponding to that web.There is also a GridView which displays the fields of the selected list. So let us jumpstart with the code. First let us get the webs list based on the site url.

using (SPSite site = new SPSite(this.TxtSiteCollection.Text))
            {

                this.CboWebs.DataTextField = "Title";
                this.CboWebs.DataValueField = "ID";
                this.CboWebs.DataSource = site.AllWebs;

                this.CboWebs.DataBind();
                CboWebs.Items.Insert(0, new ListItem("Select Web"));

            }

The AllWebs property of the SPSite object is of SPWebCollection Type. So now we have Webs(Top level site and subsites) based on SPSite. Now let us look into how to get the Lists that are there is the selected Web

using (SPSite site = new SPSite(this.TxtSiteCollection.Text))
               {
                   using (SPWeb web = site.OpenWeb(new Guid(this.CboWebs.SelectedValue)))
                   {
                       this.CboLists.DataTextField = "Title";
                       this.CboLists.DataValueField = "ID";
                       this.CboLists.DataSource = web.Lists;
                       this.CboLists.DataBind();
                       this.CboLists.Items.Insert(0, "Select List");

                   }
              }

The Lists property of the SPWeb object is of SPListCollection Type. It is always a good practice to open every SharePoint object with its GUID. In the above code I am opening the Web by using its GUID with the OpenWeb method, though there are other ways of opening the web by appending with the site url by using OpenWeb method overloading operators. So now we have filled the Lists dropdown also , let us now get the fields based on the selected List. Here I would like to draw the readers attention that by default SharePoint creates lot of additional hidden and read only fields for the support of its various features. So this will display all those unnecessary fields that are not relevant to the developer  but there is way out of it. I have used Linq to Objects and filter those fields by setting those properties to false. Have look at the code below

using (SPSite site = new SPSite(this.TxtSiteCollection.Text))
               {
                   using (SPWeb web = site.OpenWeb(new Guid(this.CboWebs.SelectedValue)))
                   {

                       SPList list;
                       list = web.Lists[new Guid(this.CboLists.SelectedValue)];

                       var FldsLst = (from SPField field in list.Fields
                                      where field.Hidden == false && field.ReadOnlyField == false
                                      select new { FieldName = field.Title, FieldType = field.Type.ToString() }).ToList();
                       this.GrdFields.DataSource = FldsLst;
                       this.GrdFields.DataBind();

                   }
               }

Closure
SharePoint is a great technology to work with, its rich object model allows to create great applications on this platform.Download the sample code of this post from here.

Tip: Always use Linq to query the SharePoint collection objects rather than looping in them. It will save those great CPU cycles.

Page copy protected against web site content infringement by Copyscape

About the Author

Goldytech
Full Name: Muhammad Afzal Qureshi
Member Level: Bronze
Member Status: Member
Member Since: 8/4/2009 10:58:17 PM
Country: India

http://goldytech.wordpress.com
Hello Everyone Myself Muhammad Afzal , aka GoldyTech. Thats my pen name. I reside in India and work as a solution Architect on .NET platform. I hope you must have enjoyed reading my blog. Please leave your comments or suggestions good or bad and help me to improve

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)