Demonstration Scenario of Logical data structure - Share Point 2010

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

Logical data structure for extranet application

SharePoint implementation of logical data structure

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

  1. Open SharePoint Designer 2010. Click New Blank Web Site.

Create Site Columns

  1. In the dialog box that appears, type the site name as Chapter.
  2. Once your new site has been created, you’ll see a Site Objects menu, as shown next.
  3. Click Site Columns.]

    Create Site Columns

  4. From the ribbon menu at the top of the window, click New Column, and then choose Single Line of Text from the menu.
  5. In the dialog that appears, enter the name ContractId, and then, in the Put This Site Column Into area, select New Group and enter Hire Sample Columns. The completed dialog should look like this:
  6. Create Site Columns

  7. Repeat steps 4 and 5 to create the following columns:
  8. Create Site Columns

  9. Since each of these fields is mandatory, it’s necessary to disallow blank values. To do this, select the column from the Site Columns list, and then select Column Settings from the ribbon. In the dialog that appears, uncheck the Allow Blank Values checkbox. Perform this action on each created field.

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

  1. From the Site Objects menu, select Content Types, and then select New Content Type from the ribbon. In the dialog that appears, enter Hire Contract as the content type name. Then, in the Select a Parent Content Type From drop-down, select Document Content Types. In the Select Parent Content Type drop-down, select Document.
  2. Create a new group to hold your custom content types. Click the New Group checkbox, and then, in text field, type Hire Sample Content Types.
  3. Create Content Types

  4. Repeat steps 1 and 2 to create another content type named On-Hire Asset, also in the Hire Sample Content Types group. This time, set the Parent Content Type drop-downs to List Content Types and Item.
  5. You now have two new, blank content types. The next step is to associate your site columns with these content types. To do this, highlight the Hire Contract content type and select Edit Columns from the ribbon. A list of the columns associated with the content type will appear. You’ll notice that a number of columns are already defined. These columns are inherited from the parent content type that you selected when creating this new content type. To add in your precreated site columns, select Add Existing Site Column from the ribbon, and then use the dialog that appears to find and add the ContractId column, the Contract Start Date column, and the Contract End Date column. Once you’ve added the required columns, click the Save icon in the title bar.
  6. Repeat step 4 for the On-Hire Asset content type. This time add the AssetId column and the AssetTag column.
  7. Repeat steps 3 and 4 to create an Asset Note content type. This time add the Location Code column. You should now have three content types within the Hire Sample Content Types group: Asset Note, Hire Contract, and On-Hire Asset.

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.

  1. The first thing that we need to do is generate a new subsite to act as our template. In SharePoint Designer 2010 Site Objects menu, select Blank Site to change the ribbon to display the Site menu. Select the Subsite menu option from the ribbon. In the dialog that appears, select Blank Site. Type in the location of the new site.Then click OK.
  2. Create Customer Template 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.

  3. Now that we have a new blank site, we need to add lists to hold our data. To do this, select Lists and Libraries from the Site Objects menu. Then, from the ribbon, select Custom List. Type On-Hire Assets as the name of the list.
  4. Repeat step 2 to create another custom list named Asset Notes.
  5. Since hire contracts are documents rather than simple collections of data, we’ll create a document library instead of a list. To do this, select Document Library from the ribbon, and then choose Document Library from the list of options. Type Hire Contracts for the name in the dialog that appears.

Associate Content Types
The next step is to associate our content types with the lists that we’ve created. Do this:

  1. Highlight the Asset Notes list, and then select List Settings from the ribbon. The list configuration page for the Asset Notes list will appear. You’ll notice a section named Settings with a series of checkboxes, as illustrated. Check the Allow Management Of Content Types checkbox. By default, SharePoint creates simple lists and libraries that are bound to basic system content types, such as Item and Document. Since we want to bind the list to a specific content type, we must configure the list to allow such binding. With this option selected, we are free to add and delete content types as appropriate.
  2. Associate Content Types

  3. In the Site Objects menu, select Content Types. Then click the Add button. Select and add the Asset Note content type. Then highlight the Item content type select Delete from the ribbon.
  4. Click the Save icon in the title bar to save the changes.
  5. Repeat steps 1–3 for the On-Hire Assets list. This time, add the On-Hire Asset content type instead of the Asset Note content type.
  6. Repeat steps 1–3 for the Hire Contracts document library. This time add the Hire Contract content type.

Defining Relationships

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:

  • On-Hire Asset is related to Hire Contract via ContractId.
  • Asset Note is related to On-Hire Asset via AssetId.

To create these relationships, take the following steps:

  1. In the Site Objects menu, select Lists and Libraries. Highlight the Asset Notes list. Select Edit Columns from the ribbon.
  2. Select Add New Column from the ribbon and then choose Lookup (information already on this site) from the list of options that appears.
  3. In the dialog that appears (shown next), select On-Hire Assets from the List Or Document Library drop-down. In the Field drop-down, select the AssetId. Uncheck the Allow Blank Values checkbox. Click OK to create the new column.
  4. column editor

  5. Once the column has been created, right-click the column name and select Rename. Change the column name by typing Asset Reference.
  6. Click the Save button in the title bar to save the changes.
  7. Repeat steps 1–5 for the On-Hire Assets list. This time, bind the lookup column to the Hire Contracts document library and set the field to ContractId. Rename the new field Contract Reference.

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:

  1. From the Site Objects menu, select Blank Site. In the ribbon, select Save as Template.
  2. In the browser window that appears, enter Hire Sample Template in the File Name text box, and then in the Template Name text box, enter Hire Sample Template. Then click OK to create the template. The new template will be stored in the User Solutions Gallery, where it can be activated and used to create additional sites as required. The solutions gallery is accessible from the root site on any web application. So, for example, if your server is named MyServer and you have a web application running on, then the solutions gallery can be found at. Using solutions for deployment is covered in more detail later, but for the purposes of demonstrating the use of LINQ, we can simply use our template site as our data source.

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

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

  1. From the Build menu, select Configuration Manager.
  2. configuration manager

  3. Under Platform, select <New…>.
  4. In the New Project Platform dialog, select x64 from the New Platform drop-down list. Click OK to save the changes.

Next, we need to add references to the SharePoint object model:

  1. In the Solution Explorer pane, right-click References and then select Add Reference.
  2. In the dialog that appears, select Microsoft.SharePoint and Microsoft. Share Point .Linq.
  3. Click OK to add the references.

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

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=" /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

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):

%SPROOT%binspmetal.exe /web:http://localhost/ Chapter14/customertemplate /code:"$(ProjectDir)HireSample.cs" /parameters:"$(ProjectDir)SPMetalParameters.xml"

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.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Share Point 2010 Topics