Go to DotNetFunda.com
 Online : 55 |  Welcome, Guest!   Login
 Skip Navigation Links Home > Articles > SharePoint > Working with SharePoint collection objects
  Win at least 8 gifts every month now! >> Top Performers of this month.   


Notice: If page content has been copied from other sources without proper permission, kindly let us know with details from where it has been copied for further action.

All Articles | Submit Article |

Working with SharePoint collection objects

 Posted on: 10/30/2009 8:33:56 AM by Goldytech | Views: 378 | Category: SharePoint | Level: Beginner | Print Article
This article will teach you how to work with SharePoint collection objects that are based on hirearchy in SharePoint architecture.

Webmaster says - If this article content has been copied from other websites without proper permission, kindly let us know with details from where it has been copied for further action.

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.


Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:10 year(s)
Home page:http://goldytech.wordpress.com
Member since:Tuesday, August 04, 2009
Biography: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
 Latest post(s) from Goldytech

   ◘ How to read csv file and import its data with Linq posted on 11/17/2009 2:04:30 AM
   ◘ Working with SharePoint collection objects posted on 10/30/2009 8:33:56 AM
   ◘ WCF and JSON Fusion posted on 10/28/2009 2:57:44 AM
   ◘ Take a scoop of Silverlight Toolkit Pie Chart posted on 10/22/2009 8:11:58 AM
   ◘ Programming SharePoint Lists posted on 10/14/2009 2:04:00 AM


Submit Article

About Us | The Team | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)