Programming SharePoint Lists

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

In this post I give you a basic walkthrough of programming SharePoint lists.
Introduction
My Blog has been quite for sometime now. I was busy in sharpening my skills on Microsoft Office SharePoint Server 2007, well known as MOSS 2007.This is awesome product from Microsoft stable.The all in one product which supports the following features out of the box.
  • Web Content Management
  • Enterprise Search
  • Portal Services
  • Business Intelligence
  • Custom Workflows
Another good thing is that it has very rich object model.Whatever you can do from its GUI , the same thing can be achieved via its api's. Last but not least this platform is based on ASP.net Technology , so there wont be a steep learning curve for existing asp.net developers you just need to get acquainted to the new terms and its rich object model. Each and every object starts with SP as prefix. To know more about it , please visit MOSS Official Website In this post I shall be talking about SharePoint Lists. To keep things simple for new kids of SharePoint development, I will be speaking in context of terms which they are familiar with. So fasten your seat belts and enjoy the joy ride of SharePoint.
SharePoint Lists
A SharePoint List is more or less similar to a table in the database. How you have rows and columns in the table , a SharePoint lists also have the rows and columns. The whole record in the list is called ListItem. The SharePoint List provides more functionality than the database tables.To name few , you can create versioning of the list items, attach a custom workflow on the list and many more.They play a vital role in SharePoint architecture. To store any custom data for your custom SharePoint solutions , custom lists are your best bet.The user interface for creating, updating and deleting the list items comes as built in feature of SharePoint. I will show you how you can achieve the same through its rich object model.
SharePoint Object Model
To perform Add,Update and Delete operations on your custom list, you would require the following objects

Now let us see some code in action.

To keep things short and simple, the below example assumes that there is a custom list by the name "Customer" which has two columns
  1. Title (This column gets auto created whenever you create new custom list.It will also be our key column to retrieve the ListItem
  2. ContactPerson (This column holds the name of the Customer)
ADDING ITEM IN CUSTOM SHAREPOINT LIST

private static void AddNewItem()

{

string objUrl; // the url of the sitecollection

string objName; // the path of the site

string objList; // the name of the list

objUrl=”http://Yourservername:Yourportnumber/sites/YoursiteCollectionName/”;

objName = "/sites/YourSiteName/";

objList = "Customer"; //it is the name of list

SPSite objsiteCollection = new SPSite(objUrl);

SPWeb objSite = objsiteCollection.AllWebs[objName];

SPListItemCollection objListItems = objSite.Lists[objList].Items ;

SPListItem objListItem;

objListItem = objListItems.Add();

objListItem["Title"] = "Northwind Traders";

objListItem["ContactPerson"] = "Alfred Pinto";

objListItem.Update();

}

Let us do the dry run of the code. Initially all required variables are declared, please change the values of the variables that has prefix “Your” to suit your setup.

The first object is SPSite , which is the sitecollection and SPWeb is the site present in that site collection. SPListItemCollection is the object which contains all the list items present in the custom list. Finally SPListItem represent the individual ListItem present in the ListItemCollection object.

We use the Add method of ListItemCollection object to add new item. Please notice the syntax how we are referencing the columns name in the ListItem object to assign the values to the columns.Finally Update method of ListItem is called to update the ListItemCollection.

UPDATING ITEM IN CUSTOM SHAREPOINT LIST

private static void UpdateItem(object itemTobeUpdated)

{

string objUrl; // the url of the site

string objName; // the name of the site

string objList; // the name of the list

objUrl=”http://Yourservername:Yourportnumber/sites/YoursiteCollectionName/”;

objName = "/sites/YourSiteName/";

objList = "Customer";

SPSite objsiteCollection = new SPSite(objUrl);

SPWeb objSite = objsiteCollection.AllWebs[objName];

SPListItemCollection objListItems = objSite.Lists[objList].Items;

var lstitem=(from SPListItem spItem in objListItems where spItem

["Title"].ToString() == Convert.ToString(itemTobeUpdated )

select spItem ).FirstOrDefault();

SPListItem lstItm = (SPListItem)lstitem ;

lstitem["ContactPerson"] = "Albert Einsten";

lstitem.Update();

}

The first few lines of the above code remains the same. We retrieve the SPListItem object from the SPListItemCollection using Linq. SharePoint has its native query language to query the data from custom lists. It is called CAML (Collaborative Application Markup Language). There is also the class name called SPQuery in SharePoint object model.To keep things simple and straight forward I haven’t used it in this post. So after retrieving the SPListItem via Linq we assign the new value to its column in the similar syntax. Calling the Update method updates the SPListItem in the SPListItemCollection.

DELETING ITEM IN CUSTOM SHAREPOINT LIST

private static void DeleteItem(object itemTobeDeleted)

{

string objUrl; // the url of the site

string objName; // the name of the site

string objList; // the name of the list

objUrl=”http://Yourservername:Yourportnumber/sites/YoursiteCollectionName/”;

objName = "/sites/YourSiteName/";

objList = "Customer";

SPSite objsiteCollection = new SPSite(objUrl);

SPWeb objSite = objsiteCollection.AllWebs[objName];

SPListItemCollection objListItems = objSite.Lists[objList].Items;

int itemsCount = objListItems.Count;

//loop the list items collection to find the desired item

//if found then delete it and exit from loop

for (int i = 0; i < itemsCount ; i++)

{

SPListItem objListItem=objListItems[i];

if (objListItem["Title"].ToString()==Convert.ToString(itemTobeDeleted))

{

objListItems.Delete(i);

return;

}

itemsCount--;

}

In the above code we rotate in loop of SPListItemCollection until we find the matching SPListItem that we want to delete. Once the item is found we call Delete method of SPListItemCollection by passing the index value in the parameter.

Conclusion
This was your first baby step towards SharePoint programming.As discuss above , it has very rich api and sky is the limit. On a concluding note , all SharePoint objects must be destroyed explicitly by calling their Dispose method. In the above examples I have purposely not used it so that it can be short and precise. I hope you must have enjoyed reading this post. Let me know your comments.
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)