Sharepoint: How To Deal With Custom List [Add,Search]


Hi Guys, this article depends on your knowledge of what’s the concept of SharePoint List,so I already created a list called Employees under my subsite with the following columns:

FirstName

LastName

Age

Department

Salary

so the first task that we want to do is adding new list item to the Employees List,here is the Code for this target:

you will need the following namespace:

using Microsoft.SharePoint;

//to open the site 

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    if (site != null)
                    {

                                                       //to open the site ….. assuming that the list will be in the same subsite

                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))
                        {
                            SPList list = web.Lists["Employees"];
                            if (list != null)
                            {
 //Create New Empty List Item
                                    SPListItem itemNew = list.Items.Add();
 //Filling it's fields
                                    itemNew["FirstName"] = txtFirstName.Text;
                                    itemNew["LastName"] = txtLastName.Text;
                                    itemNew["Age"] = txtAge.Text;
                                    itemNew["Department"] = ddlDepartments.SelectedItem.Text;
                                    itemNew["Salary"] = txtSalary.Text;

 // to enable you to update the list
                                    web.AllowUnsafeUpdates = true;
 //Updating......
                                    itemNew.Update();
                            }
                        }

                    }
                }
the second Task we need now to search and navigate between the list items, so how in your opinions, I'll say it now
look, this search will work with CAML Queries we will Create template for every search Criteria ie. in our screen we will search 
by FirstName and LastName as ex, so look at this code:

string FirstNameTemplate = string.Format("<Contains>" +
                                         "<FieldRef Name=\"FirstName\" />" +
                                          "<Value Type=\"Text\">{0}</Value>" +
                                       "</Contains>", txtFirstName.Text);

string LastNameTemplate = string.Format("<Contains>" +
 "<FieldRef Name=\"LastName\" />" +
 "<Value Type=\"Text\">{0}</Value>" +
 "</Contains>", txtLastName.Text);

string TrueCondition = "<Neq>" +
 "<FieldRef Name=\"FirstName\" />" +
 "<Value Type=\"Text\">asamir226644</Value>" + //Dummy Data
 "</Neq>";

string FirstName = txtFirstName.Text != string.Empty ? FullNameTemplate : TrueCondition;
string LastName = txtLastName.Text != string.Empty ? LastNameTemplate : TrueCondition;

string CAMLQueryemplate = string.Format("<Where>" +
 "<And>" +
 "{0}" + //FirstName
 "{1}" + //LastName
 "</And>" +
 "</Where>", FirstName, LastName);

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    if (site != null)
                    {
                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))
                        {
                            SPList list = web.Lists["Employees"];
                            if (list != null)
                            {
                                SPQuery query = new SPQuery();
                                string str = string.Format(CAMLQueryemplate);
                                query.Query = str;
                                DataTable dataTable = list.GetItems(query).GetDataTable();
                                gvEmployees.DataSource = dataTable;
                                gvEmployees.DataBind();
                            }
                        }
                    }
                }
Advertisements

3 thoughts on “Sharepoint: How To Deal With Custom List [Add,Search]

  1. Very Good article, but I have a note about SPContext.Current.Site, and SPContext.Current.Web usage, because you have the custom list in the current site, then you can use the following code:

    SPList list = SPContext.Current.Web.Lists[“Employees”];

    So no need to open site collection, then web site.

    Best Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s