Adding Data Using LINQ Share Point 2010

Our data structure consists of two lists, On-Hire Assets and Asset Notes, and a document library, Hire Contracts. Recall from earlier chapters that the difference between a document library and a list is the type of content contained in each. Document libraries contain document-based content, and as a result, content types that are used in a document library must be derived from the built-in Document content type. When we created our Hire Contract content type, we based it on the Document content type to meet this requirement. By using a Document content type, we’re dictating that all items must have an associated document, and this presents a problem for LINQ. LINQ to SharePoint is a data manipulation tool designed to deal with the actual data that’s associated with an individual item. The entity classes are created based on the fields defined in the associated content types as opposed to the properties of the object that manages the content. For example, most data items in SharePoint are represented by the object model as an SPList Item object. The SPListItem has many properties, such as Title, Url, and UniqueId. None of these properties is mapped to an entity object for use by LINQ to SharePoint; instead, LINQ to SharePoint focuses only on the values of the fields that are associated with the SP ListItem. As a result, it’s impossible to manipulate any properties of an SP ListItem object (or any other object) using LINQ to SharePoint. In the case of a Document-based item, we can’t create an object using LINQ, because items stored in a document library must have an associated file. Since File is a property of the SP ListItem object, we cannot set it via LINQ.

Since we can’t add documents to our document library using LINQ, we can write some SharePoint code that makes use of the object model to insert some sample data quickly.

Add Data Generation Buttons to LinqSampleApplication
In the LinqSampleApplication project, open Form1.cs. From the toolbox, drag three button controls, a textbox, and a label onto the design surface. The form designer should look like this:

Add Data Generation Buttons to LinqSampleApplication
Add Sample Contracts
Double-click the Add Sample Contracts button to add some code for the on-click event, and then add the following code:

As you’ll see from the comments, this code simply creates a few dummy Hire Contract documents in our document library. Once the documents have been uploaded, the properties are set using indexed properties on the SP ListItem object. You’ll notice that the field names and the name of the document are not strongly typed. This is the problem we are addressing by using LINQ.

Add On-Hire Assets
Now that we have documents in our document library, we can move on and create list items that refer to those documents. Double-click the Add On-Hire Assets button on the form designer to add an on-click event handler. Add the following code:

Notice first, and most importantly, that everything is strongly typed, meaning that if the code contained any errors, it would not compile. Second, the code is much more descriptive than the preceding example. It’s apparent which data entities we’re dealing with and what actions we’re performing on them purely from the syntax of the code. Finally, as you’ll remember from our data structure in Figure 14-2, each On-Hire Asset must have an associated Hire Contract. In this code, the assignment of a parent Hire Contract is done by simply assigning an appropriate Hire Contract entity to the Contract Reference property that defines the relationship. There’s no need to know exactly which field is used to define the foreign key since all of that information is automatically determined by the configuration of the lookup field.

Add Asset Notes
We’ve created sample contracts and associated assets; the next thing to add is Asset Notes for each asset. Double-click the Add Asset Notes button to add an on-click event handler. Add the following code:

You should now have a Windows Forms application with three buttons that can be used to generate some sample data. Run the application, clicking each of the three buttons in turn to add some sample data.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Share Point 2010 Topics