A SharePoint list is more or less similar to a table in the database. How do you have rows and columns of the table, a list of SharePoint also have rows and columns. The full record in the list is called ListItem. The list of SharePoint provides more functionality that the database tables.To name a few, you can create versions of the items in the list, attach a custom workflow in the list and many more.They play a key role in the architecture of SharePoint. To store the custom data to your custom solutions SharePoint custom lists are the best bet.The user interface to create, update and remove items from list is presented as an integrated feature of SharePoint. I will show you how you can achieve the same through its rich object model.
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, SPWeb sitecollection it is and where it is now in that site collection. SPListItemCollection is the object that contains all the elements of the list in the custom list. Finally SPListItem represent the person in the ListItem object ListItemCollection.
We use the Add method of ListItemCollection object to add a new item. Note the syntax of the way it refers to the name of columns in the ListItem object to assign values to update method is called to columns.Finally ListItem ListItemCollection update.
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 lines of code above is still the same. We retrieve the object of the SPListItem SPListItemCollection using Linq. SharePoint has its native query language query the data from custom lists. It's called CAML (Collaborative Application Markup Language). There is also the name of the class called in SharePoint model.To SPQuery designed to keep things simple and direct I have not used in this post. So after recovering through Linq SPListItem assign the new value to your column in the syntax. When you call the Update method updates the SPListItemCollection SPListItem.
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 previous code that revolve SPListItemCollection loop until we find the correspondence SPListItem we want to remove. Once the item is to call the Delete method of SPListItemCollection passing the index value in the parameter.
0 comments:
Post a Comment