饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 78 页

作者:Nick Randolph/等 当前章节:15367 字 更新时间:2026-6-18 14:51

a new Feature will be created. Features are discussed in the “Working with Features” section later

in this chapter.

The Package node contains a single file that serves as the deployment mechanism for a SharePoint

project. A package has a .wsp extension and is logically equivalent to an installer file. The package

contains a set of Features, site definitions, and additional assemblies that are deployed to a

SharePoint site. Packages are discussed in the “Packaging and Deployment” section later in this

chapter.

522 .

chaPter 24 ShArepoinT

To add a SharePoint component to this solution, right-click the project in the Solution Explorer and

select Add . New Item. As you can see in Figure 24-8, Visual Studio ships with templates for a

large number of SharePoint components. Select a new Application Page item, enter MyPage.aspx as

the name, and click OK.

fiGure 24-8

An application page is one of the two types of ASP.NET web pages that are found in SharePoint

sites. Most of the pages that end users interact with in SharePoint are actually content pages. Visual

Studio does not include a template for content pages. Instead, content pages are created and edited

by tools such as the SharePoint Designer or using the SharePoint Foundation object model. Content

pages can be added to a SharePoint page library, and they can also host dynamic Web Parts.

The SharePoint Foundation 2010 object model consists of over 70 namespaces

and provides an API that allows you to perform most administrative and user-

tasks programmatically. The bulk of the classes are contained in the Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll assemblies.

These classes can only be used to work with a local SharePoint Foundation or

SharePoint Server environment.

Although application pages cannot do many of the things that content pages can, they do have much

better support for custom application code. For this reason, application pages are often used for

non-user administration functions.

Creating a sharePoint Project .

523

When the application page is added to the project it is not added to the root of the project. Instead,

it is placed into a subfolder with the same name as your project, under a new folder called Layouts.

The Layouts folder cannot be changed, but you can rename the subfolder at any time.

The Layouts folder is an example of a SharePoint Mapped

Folder. A SharePoint Mapped Folder is essentially a shortcut

to a standard SharePoint folder, and saves you from having to

specify the full path to the folder in your SharePoint solution.

You can add additional Mapped Folders to your project by

right-clicking the project and selecting Add . SharePoint

Mapped Folder. The dialog box with all of the available

SharePoint folders will be displayed, as shown in Figure 24-9.

Application pages are rendered using a SharePoint master

page at run time and as such contain several ASP.NET

Content controls as placeholders for different regions on the

master page. You can add static content, standard HTML

controls, and ASP.NET web controls onto an application

page in addition to editing the code behind the page.

As with any other project type, press F5 to build and run

the project in Debug mode. Visual Studio will automatically

package and deploy the application page to the local

SharePoint installation and then open the browser at

the SharePoint site home page. You must manually navigate to the application page at http://

ServerName/_layouts/ProjectName/MyPage.aspx to view it (see Figure 24-10). You can debug

the application page in the same way you would debug any other ASP.NET web form.

fiGure 24-9

fiGure 24-10

524 .

chaPter 24 ShArepoinT

buildinG custoM sharePoint coMPonents

This section walks you through the development activities associated with some of the more

common SharePoint components.

developing web Parts

Two types of Web Parts can be created in Visual Studio 2010: ASP.NET Web Parts (also known as

Visual Web Parts) and SharePoint-based Web Parts.

ASP.NET Web Parts, which are new to SharePoint 2010, inherit from System.Web.UI.WebControls

.WebParts.WebPart and can be used outside of SharePoint in any ASP.NET web application that

implements the ASP.NET Web Parts functionality. However, ASP.NET Web Parts cannot be used in a

sandboxed solution. Visual Studio 2010 includes a designer for ASP.NET Web Parts.

SharePoint-based Web Parts are a legacy control and inherit from the Microsoft.SharePoint

.WebPartPages.WebPart class. SharePoint-based Web Parts can only be used in SharePoint sites.

There is no designer support for SharePoint-based Web Parts in Visual Studio 2010. Instead,

you must build up the design in code by overriding the CreateChildControls() or Render()

methods.

ASP.NET Web Parts are recommended for new Web Part development. To create a new ASP.NET

Web Part right-click the project in the Solution Explorer and select Add . New Item. Select the

Visual Web Part template, enter MyWebPart as the name, and click Add.

Several files are added to the project when a new Web Part is created. MyWebPart.cs (or MyWebPart.vb

if you are using VB) is the entry point for the Web Part and the class that is instantiated when the Web

Part is loaded at run time. Elements.xml and MyWebPart.webpart are XML - based manifest files that

provide metadata to SharePoint about the Web Part. Finally, MyWebPartUserControl.ascx is the .NET

user control that provides the UI for the Web Part. This is where you should customize the layout and

add web control and code - behind as required.

Once you have designed your Web Part and added the necessary logic, build and run the project.

Visual Studio will automatically package and deploy the Web Part to the local SharePoint site.

You can add the Web Part to an existing page in SharePoint by selecting Site Actions . Edit

Page. Click the tab labeled Insert on the Ribbon and then click Web Part to view the list of

available Web Parts. Your Web Part will be listed under the Custom category by default, as

shown in Figure 24-11.

You can change the category that your Web Part appears under by editing the

Elements.xml file.

Building Custom sharePoint Components .

525

fiGure 24-11

creating content types and lists

Content types and lists are two of the fundamental building blocks of SharePoint and are used to

implement many of the features that are provided out-of-the-box.

Create a new custom content type by right-clicking the project in the Solution Explorer and

selecting Add . New Item. Select the Content Type template, enter MyContentType as the name,

and click Add. In the SharePoint Customization Wizard choose Task as the base content type to

inherit from and click Finish. Visual Studio will create the custom content type, which is simply an

XML-based definition of the content type in the Elements.xml file.

Next, create a custom field that can be used by the new content type. From the Add New Item

dialog, select a new Empty Element, enter Owner as the name, and click Add. Add the following line

of XML to the Elements.xml file that was created, within the <Elements> node:

<Field ID=”{3BA8B2E2-4BEA-4305-ACD2-9511C5E45738}”

Type="User"

Name="Owner"

DisplayName="Task Owner">

</Field>

526 .

chaPter 24 ShArepoinT

Each custom field that you create must have a unique ID. You can generate a

new GUID within Visual Studio by selecting Tools . Create GUID.

Now go back to the Elements.xml file for MyContentType. Add the following line of XML under

the < FieldRefs > node so that the Owner custom field is available to the new content type:

< FieldRef ID=”{3BA8B2E2-4BEA-4305-ACD2-9511C5E45738}” Name="Owner"/ >

Next, create a new SharePoint list defi nition for this content type. From the Add New Item dialog,

select a new List Defi nition From Content Type, specify MyCustomTasksList as the name, and click

Add. Visual Studio will display the SharePoint Customization Wizard, as shown in Figure 24 - 12.

Enter a display name, and then ensure that the custom content type that you created earlier is selected

in the drop - down. Also confi rm that the checkbox to add a list instance is checked.

Finally, you’ll need to customize the list instance so that a useful title is displayed. By default, the title

of list instance is ProjectName -ListInstanceName. Open the Elements.xml file under the list

instance, ListInstance1, and edit the Title attribute in the XML. Save the file and press F5 to build

and run the project.

When the SharePoint site opens, you will see a new list in the left-hand column of the Home page.

Click the list and then click the Items tab in the Ribbon. Click the New Item button to display the

New Item dialog shown in Figure 24-13. Note the new custom field is shown at the bottom of

the dialog.

fiGure 24-12 fiGure 24-13

Building Custom sharePoint Components .

527

You can customize many aspects of the list, including which fields should be

displayed in the default view, by modifying the list definition Schema.xml file.

adding event receivers

Event receivers can be added to many different SharePoint types, including lists, items in a list,

workflows, Features, and SharePoint site administrative tasks. This walkthrough adds a new event

receiver to the custom list that was created in the previous section.

Begin by selecting a new Event Receiver from

the Add New Item dialog. When you click

Add, the SharePoint Customization Wizard is

displayed, as shown in Figure 24-14. Select List

Item Events Task as the type of event receiver

and the custom task list as the event source.

Tick the checkbox next to the An item was

added event and click Finish.

Visual Studio will create the new event receiver

as a class that inherits from the Microsoft

.SharePoint.SPItemEventReceiver base

class. The ItemAdded method will be overridden.

Modify this by adding the following

code that sets the Due Date of a new task to

5 days from the Start Date:

c#

public override void ItemAdded(SPItemEventProperties properties)

{

var startDate = DateTime.Parse(properties.ListItem["Start Date"].ToString());

properties.ListItem["Due Date"] = startDate.AddDays(5);

properties.ListItem.Update();

base.ItemAdded(properties);

}

Code snippet MyEventReceiver.cs

Vb

Public Overrides Sub ItemAdded(ByVal properties As SPItemEventProperties)

Dim startDate = DateTime.Parse(properties.ListItem("Start Date").ToString())

properties.ListItem("Due Date") = startDate.AddDays(5)

properties.ListItem.Update()

MyBase.ItemAdded(properties)

End Sub

Code snippet MyEventReceiver.vb

fiGure 24-14

528 .

chaPter 24 ShArepoinT

You may be prompted with a deployment conflict,

shown in Figure 24-15, when you try to build and

run the project. Check the option so that you are

not prompted more than once and click Resolve

Automatically.

Now when you add a new task to the custom tasks

list the Due Date will be automatically set when

the item is saved.

creating sharePoint workflows

Visual Studio 2010 includes support for two types

of SharePoint workflows: a sequential workflow

and a state machine workflow.

A sequential workflow represents the workflow as a set of steps that are executed in order. For

example, a document is submitted that generates an e-mail to an approver. The approver opens the

document in SharePoint and either approves or

rejects it. If approved, the document is published.

If rejected, an e-mail is sent back to the submitter

with the details of why it was rejected.

A state machine workflow represents the workflow

as a set of states, transitions, and actions. You

define the start state for the workflow and it will

transition to a new state based on an event. For

example, you may have states such as Document

Created and Document Published, and events

that control the transition to these states such as

Document Submitted and Document Approved.

To create a new SharePoint workflow right-click the

project in the Solution Explorer and select Add .

New Item. Select the Sequential Workflow template,

enter MyWorkflow as the name, and click Add.

Visual Studio will launch the SharePoint

Customization Wizard. On the first screen enter a

meaningful name for the workflow and ensure the

type of workflow template to create is set to List

Workflow, as shown in Figure 24-16.

On the next screen, specify the automatic

workflow association that should be created when

a debug session is started. The default options,

shown in Figure 24-17, will associate the workflow

with the Shared Documents document library.

Leave the defaults and click Next.

fiGure 24-15

fiGure 24-16

fiGure 24-17

Working with features .

529

The final step in the SharePoint Customization

Wizard is to specify how the workflow is started.

Leave the defaults (manually started as well as

when an item is created) and click Finish. Visual

Studio will create the workflow and open it in the

Workflow Designer, as shown in Figure 24-18.

Because workflows in SharePoint are built on the

Windows Workflow engine, we won’t spend time in

this chapter exploring how you can customize the

workflow. Instead, refer to Chapter 32 for a detailed

look at Windows Workflow. One thing to note

though: SharePoint 2010 workflows only run on

version 3.5 of Windows Workflow.

You can test your workflow by running it against

the local SharePoint installation. When you run the

solution, Visual Studio will automatically package

and deploy the workflow with the associations

that were specified earlier. When you add a new document to the Shared Documents library the

workflow will be invoked. You can debug the workflow by setting breakpoints in the code-behind

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页