4-10: Adding a New List Item Using the Server Object Model in .NET

Maker QR Code ISO/IEC18004 in .NET 4-10: Adding a New List Item Using the Server Object Model
Listing 4-10: Adding a New List Item Using the Server Object Model
Generating QR Code 2d Barcode In .NET
Using Barcode generation for .NET framework Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
         
Read QR-Code In .NET Framework
Using Barcode decoder for .NET framework Control to read, scan read, scan image in VS .NET applications.
 
Print Barcode In .NET
Using Barcode maker for VS .NET Control to generate, create bar code image in VS .NET applications.
Creating Lists
Bar Code Scanner In Visual Studio .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
      
Make Quick Response Code In Visual C#.NET
Using Barcode creator for .NET Control to generate, create QR image in VS .NET applications.
What do we need to do if we want to update this newly created item Of course first we need to find it again in the collection There are two ways to accomplish this The first one is iterating through an item collection and comparing the value of each item to some condition to find the item we are interested in Then we can access the values of each column for the item by using the indexer on the list item Listing 4-11 shows how to update the Morale Fund item in the BudgetList
QR-Code Creator In .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
Listing 4-11: Inefficient Code That Updates a List Item Using the Items Collection
QR Code 2d Barcode Encoder In VB.NET
Using Barcode generator for VS .NET Control to generate, create QR Code image in VS .NET applications.
       
Bar Code Creator In Visual Studio .NET
Using Barcode generator for .NET framework Control to generate, create bar code image in .NET applications.
       
Code-39 Printer In .NET
Using Barcode printer for VS .NET Control to generate, create Code 39 image in VS .NET applications.
 
Barcode Creator In VS .NET
Using Barcode maker for .NET Control to generate, create bar code image in .NET applications.
  
USS Code 128 Creation In Visual Studio .NET
Using Barcode creation for .NET Control to generate, create Code 128 Code Set A image in VS .NET applications.
Code You Don t Want to Copy
USPS POSTal Numeric Encoding Technique Barcode Encoder In .NET Framework
Using Barcode printer for .NET Control to generate, create Delivery Point Barcode (DPBC) image in VS .NET applications.
Listing 4-11 has some pretty inefficient code it illustrates some concepts for iterating a collection, but it would be a very bad idea to copy this code and use it in a live web site as it iterates over every item in a list, which is expensive We will now see a better way to do this using GetItems( ) and LINQ to SharePoint later in the chapter
Creating Code39 In Visual C#.NET
Using Barcode printer for .NET Control to generate, create Code 39 Extended image in Visual Studio .NET applications.
4: SharePoint Lists
Draw Barcode In C#
Using Barcode maker for Visual Studio .NET Control to generate, create barcode image in VS .NET applications.
The second option is to use the GetItems() method on an SPList object This method uses an SPQuery object to query the list for items that meet a certain condition You have to build a string containing a CAML query to retrieve the items meeting the condition The advantage over the first approach is that only items meeting a certain condition are returned, which brings performance improvements by limiting the data returned Building query strings using CAML is an advanced topic and we won t go too deep into the syntax The SharePoint SDK is a great place to learn more about this topic Listing 4-12 shows code that accomplishes the same item update in Listing 4-11 but much more efficiently First we create an SPList object representing the BudgetList Then we create a new SPQuery object and assign a query string to its query property The query string contains the condition to return all items for which Usage value is equal to Morale Fund The items returned by the query are assigned to the listItems collection of type SPListItemCollection For simplicity we assume there is only one item meeting this criteria, so we can directly access it by accessing the first item in the items collection After that we can update the Title value again and finally call the Update() method on it to commit the change
Printing Bar Code In Java
Using Barcode creation for Java Control to generate, create bar code image in Java applications.
Listing 4-12: Updating a List Item Accessed through SPQuery
Bar Code Generation In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create barcode image in ASP.NET applications.
      
Data Matrix ECC200 Creator In Visual Basic .NET
Using Barcode creator for .NET framework Control to generate, create ECC200 image in .NET framework applications.
                 
Encoding Barcode In VB.NET
Using Barcode encoder for .NET framework Control to generate, create bar code image in .NET framework applications.
    
Code 128 Code Set C Generator In .NET Framework
Using Barcode generation for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
The last item operation using the server object model is deleting an item Once you have returned the item you want to delete by one of the methods above you simply can call SPListItem s Delete method
Code 39 Drawer In Java
Using Barcode creation for Java Control to generate, create ANSI/AIM Code 39 image in Java applications.
Creating Lists Working with Items in a List Using the Client-side Object Model
Let s look at how to create, read, update, and delete list items by using the client object model Again we will focus on the managed NET Client object model You can find further information on the ECMA client object model at http://msdnmicrosoftcom/sharepoint We will discuss the Silverlight client object model in 9, SharePoint Web Parts, when we discuss Silverlight web parts We will reuse our sample BudgetList from the server-side object model samples The first task we will look at is how to create list items Keep in mind that the client application needs to reference the assemblies Microsoft SharePointClient and MicrosoftSharePointClientRuntime Listing 4-13 shows the code that creates a new budget item First we create a new ClientContext object for the site collection our list is in Next we get the List object for the BudgetList by using the GetByTitle method on the Lists collection object Before we can add the new item we need to create a new ListCreationInformation object and set its properties This object will be passed in as a parameter for the AddItem method on the List object Finally we call the Update method on the ListItem object and ExecuteQuery on the ClientContext object Without calling ExecuteQuery nothing would be posted to the server and we couldn t create the item