SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web...

16
Hands-On Lab Lab: SharePoint 2010 Best Practices Lab version: 1.0.0 Last updated: 5/5/2022

Transcript of SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web...

Page 1: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

Hands-On LabLab: SharePoint 2010 Best PracticesLab version: 1.0.0

Last updated: 5/24/2023

Page 2: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

CONTENTS

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

EXERCISE 1: CREATING A FEATURE WITH COMMON RESOURCES..................................................4

EXERCISE 2: CREATING A WEB PART WITH CUSTOM CAS POLICIES..............................................7

EXERCISE 3: CREATING A DEPENDENT WEB PART..........................................................................12

Page 3: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

Overview

Lab Time: 30 minutes

Lab Folder: C:\Student\Labs\xx_BestPractices

Lab Overview: In this lab you will learn how to create a feature that depends on another feature and how you can create features using Code Access Security.

Lab Setup Requirements

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

Page 4: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

Exercise 1: Creating a Feature with Common Resources

In this exercise, you will create a feature that contains images upon which other features depend.

1. If you haven’t already done so, run the batch file named SetupLabxx.bat, found in the c:\Student\Labs\xx_BestPractices\ 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/Labxx.

2. Start Visual Studio 2010.

3. Select File » New » Project from the main menu.

4. In the New Project dialog, select Visual C#/Visual Basic » SharePoint »2010 in the Installed Templates list.

5. Select Empty SharePoint Project as the Project type.

6. Ensure that the target framework is set to .NET Framework 3.5.

7. Name the new project BestPracticesImages and click the OK button.

8. When the SharePoint Customization Wizard, enter the address of the site you are using for this exercise: http://intranet.contoso.com/sites/Labxx/.

9. Select to deploy the solution as a Farm Solution.

10. Click the Finish button.

Page 5: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

11. In the Solution Explorer, right click the Features node and select Add Feature from the context menu.

12. In the Solution Explorer, right click the BestPracticesImages project node and select Add » SharePoint “Images” Mapped Folder from the context menu.

13. In the Solution Explorer, inside of the Images folder, right click the BestPracticesImages folder and select Add » Existing Item from the context menu.

14. Locate the file C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\Template\Images\delete.gif and add it to the folder.

15. In the Solution Explorer, expand the Package node and double-click the Package.package node.

16. In the Properties window (lower right side), locate the SolutionId property and copy the SolutionId property to Notepad for use later in the lab.(Note: your ID will be different from the one shown in the image below)

Page 6: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

17. DO NOT DEPLOY THE SOLUTION YET!

Page 7: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

Exercise 2: Creating a Web Part with Custom CAS Policies

In this exercise, you will create a web part that utilizes a custom CAS policy.

1. Start a new instance of Visual Studio 2010.

2. Select File » New » Project from the main menu.

3. In the New Project dialog, select Visual C#/Visual Basic » SharePoint »2010 in the Installed Templates list.(Note: be sure to choose the same language here [C# or VB.NET] as before).

4. Select Empty SharePoint Project as the Project type.

5. Ensure that the target framework is set to .NET Framework 3.5.

6. Name the new project BestPracticesParts and click the OK button.

7. When the SharePoint Customization Wizard, enter the address of the site you are using for this exercise: http://intranet.contoso.com/sites/Labxx/.

8. Select to deploy the solution as a Farm Solution.

9. Click the Finish button.

Page 8: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

10. In the Solution Explorer, right click the BestPracticesParts project node and select Add » New Item from the context menu.

11. Select to add a new Web Part.

12. Name the Web Part StringProvider and click the Add button.

13. In the Solution Explorer, right click the BestPracticesParts project node and select Add » New Item » expand Common Items and select Code » Interface from the Add New Item dialog box.

14. Name the interface IStringConnection.cs or IStringConnection.vb (Note: the first character here is a capital i that is used to denote an interface class) and click the Add button.

15. Open IStringConnection.cs/vb for editing in Visual Studio and add the code to create an interface as shown.

C#

public interface IStringConnection{ string ProvidedString { get; }}

C#

Public Interface IStringConnection ReadOnly Property ProvidedString() As StringEnd Interface

16. Open StringProvider.cs/vb for editing in Visual Studio.

17. Update the code so that the web part implements the interface as follows.

C#

[ToolboxItemAttribute(false)]public class StringProvider : WebPart, IStringConnection{ private TextBox theString;

[ConnectionProvider("String Provider")] public IStringConnection ConnectionInterface() { return this; }

public string ProvidedString { get { return theString.Text; } }

protected override void CreateChildControls() { theString = new TextBox();

Page 9: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

if (!Page.IsPostBack) theString.Text = "Web Part connections are good"; this.Controls.Add(theString); }

protected override void RenderContents(HtmlTextWriter writer) { writer.Write("<p>Enter a string</p>"); theString.RenderControl(writer);

}}

Visual Basic

<ToolboxItemAttribute(False)> _Public Class StringProvider Inherits WebPart Implements IStringConnection Private theString As TextBox

<ConnectionProvider("String Provider")> _ Public Function ConnectionInterface() As IStringConnection Return Me End Function

Public ReadOnly Property ProvidedString() As String _ Implements IStringConnection.ProvidedString Get Return theString.Text End Get End Property

Protected Overrides Sub CreateChildControls() theString = New TextBox() If Not Page.IsPostBack Then theString.Text = "Web Part connections are good" End If Me.Controls.Add(theString) End Sub

Protected Overrides Sub RenderContents(ByVal writer _ As HtmlTextWriter) writer.Write("<p>Enter a string</p>") theString.RenderControl(writer) End SubEnd Class

18. In the Solution Explorer, expand the Package node and double click the Package.package node.

Page 10: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

19. In the visual designer, click the Manifest button (located near the bottom of the Package.package window) and expand the Edit Options.

20. Click the Open in XML Editor link.

21. Modify the XML to add custom CAS policies as shown in the following code.

XAML

<?xml version="1.0" encoding="utf-8"?><Solution xmlns="http://schemas.microsoft.com/sharepoint/"> <CodeAccessSecurity> <PolicyItem> <PermissionSet class="NamedPermissionSet" version="1" Description="Connection Permissions"> <IPermission class="AspNetHostingPermission" version="1" Level="Minimal" /> <IPermission class="SecurityPermission" version="1" Flags="Execution" /> </PermissionSet> <Assemblies> <Assembly Name="BestPracticesParts"/> </Assemblies> </PolicyItem> </CodeAccessSecurity></Solution>

22. In the Solution Explorer, select the BestPracticesParts project and in the Properties window, change the Assembly Deployment property to Web Application.

Page 11: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

23. In the Solution Explorer, expand the(if using C#) Properties node / (if using VB.NET) MyProject node, and open the AssemblyInfo.cs/vb file for editing.(Note: you may have to click the Show all files button in the solution explorer to see the items in these nodes)

24. Verify that the following to assembly attribute exists somewhere in this file. This allows the web parts to run outside of the GAC.(Note: both C# and VB.NET should automatically add this.)

C#

[assembly: System.Security.AllowPartiallytrustedCallers()]

Visual Basic

<Assembly: System.Security.AllowPartiallyTrustedCallers()>

25. In the Solution Explorer, right click the project and select Build from the context menu.

26. After you have a successful build, right click the project and select Package.

Page 12: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

Exercise 3: Creating a Dependent Web Part

In this exercise, you will create a web part that depends upon the Solution packaged in Exercise 2.

1. In the Solution Explorer for BestPracticesParts, right click the project node and select Add » New Item from the context menu.

2. Select to add a new Web Part.

3. Name the Web Part StringConsumer and click the Add button.

4. Open StringConsumer.cs/vb for editing in Visual Studio.

5. Update the code so that the web part implements the interface as follows.

C#

public class StringConsumer : WebPart{ BestPracticesParts.IStringConnection providerPart = null;

[ConnectionConsumer("String Consumer")] public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) { this.providerPart = ProviderPart; }

protected override void RenderContents(HtmlTextWriter writer) { try { writer.Write("<p>" + providerPart.ProvidedString + "</p>"); } catch { writer.Write( @"<img src='/_layouts/images/BestPracticesImages/delete.gif'/>" ); } }}

VB.NET

<ToolboxItemAttribute(false)> _

Page 13: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

Public Class StringConsumer Inherits WebPart

Private providerPart As BestPracticesParts.IStringConnection = Nothing

<ConnectionConsumer("String Consumer")> _ Public Sub GetConnectionInterface(ByVal ProviderPart _ As BestPracticesParts.IStringConnection) Me.providerPart = ProviderPart End Sub

Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter) Try writer.Write("<p>" _ & Convert.ToString(providerPart.ProvidedString) & "</p>") Catch writer.Write( _ "<img src='/_layouts/images/BestPracticesImages/delete.gif'/>") End Try End SubEnd Class

In the Solution Explorer, expand the Package node, expand the Package.package node, and double click the Package.template.xml node.(Note: you may have to click the Show all files button in the solution explorer to see the items in these nodes)

6. Edit the XML to apply a solution dependency to the solution contained in Exercise 1. Place the solution dependency code above the CAS policies as shown below. Be sure to use the SolutionId you saved to notepad in Exercise 1.

XAML

…<Solution xmlns="http://schemas.microsoft.com/sharepoint/"><ActivationDependencies> <ActivationDependency SolutionId="Your Solution Id here"/></ActivationDependencies><CodeAccessSecurity> …

Page 14: SharePoint 2010 Best Practices Labaz12722.vo.msecnd.net/.../labs/bestpracticeslab2-0/La… · Web viewSharePoint 2010 Best Practices Lab Description In this lab you will learn how

7. In the Solution Explorer, right click the project and select Deploy from the context menu. Your solution deployment should fail because the BestPracticesImages solution has not been deployed.

8. Open the BestPracticesImages solution in Visual Studio 2010.

9. In the Solution Explorer, right click the project and select Deploy from the context menu.

10. After successfully deploying the BestPracticesImages project, return to the BestPracticesParts project.

11. In the Solution Explorer, right click the project and select Deploy from the context menu. Your solution should now deploy successfully.

12. Open your test site in Internet Explorer.

13. In the test site, add the StringProvider and StringConsumer web parts to the page.

14. Initially, the consumer should be displaying the delete image indicating that it has no connection.

15. Using the web part menu, connect the consumer and provider and verify that the text is passed between them.