linq to sharepoint labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/lab.docx · web viewlinq to...

16
Hands-On Lab Lab 05: LINQ to SharePoint Lab version: 1.0.0 Last updated: 4/7/2022

Upload: others

Post on 12-Mar-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

Hands-On LabLab 05: LINQ to SharePointLab version: 1.0.0

Last updated: 5/18/2023

Page 2: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

CONTENTS

OVERVIEW................................................................................................................................................. 3

EXERCISE 1: CREATING LIST DATA.......................................................................................................3

EXERCISE 2: CREATING ENTITIES USING THE SPMETAL UTILITY....................................................9

EXERCISE 3: CREATING A WEB PART THAT USES LINQ..................................................................10

Page 3: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

Overview

Lab Time: 45 minutes

Lab Folder: C:\Student\Labs\05_LINQ

Lab Overview: LINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer from having to write CAML queries. In this lab, you will be making use of the new support for LINQ in SharePoint. In the first exercise, you will be creating lists for use with LINQ. In the second exercise you will use the SPMETAL utility to create Entities. In the final exercise you will create a web part for accessing the list data using LINQ.

Note: Lab Setup Requirements

Before you begin this lab, you must run the batch file named SetupLab05.bat. This batch file creates a new SharePoint site collection at the location http://intranet.contoso.com/sites/Lab05.

Exercise 1: Creating List Data

In this exercise you will create a feature to provision lists. Because LINQ code is tied to specific list schemas, your solutions will often contain a list-provisioning component.

1. If you haven’t already done so, run the batch file named SetupLab05.bat, found in the c:\Student\Labs\05_LINQ\ folder, to create the new site collection that will be used to test and debug the code you will be writing in this lab. This batch file creates a new site collection at an URL of http://intranet.contoso.com/sites/Lab05. (Be sure to check the output of the batch file to verify the site URL)

2. Launch Internet Explorer and navigate to the top-level site at http://intranet.contoso.com/sites/Lab05. Take a moment to inspect the site and make sure it behaves as expected. Note that the setup script creates a new site collection with a Team site as its top-level site.

3. Launch Visual Studio 2010 and create a new project by selecting File » New » Project.

a. Expand nodes to SharePoint » 2010 and select Empty Project.

b. Name the new project LINQLists and click the OK button to create the new project.

c. In the SharePoint Customization Wizard, enter http://intranet.contoso.com/sites/Lab05 as the target debugging site.

Page 4: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

d. Select the option to Deploy as farm solution and click the Finish button.

4. When the new project is created, right click the Features node and choose Add Feature.

5. Right-click the new feature and select Add Event Receiver to add a Feature Receiver.

6. Open the Feature1EventReceiver.cs/vb file in Visual Studio for editing.

(Note: in VB.NET you may have to click on the Show All Files button in the Solution Explorer. This file is located underneath the Features/Feature1/Feature1.feature location).

7. Add the following code to the feature receiver class to help with the creation of fields in the lists

C#

using System;using System.Runtime.InteropServices;using System.Security.Permissions;using Microsoft.SharePoint;using Microsoft.SharePoint.Security;

namespace LINQLists.Features.Feature1{

[Guid("a5bcd625-e0b9-4126-b544-89b478334be0")] public class Feature1EventReceiver : SPFeatureReceiver { private void FixupField(SPList spList, string fieldInternalName) { FixupField(spList.Fields.GetFieldByInternalName(fieldInternalName)); }

private void FixupField(SPField spField) { // This method takes an InternalName of a field in a spList // and process a few things we want all fields to have by default // for example setting them to show into the default view spField.ShowInDisplayForm = true; spField.ShowInEditForm = true; spField.ShowInNewForm = true; spField.ShowInListSettings = true; spField.ShowInVersionHistory = true; spField.ShowInViewForms = true;

// Add field to default view SPView defaultView = spField.ParentList.DefaultView; defaultView.ViewFields.Add(spField); defaultView.Update();

Page 5: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

spField.Update(); }

VB.NET

<GuidAttribute("4c02d9ec-6e77-4922-a3bf-594fd4b70bc9")> _Public Class Feature1EventReceiver Inherits SPFeatureReceiver Private Sub FixupField(ByVal spList As SPList, ByVal fieldInternalName As String) FixupField(spList.Fields.GetFieldByInternalName(fieldInternalName)) End Sub

Private Sub FixupField(ByVal spField As SPField) ' This method takes an InternalName of a field in a spList ' and process a few things we want all fields to have by default ' for example setting them to show into the default view spField.ShowInDisplayForm = True spField.ShowInEditForm = True spField.ShowInNewForm = True spField.ShowInListSettings = True spField.ShowInVersionHistory = True spField.ShowInViewForms = True

' Add field to default view Dim defaultView As SPView = spField.ParentList.DefaultView defaultView.ViewFields.Add(spField) defaultView.Update()

spField.Update() End Sub ' Commented out area removed for brevity sake in this display.End Class

8. Locate and uncomment the FeatureActivated method. This is the method that will run when your feature is activated in a SharePoint site.

Add the following code to the FeatureActivated method to build a number of lists.

C#

public override void FeatureActivated(SPFeatureReceiverProperties properties){ using (SPWeb spWeb = (SPWeb)properties.Feature.Parent) { //Projects List Guid pListGuid = spWeb.Lists.Add("Projects", "Company Projects",SPListTemplateType.GenericList); spWeb.Update();

Page 6: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

//Projects List columns SPList pList = spWeb.Lists[pListGuid]; pList.OnQuickLaunch = true; SPField pTitleIDField = pList.Fields["Title"]; FixupField(pList, pList.Fields.Add("Description", SPFieldType.Text, false)); FixupField(pList, pList.Fields.Add("Due Date", SPFieldType.DateTime, false)); SPFieldDateTime dueDateField =

(SPFieldDateTime)pList.Fields["Due Date"]; dueDateField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;

dueDateField.Update(); pList.Update();

// Employees List Guid eListGuid = spWeb.Lists.Add("Employees", "Employees",SPListTemplateType.GenericList); spWeb.Update();

// Employees List columns SPList eList = spWeb.Lists[eListGuid]; eList.OnQuickLaunch = true; SPField titleIDField = eList.Fields["Title"]; titleIDField.Title = "Fullname"; titleIDField.Update();

FixupField(eList, eList.Fields.Add("JobTitle", SPFieldType.Text, false)); FixupField(eList, eList.Fields.Add("Team", SPFieldType.Text, false)); FixupField(eList, eList.Fields.Add("Contribution (in Milestones)", SPFieldType.Number, false)); string projectFieldInternalName = eList.Fields.AddLookup("Project",pListGuid, false); SPFieldLookup projectField = (SPFieldLookup)eList.Fields.GetFieldByInternalName(projectFieldInternalName); projectField.LookupField = pTitleIDField.InternalName; FixupField(projectField); eList.Update();

// Project Manager field (Project to Employee lookup) string employeeFieldInternalName = pList.Fields.AddLookup("Primary Contact", eListGuid, false); SPFieldLookup managerField =

Page 7: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

(SPFieldLookup)pList.Fields.GetFieldByInternalName(employeeFieldInternalName); managerField.LookupField = titleIDField.InternalName; FixupField(managerField); pList.Update(); }}

VB.NET

Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties) Using spWeb As SPWeb = DirectCast(properties.Feature.Parent, SPWeb) 'Projects List Dim pListGuid As Guid = spWeb.Lists.Add("Projects", "Company Projects", SPListTemplateType.GenericList) spWeb.Update() 'Projects List columns Dim pList As SPList = spWeb.Lists(pListGuid) pList.OnQuickLaunch = True Dim pTitleIDField As SPField = pList.Fields("Title") FixupField(pList, pList.Fields.Add("Description",SPFieldType.Text, False)) FixupField(pList, pList.Fields.Add("Due Date",SPFieldType.DateTime, False)) Dim dueDateField As SPFieldDateTime = DirectCast(pList.Fields("Due Date"), SPFieldDateTime) dueDateField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly dueDateField.Update() pList.Update()

' Employees List Dim eListGuid As Guid = spWeb.Lists.Add("Employees", "Employees",SPListTemplateType.GenericList) spWeb.Update()

' Employees List columns Dim eList As SPList = spWeb.Lists(eListGuid) eList.OnQuickLaunch = True Dim titleIDField As SPField = eList.Fields("Title") titleIDField.Title = "Fullname" titleIDField.Update()

FixupField(eList, eList.Fields.Add("JobTitle",SPFieldType.Text, False)) FixupField(eList, eList.Fields.Add("Team", SPFieldType.Text, False)) FixupField(eList, eList.Fields.Add("Contribution (in Milestones)",SPFieldType.Number, False))

Page 8: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

Dim projectFieldInternalName As String =eList.Fields.AddLookup("Project", pListGuid, False) Dim projectField As SPFieldLookup = DirectCast(eList.Fields.GetFieldByInternalName(projectFieldInternalName),SPFieldLookup) projectField.LookupField = pTitleIDField.InternalName FixupField(projectField) eList.Update()

' Project Manager field (Project to Employee lookup) Dim employeeFieldInternalName As String = pList.Fields.AddLookup("Primary Contact", eListGuid, False) Dim managerField As SPFieldLookup = DirectCast(pList.Fields.GetFieldByInternalName(employeeFieldInternalName), SPFieldLookup) managerField.LookupField = titleIDField.InternalName FixupField(managerField) pList.Update() End UsingEnd Sub

9. Locate and uncomment the FeatureDeactivating method. This is the method that will run when your feature is being deactivated in a SharePoint site.

10. Add the following code to the FeatureDectivating method to tear down the lists.

C#

public override void FeatureDeactivating(SPFeatureReceiverProperties properties){ using (SPWeb spWeb = (SPWeb)properties.Feature.Parent) { SPList empList = spWeb.Lists["Employees"]; empList.Delete(); spWeb.Update(); SPList projList = spWeb.Lists["Projects"]; projList.Delete(); spWeb.Update(); }}

Visual Basic

Public Overrides Sub FeatureDeactivating(ByVal properties As _ SPFeatureReceiverProperties) Using spWeb As SPWeb = DirectCast(properties.Feature.Parent, SPWeb) Dim empList As SPList = spWeb.Lists("Employees") empList.Delete()

Page 9: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

spWeb.Update() Dim projList As SPList = spWeb.Lists("Projects") projList.Delete() spWeb.Update() End UsingEnd Sub

11. Build the project and verify that the code is correct; fixing any errors that may be reported by the compiler.

12. Once the project is completed, select Debug » Start Without Debugging from the Visual Studio main menu or use the shortcut key combination of or by pressing [CTRL]+[F5]. Note that when you run the project from Visual Studio, the project is built, packaged, deployed, and features are activated automatically. After the project runs, your browser will open to the test site specified at the beginning of the exercise.

13. When the test site opens, select Site Actions » Site Settings.

14. On the Site Settings page, under the Site Actions section select Manage site features.

15. On the Site Features page, verify that the LINQLists Feature1 is activated.

Figure 1The Site Features page

16. After the feature is activated, you will see two new lists on the Quick Launch bar: Projects and Employees. Add some names to the Employees list and then add some items to the Projects list. The Projects list and Employees list have mutual lookups, so you’ll want to flip back and forth between the lists to add items.

Note: In this exercise you created two lists and added some data to them for future exercises.

Page 10: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

Exercise 2: Creating Entities using the SPMetal Utility

In this exercise you will create entities for use with LINQ. Entity creation is done by using the command line utility SPMetal.

1. Open a command window and navigate to the following path.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin\

2. At the command prompt, execute the following command to build a set of entity classes.

C#

For C# use:SPMetal /web:http://intranet.contoso.com/sites/Lab05 /code:Entities.cs /language:csharp

Visual Basic

For VB.NET use:SPMetal /web:http://intranet.contoso.com/sites/Lab05 /code:Entities.vb /language:vb

3. Locate the file named Entities.cs that was created in the previous step. By default the file will be placed in the same location where SPMetal was executed (in this case, the SharePoint ’14’ bin folder. )

4. Move this file from this location to the lab location: c:\Student\Labs\Lab05_LINQ for use in the next Exercise.

5. Open the file using Notepad or Visual Studio and examine the code inside that was automatically generated by the SPMetal utility. This file will be used in the next exercise to leverage the new SharePoint LINQ capabilities.

Note: In this exercise you used the SPMetal utility to create a class that is used as the underlying source for SharePoint LINQ queries.

Exercise 3: Creating a Web Part that uses LINQ

Page 11: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

In this exercise you will create a web part that queries the lists you created earlier. The web part will create a view of the lists.

1. In the Visual Studio 2010, create a new Visual Web Part project named LINQListsPart.

Figure 2Create a Visual Web Part project. VB.NET shown, C# similar.

2. After creating the project, the SharePoint Customization Wizard will appear. Enter http://intranet.contoso.com/sites/Lab05 as the test URL and click the Finish button.

3. Add the Entities.cs/vb file you created in the previous exercise to the project by right-clicking the project and selecting Add » Existing Item.

4. Open the Entities.cs/vb file and scroll through the classes. You’ll notice that a DataContext has been defined along with classes for all of the list content types in your site. Ignore all the code errors in the file… these are appearing because the project is missing a needed reference.

5. To work with LINQ in SharePoint, you need to add a reference to a new assembly. Do this with the following steps:

a. Select Project » Add Reference… from the Visual Studio main menu.

b. In the Add Reference dialog, add Microsoft.SharePoint.Linq from the .NET tab.(Note: if you have any trouble finding it in the .NET tab you may also use the Browse tab to find this at c:\Program Files\Common Files\Microsoft Shared\web server extensions\14\ISAPI and select Microsoft.SharePoint.Linq.dll. Then click the OK button.)

Page 12: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

6. Add a Literal control named display to the design surface of VisualWebPart1UserControl.ascx.

XAML

<asp:Literal ID=”Display” runat=”server” />

7. Add the following statement to the top of the VisualWebPart1UserControl.ascx.cs/vb file to reference the necessary assemblies:

(Note: in VB.NET you may have to click on the Show All Files button in Solution Explorer.)

C#

using System;using System.Linq;using System.Text;using Microsoft.SharePoint;using Microsoft.SharePoint.Linq;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;

Visual Basic

Imports SystemImports System.LinqImports System.TextImports Microsoft.SharePointImports Microsoft.SharePoint.LinqImports System.Web.UIImports System.Web.UI.WebControlsImports System.Web.UI.WebControls.WebParts

8. Add the following code to the Page_Load method of the Web Part that will query the SharePoint site using LINQ thanks to the Entities.cs/vb code file you generated using SPMetal:

C#

protected void Page_Load(object sender, EventArgs e){ StringBuilder writer = new StringBuilder();

try { using (EntitiesDataContext dc = new EntitiesDataContext("http://intranet.contoso.com/sites/Lab05")) { //Query Expressions var q = from emp in dc.Employees where emp.Project.DueDate < DateTime.Now.AddYears(5) orderby emp.Project.DueDate select new { emp.Title,Contact = emp.Project.PrimaryContact.Title };

Page 13: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

writer.Append("<table border=\"1\" cellpadding=\"3\" cellspacing=\"3\">");

foreach (var employee in q) { writer.Append("<tr><td>"); writer.Append(employee.Title); writer.Append("</td><td>"); writer.Append(employee.Contact); writer.Append("</td></tr>"); } } } catch (Exception x) { writer.Append("<tr><td>"); writer.Append(x.Message); writer.Append("</td></tr>"); } finally { writer.Append("</table>"); display.Text = writer.ToString(); }}

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) _ Handles Me.Load

Dim writer As New StringBuilder() Try Using dc As New EntitiesDataContext( _ "http://intranet.contoso.com/sites/Lab05") 'Query Expressions Dim q = From emp In dc.Employees _ Where emp.Project.DueDate < DateTime.Now.AddYears(5) _ Order By emp.Project.DueDate _ Select New With { _ emp.Title, _ .Contact = emp.Project.PrimaryContact.Title _ }

writer.Append("<table border=""1"" cellpadding=""3"" cellspacing=""3"">")

For Each employee In q writer.Append("<tr><td>") writer.Append(employee.Title)

Page 14: Linq to SharePoint Labaz12722.vo.msecnd.net/.../linqtosharepointlab2-0/Lab.docx · Web viewLINQ to SharePoint is a technology for querying SharePoint lists that relieves the developer

writer.Append("</td><td>") writer.Append(employee.Contact) writer.Append("</td></tr>") Next End Using Catch x As Exception writer.Append("<tr><td>") writer.Append(x.Message) writer.Append("</td></tr>") Finally writer.Append("</table>") Display.Text = writer.ToString() End TryEnd Sub

9. Build the project and verify that the code compiles.

10. Set a breakpoint in the Page_Load method and select Debug » Start Debugging.

11. The Visual Studio debugger should launch a browser and navigate to the http://intranet.contoso.com/sites/Lab05 site.

12. Put the home page in edit mode by selecting Site Actions » Edit Page.

13. Select the Rich Content area (Left web part zone, welcome text at the top). Insert the new Web Part by clicking the Insert tab in the Page Tools tab group on the ribbon and then clicking the Web Part button.

14. Your new Web Part will be in the Custom category. Locate the Web Part VisualWebPart1, select it and then click the Add button.

15. When the Web Part is added to the page, your breakpoint should be hit. Step through the code (F11) and verify that it is working correctly.

Note: In this exercise you created a visual Web Part that used SharePoint LINQ to query a list and display the results.