Hopefully, you now have a clear understanding of what LINQ is and how it works. We need to use entity objects that represent the data in our application if we want to use LINQ syntax. To illustrate the use of LINQ to SharePoint and the tools for generating entity classes, consider the following scenario: Your company is involved in renting industrial machinery. You’re working on a webbased application that allows customers to view a list of the items that the company currently rents, together with details of the individual rental contracts. Customers come from a number of geographical locations and sometimes relocate equipment from one location to another. To make it easier for customers to track the location of the rented equipment, your application allows the customer to add multiple notes for each item, and these notes should also be visible in your web application.
NOTEYou might appreciate a bit of clarification regarding some of the terminology used here. You non-British readers rent equipment, and equipment is said to be rented from the company. In the United Kingdom, we hire equipment, and equipment is said to be on hire when it is rented. So an “on-hire asset” refers to an asset that can be rented and is subject to a rental contract (or hire contract). Since your company has a relatively small number of large customers, you’ve opted for an extranet solution. Each customer has a private site within your SharePoint farm that they can use to assist with managing their account with your company. Since Hire Contracts are standard documents used by all customers, you’ve defined a Hire Contract content type in your farm. Each customer site has a Contracts document library that contains Hire Contract documents. Additionally, you’ve defined an On-Hire Asset content type that represents the individual item of machinery that is subject to the rental contract. Each asset has a unique asset tag, and you allow customers to add notes against each asset tag in a separate list called Asset Notes. If implemented using a relational database, your data structure would look similar to Figure.
A common misconception is to equate a list or document library in SharePoint with a logical entity in your data structure. However, this isn’t necessarily the best approach and often making such an assumption makes you miss out on a lot of the real power of the SharePoint platform. A more useful parallel is to equate a logical entity with a content type. Since lists and document libraries can contain items of more than one content type, and sites can contain more than one list with the same content type, using lists and documents libraries to store items is mainly useful for setting security on a subset of items or for grouping items based on their status or some other business-specific reason. Bearing this in mind, when we translate our data structure into a SharePoint model, we end up with something similar to Figure. Logically, we still end up with the same collections of entities, but our use of content types allows us to create an unlimited number of web sites, each with the same structure andpermissions to view only the customer-specific subset of data.
Logical data structure for extranet application
SharePoint implementation of logical data structure
If you compare this model to a standard ASP.NET application using a relational database to implement our original logical data structure, you can see that implementing security and filtering for each customer would incur significant overhead.
Create a Data Structure Using SharePoint 2010
To create the demonstration scenario data structure, take the following steps:
Create Site Columns
TIPTo make it easier to find specific columns, the column headers in the site columns list can be used to filter the view. For example, clicking the down arrow on the far right of the Group header allows you to select Hire Sample Columns as a filter. Only items that match the filter are shown in the list.
Create Content Types
Create Customer Template Site
Now that we’ve defined the content types that make up our data structure, our next step is to create a template site that can be deployed for each individual customer. You might have noticed a Customer content type was not included to represent our customer. By using multiple sites, one for each customer, the site itself effectively becomes the Customer data container. This may seem a bit bizarre when thinking in relational database design terms, but when you give it a bit more thought it makes sense: In our relational data structure, the Customer table existed only as a means of identifying which customer corresponded to which contract. In effect, it provided a means of segmenting the contract data (and therefore any data that was related to contracts). By using content types within multiple sites, we achieve similar segmentation. Each site contains only data that should be visible to a particular customer. To see data for a different customer, we look at a different site.
After the subsite has been created, a new instance of SharePoint Designer 2010 will open automatically. You’ll see from the title bar that it’s pointing to the new subsite. If you select Content Types or Site Columns from the Site Objects menu, you’ll notice that the content types and columns that were created in the preceding steps are available to this new site. By default, content types are inherited by all subsites within a site collection; as a result, our content types will be available to any sites that are created as subsites.
Associate Content Types
The next step is to associate our content types with the lists that we’ve created. Do this:
We now have a template site with each of our three content types bound to a list or library. We could start entering data into these lists. However, one thing that we haven’t considered is the relationships between the various content types. In SharePoint, relationships are defined by using lookup fields. Each lookup field is bound to a particular column in a particular list (although other columns can also be carried through if required). In relational database terms, the lookup field works like a foreign key, holding a value that uniquely identifies a record on the source list.
NOTEIt is possible to create lookups that can accept multiple values, but for the purposes of defining this data structure, we’re interested only in single value lookups. Lookup columns target a specific list. While it is possible to include lookup fields in content types, each time the content type is used the lookup field is bound to the list that was set when the content type was created, potentially causing security issues if the list is in a separate site. For that reason, when using lookups to define data structures, you should view them as a constraint to be added on the list, as opposed to a field to contain data within the content type. Adding lookups in a content type greatly reduces the portability of the content type. Our data structure requires two relationships:
To create these relationships, take the following steps:
Create a Site Template
We’ve now created a template web site for use by our customers, complete with a data structure that’s logically identical to the relational schema that we defined in Figure. The final step that we need to take is to save this site as a template so that we can easily create a new site for each customer as required. To do this, do the following:
Creating Entities Using SPMetal
Now that we’ve created a basic data structure, let’s make use of SPMetal to generate some entity classes. At the time of writing, SPMetal is implemented as a command line only tool. It’s installed by default at % SPROOT % Bin SPMetal.exe and can be called using the command line arguments in Table. The tool will produce a .cs or .vb file containing class definitions for each entity available within the site referenced by the /web: argument. Additionally, it will also create a strongly typed Data Context object with properties for each list and document library within the data structure.
Create a Windows Forms Sample Application
First things first: if we’re going to make use of LINQ, we’ll need a Visual Studio 2010 project. Create a new Windows Forms Application project. It’s important to make sure that the project is set to use .NET Framework 3.5 and not version 4.0. For ease of narrative, save the project in C:CodeChapter14 and call it LinqSampleApplication.
SPMetal Command Line Arguments
Once the project has been created, we need to take a few steps to make it compatible with SharePoint 2010. First, we need to set the build type to be 64-bit. SharePoint 2010 is 64-bit only, and by default Windows Forms Application projects are config
Next, we need to add references to the SharePoint object model:
Generate Entity Classes
We’re now ready to use SPMetal to generate entity classes for our extranet sample. Open a command prompt, change the current directory to C: Code Chapter Linq Sample Application, and then execute the following command (note that this command should be entered on a single line, not on two lines as shown here): % SP ROOT % bin SPMetal. exe
TIP Define a system variable to point to SharePoint root; it saves a whole load of time instead of typing C:Program Files Common Files Microsoft SharedWeb Server Extensions . Instead you can simply type %SPROOT% as a shortcut. See Chapter for a step-by-step guide. SPMetal will create a new file named HireSample.cs in the project directory for our Linq Sample Application. To add it into the project, select Add Existing Item from the Project menu, and then select the file named Hire Sample.cs. Have a look at the code in HireSample.cs. You’ll notice that each class is declared using the partial keyword. This allows you to extend the functionality of the auto-generated classes using additional files so that if the classes are automatically regenerated using SPMetal, any customizations are not lost. Figure shows the objects that are defined in HireSample.cs. You’ll notice that a Hire Sample Data Context class that’s derived from Data Context has been defined and has properties corresponding to each of the three lists: AssetNotes, HireContracts, and OnHireAssets. Additionally, there are two classes derived from AssetNote and OnHireAsset, named Asset Notes Asset Note and OnHire Assets On Hire Asset. These derived classes contain the additional lookup values that we added to define the relationships between the content types. Since the lookup values were not added to the content type for reasons mentioned earlier, a new derived class exists containing only the lookup field.
Controlling Entity Creation Using a Parameters File
To save any confusion, it would be nice to rename a few of the entities, particularly the derived entities containing the additional columns. While it’s perfectly possible to do this manually in code, the next time SPMetal is used to regenerate the entities, all changes will be lost and any code that refers to the old entity names will fail to compile. Instead, SPMetal allows for the use of a parameters file.
Generated entity class diagram
In Visual Studio, add a new XML file named SPMetalParameters.xml. Insert the following XML into the file:<?xml version="1.0" encoding="utf-8" ?><Web AccessModifier="Internal" xmlns="http://schemas.microsoft.com/SharePoint /2009/spmetal"><List Name="Asset Notes" Type="AssetNote"><ContentType Name="Asset Note" Class="AssetNote" /></List><List Name="On-Hire Assets" Type="OnHireAsset"><ContentType Name="On-Hire Asset" Class="OnHireAsset" /></List><ContentType Name="Asset Note" Class="BaseAssetNote"/><ContentType Name="On-Hire Asset" Class="BaseOnHireAset"/></Web>
Save the file, and then rerun SPMetal using the following command line (as before, this command should be entered on one line):
% SPROOT % bin spmetal.exe /web:http:/ /localhost/ Chapter/ customer template /code:Hire Sample.cs /parameters: SPMetalParameters.xml
Revised entity class diagram
If you now examine HireSample.cs, you’ll notice that the objects have been renamed as specified in the parameters file. Figure 14-5 shows the new object model.
Incorporate a Build Script to Regenerate Entities Automatically
Rather than having to rerun SP Metal manually each time your data structure changes, you can incorporate a build script into your Visual Studio project that calls SP Metal automatically each time the project is built.
In Visual Studio, in the Project menu, select Linq Sample Application Properties. In the Properties pane, select Build Events, and then click the Edit Pre-Build button. In the dialog that appears, enter the following script (in one continuous line; line breaks are added here for readability):
Build the project. You’ll notice that when the build has completed, you’re notified that Hire Sample.cs has been modified. This confirms that the prebuild script has automatically regenerated the file as part of the build process.
Share Point 2010 Related Interview Questions
|Web Services Interview Questions||XML Interview Questions|
|Share Point 2010 Interview Questions||ASP.NET Interview Questions|
|Share Point Administration Interview Questions||BizTalk Admin Interview Questions|
|Microsoft Office SharePoint Server (MOSS) Interview Questions||Biztalk Server Interview Questions|
|Asp Dot Net Mvc 4 Interview Questions||Biztalk Esb Toolkit Interview Questions|
|InfoPath Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.