使用VS2008进行WebPart开发时,用其自带的部署菜单项进行部署时,总是有各种各样的错误,最终无法生成部署文件和部署批处理文件。所以我一直想搞清楚VS2008是如何生成这些部署文件的。现在终于有了一个结果,可以与大家分享。
要生成部署文件需要以下几个文件。
- Manifest.xml
Defines the list of features, site definitions, resource files, web part files, and assemblies to be included in the Solution package
Manifest.xml文件通常作为Solution的入口点,用来指明在这个Solution中需要去处理的Package—你可以在FeatureManifests节点下指定多个FeatureManifest来执行多个Feature的部署。在英文解释中提到还可以指定别的一些类型的文件,但Assemblies是通常会在这里指定,其他文件最好被声明在各个Feature中。 - Feature.xml
Defines the feature and specifies the location of assemblies, files, dependencies, or properties that support the Feature.
Feature.xml是很常见的配置文件,用来指定所安装的Feature中要包含的DLL,以及其详细配置文件Elements.xml的路径。Feature.xml我们可以认为是单个Feature的入口点,大多数时间之需要指明Elements.xml的路径,而无需将具体操作置入Feature.xml。这样做是为了让我们的配置文件更结构化,功能化。 - MyWebPartManifest.xml
Element manifest file containing definitions to the feature's elements.
Elements.xml文件是最终这个Feature所要做的动作的具体描述。在这里可以应用诸如CustomAction, Module, ModuleGroup, Assemblies, ActivationDependencies, Recievers等扩展标记来告诉Package在部署时要做的动作。 - MyWebpart.webpart
Web Parts control description files contain property values, state data, and assembly or source file details exported from a WebPart control (or other ASP.NET server or user control used in a Web Parts application) to an XML file with a .WebPart extension.
.webpart文件包含了Webpart的属性、数据等信息。 - MyWebpart.ddf
Package.ddf is a MakeCab diamond directive file used to define the structure and contents of the solution package.
.ddf文件指定了将来生成的.CAB文件或.WSP文件包含的内容。这里定义了所有需要部署的文件结构信息。需要注意的是,目录结构的变化需要用.SET DESTINATIONDIR=’’ 来显式指定。
(注:以上部分说明引自博客:初探SharePoint部署 – WSS Solution Package )
以下是各个文件的具体实例:
- Manifest.xml
Code
<?xml version="1.0" encoding="utf-8"?>
<Solution SolutionId="42560968-69f5-4535-a1fc-9d3d2428eb4e" xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureManifests>
<FeatureManifest Location="MeetingSchedule\feature.xml" />
</FeatureManifests>
<Assemblies>
<Assembly Location="MeetingSchedule.dll" DeploymentTarget="GlobalAssemblyCache">
<SafeControls>
<SafeControl Assembly="MeetingSchedule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="MeetingSchedule" TypeName="MeetingSchedule" Safe="True" />
</SafeControls>
</Assembly>
</Assemblies>
</Solution> - Feature.xml
Code
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="91a1e498-bd15-439c-b28f-bde8f690ef55" Title="MeetingSchedule" Scope="Site" Version="1.0.0.0" Hidden="FALSE" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="MeetingSchedule\MeetingSchedule.xml" />
<ElementFile Location="MeetingSchedule\MeetingSchedule.webpart" />
</ElementManifests>
</Feature>
注意:此处的.webpart文件的名字为MeetingSchedule.webpart;ElementManifest文件的名字为MeetingSchedule.xml。 - MyWebPartManifest.xml
Code
<?xml version="1.0" encoding="utf-8"?>
<Elements Id="02342748-2698-4e26-867a-9b4abd2b9da7" xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="WebParts" List="113" Url="_catalogs/wp">
<File Path="MeetingSchedule\MeetingSchedule.webpart" Url="MeetingSchedule.webpart" Type="GhostableInLibrary" />
</Module>
</Elements> - MyWebpart.webpart
Code
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<!--
The following Guid is used as a reference to the web part class,
and it will be automatically replaced with actual type name at deployment time.
-->
<type name="MeetingSchedule.MeetingSchedule, MeetingSchedule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />
<importErrorMessage>Cannot import MeetingSchedule Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">MeetingSchedule Web Part</property>
<property name="Description" type="string">MeetingSchedule Description</property>
</properties>
</data>
</webPart>
</webParts> - MyWebpart.ddf
Code
;
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=MeetingSchedule.wsp
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP;** All files are compressed in cabinet files
.Set UniqueFiles="ON"
.Set Cabinet=on
.Set DiskDirectory1=Package
;adds manifest file
manifest.xml
;adds webpart dll
MeetingSchedule.dll
;sets the title webpart feature directory
.Set DestinationDir=MeetingSchedule
;adds the feature manifest to the feature directory
MeetingSchedule\feature.xml feature.xml
;adds the webpart manifest to the feature directory
MeetingSchedule\MeetingSchedule\MeetingSchedule.webpart MeetingSchedule\MeetingSchedule.webpart
;adds the element manifest to the feature directory
MeetingSchedule\MeetingSchedule\MeetingSchedule.xml MeetingSchedule\MeetingSchedule.xml
这些文件的目录结构为:
\root
manifest.xml
MeetingSchedule.ddf
MeetingSchedule.ddl
\MeetingSchedule
feature.xml
\MeetingSchedule
MeetingSchedule.webpart
MeetingSchedule.xml
注:这其中的feature.xml、manifest.xml文件、.webpart文件和ElementManifest文件,都由VS2008自动生成。但是manifest.xml文件中不含有SafeControls的信息,需要自己添加。查看方法:选择View->Other Windows->WSP View。除了ddf文件外,其他文件在VS2008能够正常部署时也可以自动生成(包括正确的manifest.xml文件)。实际上我的这些文件就是这样得到的。
好了,到此为止我们已准备好了所有文件,现在就可以开始生成部署文件了。生成的部署文件,实际上是由Makecab压缩成的cab文件,只不过其后缀改为了wsp而已。在root下执行:
Makecab /F meetingschedule.ddf
就可以生成一个新的目录Package,在此目录下含有生成的部署文件:MeetingSchedule.wsp。
现在可以进行部署了。通常执行Stsadm就可以进行部署,但是为了每次部署及解除部署的方面,最好使用一个批处理文件自动进行。VS2008在能够正常部署时,会自动生成这样一个名为“Setup.bat”批处理文件。我就把这个文件照搬到此,作为模板,以后可以根据需要改动。
注意:其中的Feature Id,就是Feature.xml中的ID。
@rem======================================================================
@rem
@rem setup.bat
@rem
@rem======================================================================
@echo off
setlocal
pushd .
goto LInitialize
@rem----------------------------------------------------------------------
@rem LInitialize
@rem----------------------------------------------------------------------
:LInitialize
set SPAdminTool=%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN\stsadm.exe
set Install=
set Uninstall=
set PackageFile=%~dp0Package\MeetingSchedule.wsp
set PackageName=MeetingSchedule.wsp
set DefaultWebUrl=http://myserver:40000
set DefaultSiteUrl=http://myserver:40000
set TargetWebUrl=
set TargetSiteUrl=
goto LParseArgs
@rem----------------------------------------------------------------------
@rem LParseArgs
@rem----------------------------------------------------------------------
:LParseArgs
@rem --- help ---
if "%1" == "/?" goto LHelp
if "%1" == "-?" goto LHelp
if "%1" == "/h" goto LHelp
if "%1" == "-h" goto LHelp
if "%1" == "/help" goto LHelp
if "%1" == "-help" goto LHelp
@rem --- Fix execute task ---
if "%1" == "/i" (set Install=1) & shift & goto LParseArgs
if "%1" == "-i" (set Install=1) & shift & goto LParseArgs
if "%1" == "/install" (set Install=1) & shift & goto LParseArgs
if "%1" == "-install" (set Install=1) & shift & goto LParseArgs
if "%1" == "/u" (set Uninstall=1) & shift & goto LParseArgs
if "%1" == "-u" (set Uninstall=1) & shift & goto LParseArgs
if "%1" == "/uninstall" (set Uninstall=1) & shift & goto LParseArgs
if "%1" == "-uninstall" (set Uninstall=1) & shift & goto LParseArgs
@rem --- Fix url ---
if "%1" == "/weburl" (set TargetWebUrl=%2) & shift & shift & goto LParseArgs
if "%1" == "-weburl" (set TargetWebUrl=%2) & shift & shift & goto LParseArgs
if "%1" == "/siteurl" (set TargetSiteUrl=%2) & shift & shift & goto LParseArgs
if "%1" == "-siteurl" (set TargetSiteUrl=%2) & shift & shift & goto LParseArgs
@rem --- Check invalid arguments ---
if not "%1" == "" (
echo Invalid argument.
goto LHelp
)
@rem --- Check arguments ---
if "%Install%" == "1" (
if "%Uninstall%" == "1" (
goto LHelp
)
)
if "%Install%" == "" (
if "%Uninstall%" == "" (
set Install=1
)
)
if "%TargetSiteUrl%" == "" (
if "%TargetWebUrl%" == "" (
set TargetWebUrl=%DefaultWebUrl%
set TargetSiteUrl=%DefaultSiteUrl%
)
if not "%TargetWebUrl%" == "" (
set TargetSiteUrl=%TargetWebUrl%
echo Setting TargetSiteUrl to be %TargetWebUrl%
)
)
if "%TargetWebUrl%" == "" (
set TargetWebUrl=%TargetSiteUrl%
echo Setting TargetWebUrl to be %TargetSiteUrl%
)
goto LMain
@rem----------------------------------------------------------------------
@rem LHelp
@rem----------------------------------------------------------------------
:LHelp
echo Usage:
echo setup.bat [/install or /uninstall][/weburl ^<url^>][/siteurl ^<url^>]
echo [/help]
echo.
echo Options:
echo /install or /uninstall
echo Install specified Solution package (.wsp) to the SharePoint server
echo or uninstall specified Solution from the SharePoint server.
echo Default value: install
echo /weburl
echo Specify a web url of the SharePoint server.
echo Default value: %DefaultWebUrl%
echo /siteurl
echo Specify a site url of the SharePoint server.
echo Default value: %DefaultSiteUrl%
echo /help
echo Show this information.
echo.
goto LTerminate
@rem----------------------------------------------------------------------
@rem LMain
@rem----------------------------------------------------------------------
:LMain
if "%Install%" == "1" (
call :LDeploy
)
if "%Uninstall%" == "1" (
call :LRetract
)
goto LTerminate
@rem----------------------------------------------------------------------
@rem LDeploy
@rem----------------------------------------------------------------------
:LDeploy
echo Adding solution %PackageName% to the SharePoint
"%SPAdminTool%" -o addsolution -filename "%PackageFile%"
echo Deploying solution %PackageName%
"%SPAdminTool%" -o deploysolution -name "%PackageName%" -local -allowGacDeployment -url %TargetWebUrl%
echo Activating feature YBDC.MeetingSchedule
"%SPAdminTool%" -o activatefeature -id 91a1e498-bd15-439c-b28f-bde8f690ef55 -url %TargetSiteUrl%
goto :EOF
@rem----------------------------------------------------------------------
@rem LRetract
@rem----------------------------------------------------------------------
:LRetract
echo Deactivating feature YBDC.MeetingSchedule
"%SPAdminTool%" -o deactivatefeature -id 91a1e498-bd15-439c-b28f-bde8f690ef55 -url %TargetSiteUrl%
echo Uninstalling feature YBDC.MeetingSchedule
"%SPAdminTool%" -o uninstallfeature -id 91a1e498-bd15-439c-b28f-bde8f690ef55 -force
echo Retracting solution %PackageName%
"%SPAdminTool%" -o retractsolution -name "%PackageName%" -local -url %TargetWebUrl%
echo Deleting solution %PackageName% from SharePoint
"%SPAdminTool%" -o deletesolution -name "%PackageName%"
goto :EOF
@rem----------------------------------------------------------------------
@rem LTerminate
@rem----------------------------------------------------------------------
:LTerminate
set UserInput=
set /P UserInput=Hit enter key to quit.
set SPAdminTool=
set PackageFile=
set PackageName=
set Install=
set Uninstall=
set TargetSiteUrl=
set TargetWebUrl=
set UserInput=
popd
endlocal
这样就可以方面的进行部署与解除。
部署命令:Setup /i
解除命令:Setup /u