programming against lists using the server-side object model


Here will see programming against lists using the server-side object model. To do develop applications against SharePoint list we have to Microsoft.SharePoint namespace. To add this DLL reference to your project just go to c:\\Program Files\\Common Files\\Microsoft Shared\\Web ServerExtensions\\14\\ISAPI.

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 server side object model application to add items into the list.

Create a WPF application and create form which will take Product details. Add Micosoft.SharePoint Dll reference and add the namespace to application code file. After form design xaml file will look like the following

<Window x:Class=\"WPF_Lists_ServerSideObjectModel.MainWindow\"
          Title=\"MainWindow\" Height=\"350\" Width=\"525\">
          <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\" />

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 btnLoad_Click(object sender, RoutedEventArgs e)
              string siteURL = \"http://siva-pc:46006\";
              using (SPSite mySiteCollection = new SPSite(siteURL))
                  using (SPWeb web = mySiteCollection.OpenWeb())
                      web.AllowUnsafeUpdates = true;
                      SPList list = web.Lists[\"Products\"];
                      SPListItem listItem = list.Items.Add();
                      listItem[\"Title\"] = txtProductName.Text;
                      listItem[\"Product_SKU\"] = txtProductSKU.Text;
                      listItem[\"Price\"] = txtProductPrice.Text;
                      web.AllowUnsafeUpdates = false;

After completion, debug the application, Fill the product information in prompted UI and press Load. go and check SharePoint site –> Products list for added product information through application.

Leave a Reply

Scroll to Top
%d bloggers like this: