在sharepoint開發中,自定義的feature或是webpart要發佈到Production Server上。很多時候是開發人員先在test server上開發並測試,然後發佈到production server,因此部署的時候是比較麻煩的,比較方便的方法是將開發的feature和webpart打包成一個sharepoint solution(.wsp文件),然後使用stsadm命令發佈到指定的server上。下面先來介紹如何將一個feature打包成solution併發布。
Step1. 先來創建一個文件夾結構吧。如下所示
MyTestSolution (Solution文件夾)
Bin (放置生成的Solution文件)
Source (Feature以及其他需要包含的文件)
SuhuaTestList (自定義的Feature)
ListTemplates
Messages
其中Feature的組織結構是仿照Sharepoint中的Feature結構,可以到
C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES/ 目錄下查看DiscussionList。
Step2. 創建Feature
我們就基於Sharepoint discussions list來創建一個Feature。在SuhuaTestList文件夾下創建一個feature.xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="C6C9FBCB-330F-483b-9367-7A7BD9AAF34B"
Title="Suhua Test Feature"
Description="This is my feature containing a list"
Version="1.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="ListTemplates/MyListManifest.xml" />
<ElementFile Location="Messages/schema.xml"/>
</ElementManifests>
</Feature>
其中的Feature Id可以使用Guidgen.exe創建。
從C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES/DiscussionsList/Discuss中將
schema.xml拷貝到Messages文件夾下。
在ListTemplates文件夾下創建MyListManifest.xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListTemplate
Name="Messages"
Type="108"
BaseType="0"
OnQuickLaunch="FALSE"
FolderCreation="FALSE"
SecurityBits="12"
Sequence="999"
DisplayName="Suhua test List"
Description="This is my custom list based on the discussions list"
Image="/_layouts/images/itdisc.gif"/>
</Elements>
這裏要注意的是,ListTemplate標籤下的Name屬性必須跟schema.xml所在的文件夾(Messages)的名字一樣。
Step3. 構建Solution Package(.wsp文件)
首先要在Source文件夾下創建一個manifest.xml文件,這個文件定義了要處理的features, site definitions, resource files, Web Part files, and assemblies。如果一個文件包含在solution中但是沒有在manifest.xml文件中列出來,這個文件就不會被處理。
<?xml version="1.0" encoding="utf-8"?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/"
SolutionId="09BC4315-5234-4070-BBFA-B437B737370A" >
<FeatureManifests>
<FeatureManifest Location="SuhuaTestList/Feature.xml"/>
</FeatureManifests>
</Solution>
接下來就要生成wsp文件了,.wsp文件實際上是一個.cab文件,區別就是後綴名不一樣。可以使用makecab.exe創建,這需要一個.ddf文件。在Source文件夾下創建一個文件wsp.ddf,內容如下所示:
.OPTION Explicit
.Set DiskDirectory1="../bin"
.Set CabinetNameTemplate="SuhuaListSolution.wsp"
manifest.xml
.Set DestinationDir="SuhuaTestList/ListTemplates"
SuhuaTestList/ListTemplates/MyListManifest.xml
.Set DestinationDir="SuhuaTestList/Messages"
SuhuaTestList/Messages/schema.xml
.Set DestinationDir="SuhuaTestList"
SuhuaTestList/Feature.xml
在這個文件中,我們將生成的wsp文件放置到../bin目錄下,名字爲SuhuaListSolution.wsp。接下來告訴makecab有四個文件,同時在部署時要創建三個文件夾(DestinationDir)。
現在一切都準備好了,使用makecab.exe就可以創建solution了,也就是wsp文件。可以運行cmd,然後執行makecab。這裏介紹個簡單的可以重複使用的方法,就是建立一個build.cmd文件,然後以後也可以直接拷貝到其他的需要的地方雙擊執行就行:
@setlocal
@pushd.
@cd %~dp0
makecab /f wsp.ddf
@popd
@endlocal
現在去bin目錄下看看,是不是生成了一個wsp文件。這個文件就是我們需要的sharepoint solution 文件了。
Step4. 部署solution
生成的wsp文件,如何部署到production server中呢?這就要用到Stsadm命令工具。此它位於%PROGRAMFILES%/common files/microsoft shared/web server extensions/12/bin目錄下,利用此命令行工具,可訪問整套 Windows SharePoint Services 3.0 操作。可通過命令行或通過批處理文件或腳本來使用 Stsadm。必須在服務器本地運行 Stsadm。
運行cmd,到stsadm所在的目錄執行以下命令:
stsadm.exe -o addsolution -filename MyListSolution.wsp
stsadm.exe -o deploysolution -name MyListSolution.wsp -local
上面第一條命令是將solution添加到sharepoint中,第二條命令是部署到solution中。
如果想要移除這個solution可以執行下面的命令
stsadm.exe -o retractsolution -name MyListSolution.wsp –local
stsadm.exe -o deletesolution -name MyListSolution.wsp
除此之外,也可以使用Sharepoint 3.0 Central Administration來部署solution。方法是啓動Administrative Tools中的Sharepoint 3.0 Central Administration,然後選擇Operations中的Solution Management。
Step5. 使用Features
部署完之後,就可以使用自定義的discussion list啦,但必須先激活這個feature。打開sharepoint,在Site Setting中選擇Site features,然後激活前面創建的feature。接着就可以在Create頁面中看到自定義的List了。
(參考:http://geekswithblogs.net/evgenyblog/archive/2008/01/27/118966.aspx)