Programming against lists using the Client Object Model

Hai,

Here will see programming against lists using the Client Object Model. To do develop applications against SharePoint list we have to use Microsoft.SharePoint.Client namespace. To add this reference to your project just go to c:\\Program Files\\Common Files\\Microsoft Shared\\Web ServerExtensions\\14\\ISAPI and Add Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll DLL files.

Before starting coding against list first will create a simple list using ShapePoint site. To do this go to SiteActions –> View All Site Content –>click Create –> Lists –> Custom List. Enter Products as the name of the list and click Create. After the site is created, we???ll add two more columns . To add two columns, click the List tab, and then List Settings –> Create Column. Add a column named Product_SKU and another column named Price, leave both columns with the default ???Single line of text??? type.

Now we have a list called Products with Title/Product_SKU/Price columns, will create a Client Object Model application to add items into the list.

\"\"/
<Window x:Class=\"WPF_Lists_ClientObjectModel.MainWindow\"
          xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
          xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
          Title=\"MainWindow\" Height=\"350\" Width=\"525\">
      <Grid>
          <Label Content=\"Product Name\" Height=\"28\" HorizontalAlignment=\"Left\" Margin=\"76,64,0,0\" Name=\"lblProductName\" VerticalAlignment=\"Top\" />
          <Label Content=\"Product SKU\" Height=\"28\" HorizontalAlignment=\"Left\" Margin=\"76,106,0,0\" Name=\"lblProductSKU\" VerticalAlignment=\"Top\" />
          <Label Content=\"Product Price\" Height=\"28\" HorizontalAlignment=\"Left\" Margin=\"76,154,0,0\" Name=\"lblProductPrice\" VerticalAlignment=\"Top\" />
          <TextBox Height=\"23\" HorizontalAlignment=\"Left\" Margin=\"196,68,0,0\" Name=\"txtProductName\" VerticalAlignment=\"Top\" Width=\"231\" />
          <TextBox Height=\"23\" HorizontalAlignment=\"Left\" Margin=\"196,112,0,0\" Name=\"txtProductSKU\" VerticalAlignment=\"Top\" Width=\"231\" />
          <TextBox Height=\"23\" HorizontalAlignment=\"Left\" Margin=\"196,158,0,0\" Name=\"txtProductPrice\" VerticalAlignment=\"Top\" Width=\"231\" />
          <Button Content=\"Load\" Height=\"23\" HorizontalAlignment=\"Left\" Margin=\"211,210,0,0\" Name=\"btnLoad\" VerticalAlignment=\"Top\" Width=\"75\" Click=\"btnLoad_Click\" />
          <Button Content=\"Clear\" Height=\"23\" HorizontalAlignment=\"Left\" Margin=\"330,210,0,0\" Name=\"btnClear\" VerticalAlignment=\"Top\" Width=\"75\" Click=\"btnClear_Click\" />
      </Grid>
  </Window>

Double-click the Clear button and add the following code to clear the fields:

private void btnClear_Click(object sender, RoutedEventArgs e)
          {
              txtProductName.Text = string.Empty;
              txtProductPrice.Text = string.Empty;
              txtProductSKU.Text = string.Empty;
          }

Double-click the Load button and add the following code to add a new record to a SharePoint list 

private void btnAdd_Click(object sender, RoutedEventArgs e)
          {
              string siteURL = \"http://siva-pc:46006\";
 
              ClientOM.ClientContext mySPContext = new ClientOM.ClientContext(siteURL);
              ClientOM.List productList = mySPContext.Web.Lists.GetByTitle(\"Products\");
              mySPContext.Load(mySPContext.Web);
              mySPContext.Load(productList);
              mySPContext.ExecuteQuery();
 
              ClientOM.ListItemCreationInformation newProductRecord = new ClientOM.ListItemCreationInformation();
              ClientOM.ListItem newProductListItem = productList.AddItem(newProductRecord);
 
              newProductListItem[\"Title\"] = txtProductName.Text;
              newProductListItem[\"Product_SKU\"] = txtProductSKU.Text;
              newProductListItem[\"Price\"] = txtProductPrice.Text;
              newProductListItem.Update();
 
              mySPContext.ExecuteQuery();
 
          }

Now will write code to read the list,

private void btnLoad_Click(object sender, RoutedEventArgs e)
          {
              string spURL = txtSharePointURL.Text;
              IEnumerable<ClientOM.ListItem> myListItems;
              List<ProductInfo> myProducts = new List<ProductInfo>();
 
              ClientOM.ClientContext spContext = new ClientOM.ClientContext(spURL);
              ClientOM.Web mySPSite = spContext.Web;
 
              ClientOM.ListCollection myListCollection = mySPSite.Lists;
              //var producList = myListCollection.GetByTitle(\"Products\");
              var producList = spContext.Web.Lists.GetByTitle(\"Products\");
              ClientOM.CamlQuery mycamlQuery = new ClientOM.CamlQuery();
              IQueryable<ClientOM.ListItem> myList = producList.GetItems(mycamlQuery);
              myListItems = spContext.LoadQuery(myList);
              spContext.ExecuteQuery();
 
              var returnListData = from prod in myListItems select prod;
              foreach (ClientOM.ListItem tempListItem in returnListData)
              {
                  ProductInfo tempProdinfo = new ProductInfo();
                  tempProdinfo.productName = tempListItem.FieldValues.Values.ElementAt(1).ToString();
                  tempProdinfo.productSKU = tempListItem.FieldValues.Values.ElementAt(4).ToString();
                  tempProdinfo.productPrice = tempListItem.FieldValues.Values.ElementAt(5).ToString();
 
                  myProducts.Add(tempProdinfo);
 
              }
              gvSharePointData.DataSource = myProducts;
 
          }

After completion, debug the application. go and check SharePoint site –> Products list for added product information through application.

Leave a Reply

Scroll to Top
%d bloggers like this: