(v) Data updates using LINQ
The advantage of LINQ to SharePoint is that SharePoint list data queries can be made quickly and easily, and the data entity class generated by Spmetal is actually a model that supports bidirectional synchronization. That means that through DataContext we can also submit changes in data to the SharePoint list.
Data submission via DataContext is divided into three phases: (1) Ensure that the DataContext supports data submission, where the objecttrackingenabled attribute is true (default), and (2) modifies the contents of the data entity ; (3) Call DataContext's SubmitChanges method to submit the data.
1, the new data
When creating a new entry in the list, we can create an entity object and add it to the corresponding list (entitylist), as shown in the following example:
1:using
2:new bookdatacontext ("Http://sp2010/book"))
3: {
4:
5: New
6:
7: "Ch02"
8: "Visual Studio"
9: "Kaneboy"
Ten: };
One : ctx. Chapters.insertonsubmit (Newchpater);
: ctx. SubmitChanges ();
: }
When submitting new data to a list, you need to add the corresponding data entity using the Entitylist InsertOnSubmit method. Similarly, you can also use the Insertallonsubmit method to insert multiple data entities at once, such as adding a query result in one list to another list that uses the same content type at a time.
2. Modification of data
In DataContext, changes to SharePoint list data can be modified directly on the data entities, and then called SubmitChanges for data updates, for example:
1:using
2: New Bookdatacontext ("Http://sp2010/book"))
3: {
4: foreach in CTX. Chapters)
5: "Kaneboy";
6: ctx. SubmitChanges ();
7: }
3, the deletion of data
Similar to data additions, deletion of data uses the Entitylist DeleteOnSubmit method to delete an entity (that is, a list entry), using the Deleteallonsubmit method to delete multiple entities, for example:
1:using
2: New Bookdatacontext ("Http://sp2010/book"))
3: {
4: in CTX. Chapters
5: where CHP. Name.contains ("[obsolete]")
6: Select CHP;
7: ctx. Chapters.deleteallonsubmit (uselesschapters);
8: ctx. SubmitChanges ();
9: }
Like the Delete method for list entries (that is, SPListItem), Entitylist DeleteOnSubmit and Deleteallonsubmit are completely removed from the list entries. If you only want to delete content to the Recycle Bin, you should use the Recycleonsubmit and Recycleallonsubmit methods.
hint : Similar to DataSet, because DataContext is the data cache first, modified and then unified commit, so in the real environment of multi-person co-editing, there will inevitably be data submission conflict problems. As to the resolution of the conflict, due to the limitation of space is not listed here, interested readers can refer to the following Web site:
http://msdn.microsoft.com/en-us/library/ee538246 (office.14). aspx
SharePoint Server-side object model using LINQ for data Access Operations (Part 3)