注:本文所講內容以SharePoint2013版本爲例,開發工具以VS2013爲基礎。歷史版本也可以參考本文。
WSP:SharePoint Solution Package 解決方案包。
一、概念和工具
我們先引出WSP的由來。要想深入瞭解WSP,要先從SharePoint開發說起。SharePoint可開發的內容比較雜,最新版本的VS2013中已經包含了Sharepoint解決方案的基本模板,從創建到部署整個過程簡單易用,比起以前開發解決方案要容易多了。但還是不能脫離SharePoint宿主機環境,不能遠程開發和部署。不過SharePoint已經開始了新的編程思路就是SharePoint APP開發,這種開發模式已經可以在客戶端編程部署了。
工程師創建好SharePoint解決方案後,對列表庫、Web部件、事件、網站模板、工作流等多方面進行編程開發。這一個個的功能就是Feature,那麼如何最終把這些功能部署到SharePoint環境中呢,這就需要先把解決方案打包成WSP文件,其實這個WSP文件就是一個cab文件,是個壓縮文件,你如果感興趣可以用rar工具打開這個文件看看。
要想打包解決方案爲wsp文件,有很多種方法,以前我用過的方法有
WSPBuilder工具命令方式,相關鏈接CodePlex項目
WSPBuilderExtension,一個VS插件,這個比上面的方式更簡單
SharePoint Software Factory(SP|SF),這是一個Visual Studio擴展工具,最新版本已經支持了VS2013版本,相關鏈接VS擴展,CodePlex項目地址
當然VS2013開發SharePoint解決方案已經可以從上下文菜單中直接部署了。在部署過程中會自動打包解決方案。
當然一些其他第三方的cab生成工具也能幫助我們生成wsp文件,但操作過程還是繁瑣複雜一些。
這些工具的使用不是本文所要講解的部分,如何使用可關注我的博客的其他相關文章吧。
二、WSP部署、更新、刪除
先要說明一下:
解決方案分爲:增加、安裝(又名部署)、更新、收回、刪除,
Feature分爲:激活、停用
增加解決方案
STSADM和SharePoint PowerShell兩種命令,從SharePoint2013開始推薦用PowerShell命令集
STSADM命令示例如下(淘汰的用法):
>stsadm -o addsolution -filename <path>\YourSolution.wsp
PowerShell命令示例如下:
>Add-SPSolution -LiteralPath "wsp路徑\文件名.wsp"
對於增加後的解決方案,可到管理中心部署解決方案:
>進入管理中心--系統設置--管理場解決方案,我在這裏只引入功能所在位置,不去描述如何具體操作了。操作也很簡單,也容易理解。
安裝(部署)解決方案
>Install-SPSolution -Identity "YourSolution.wsp" -WebApplication "站點地址" -GACDeployment
更新解決方案
>Update-SPSolution -Identity "YourSolution.wsp" -LiteralPath "<path>\YourSolution.wsp" -GACDeployment -force
收回解決方案
>Uninstall-SPSolution -Identity "YourSolution.wsp"
刪除解決方案
>Remove-SPSolution -Identity "YourSolution.wsp"
注意:刪除解決方案前,先要收回解決方案。
獲取已增加的解決方案
>Get-SPSolution
解除解決方案的部署鎖定
>Remove-SPSolutionDeploymentLock
三、Feature的部署、停用
在一個解決方案中,會包含一個及其以上的Feature,解決方案部署到站點後,可通過命令來控制解決方案中的各個Feature的狀態。Feature概念可對應SharePoint解決方案項目中的Features目錄裏的結構定義。
獲得Feature
>Get-SPFeature
上面這個命令可獲得farm中的所有Feature,顯示“名稱、GUID、SharePoint功能版本”
>Get-SPFeature -Identity "<GUID>"
可以獲得一個ID是GUID的那個Feature,
安裝SharePoint功能
>Install-SPFeature -path "<YourSolution>_<FeatureName>"
卸載Feature
>Uninstall-SPFeature -Identity "<YourSolution>_<FeatureName>"
在給定範圍啓用Feature
>Enable-SPFeature -Identity "<YourSolution>_<FeatureName>" -URL "<siteUrl>"
在給定範圍禁用Feature
>Disable-SPFeature -Identity "<YourSolution>_<FeatureName>" -URL "<siteUrl>"
四、沙箱解決方案
>Update-SPUserSolution
>Install-SPUserSolution
>Uninstall-SPUserSolution
>Remove-SPUserSolution
>Get-SPUserSolution
>Add-SPUserSolution
五、定時器解決方案
安裝 AutoSenArvatodMailJob.wsp
1) Add-SPSolution -LiteralPath "c:\AutoSendMailJob.wsp"
2) Install-SPSolution -Identity "AutoSendMailJob.wsp" -WebApplication <siteUrl> -GACDeployment
3) iisreset
4) Disable-SPFeature -Identity "AutoSendMailJob_Feature2" -Url "<siteUrl>" 5) Enable-SPFeature -Identity "AutoSendMailJob_Feature2" -Url "<siteUrl>"
6) net stop SPTimerV4
7) net start SPTimerV4
更新 AutoSenArvatodMailJob.wsp
1) Disable-SPFeature -Identity "Arvato.BBA.AutoSendMailJob_Feature2" -Url "<siteUrl>"
2) Uninstall-SPSolution –Identity "Arvato.BBA.AutoSendMailJob.wsp" -WebApplication "<siteUrl>" -Confirm:$false
3) Remove-SPSolution –Identity Arvato.BBA.AutoSendMailJob.wsp -Confirm:$false
4) Add-SPSolution -LiteralPath "c:\Arvato.BBA.AutoSendMailJob2.wsp"
5) Install-SPSolution -Identity "Arvato.BBA.AutoSendMailJob.wsp" -WebApplication "<siteUrl>" -GACDeployment
6) iisreset
7) Enable-SPFeature -Identity "Arvato.BBA.AutoSendMailJob_Feature2" -Url "<siteUrl>" 8
8) net stop SPTimerV4
9) net start SPTimerV4
3、刪除多餘定時器方法
//查看 計時器 "ArvatoBBASendMailTimerJob"
Get-SPTimerJob | ?{$_.Name -match "ArvatoBBASendMailTimerJob"} |ft id,name
//Set job to a variable
$job = Get-SPTimerJob -id 59af3540-7ac0-4477-82f9-1f6ab9829aca
//And delete it.
$job.Delete()
最後說明一下:VS2013的SharePoint解決方案的bin\debug目錄下,編譯後會自動打包wsp文件。要想將方案遠程部署到企業環境,可拷貝這個wsp文件,寫好一個ps1的PowerShell腳本文件。交付給企業系統管理員執行安裝就可以。
PS1腳本文件,可以被C#調用執行,這部分的講解待後續博文摘寫,在此說明一下。
以編寫備份網站集PS1文件爲例:
Add-PSSnapin Microsoft.SharePoint.PowerShell backup-spsite -identity $args[0] -path $args[1] -force
該腳本用了backup-spsite來備份SharePoint網站集,並將網站的URL和備份路徑設成兩個參數,將其保存成後綴名爲ps1的文件,比如backup.ps1
寫一個批處理腳本,用於執行備份和寫入日誌。
@echo off SET SOURCE_SITE= SET DEST=C:\backup\RZH-SP-204_%date:~0,10%.bak SET LOG=C:\backup\BackupLog.txt echo %DATE% %time:~,5% :開始備份網站:%SOURCE_SITE%>> %LOG% powershell -command C:\backup.ps1 %SOURCE_SITE% %DEST% echo %DATE% %time:~,5% :網站:%SOURCE_SITE%備份完成>> %LOG% @echo on
將這個腳本保存成bat批處理文件
不知道大家能否看懂以上代碼,在這裏我就不說明了。
最後奉獻給大家一個完整的安裝wsp解決方案的腳本,如果寫的還不夠完整,請大家告知!
Add-PSSnapin Microsoft.SharePoint.PowerShell $solutionName = "SharePointProject2.wsp" $webUrl = "http://wisdom-moss/" $path = Split-Path -Parent $MyInvocation.MyCommand.Definition $solution = Get-SPSolution | select name foreach($s in $solution){ if($s -match $solutionName){ Write-Host -ForegroundColor Green "Solution '$solutionName' was existed." $deployed = Get-SPSolution -Identity $solutionName | select deployed if($deployed.Deployed){ Write-Host -ForegroundColor Green "Start uninstall solution..." Uninstall-SPSolution -Identity $solutionName -Confirm:$false -Verbose -WebApplication $webUrl #sleep 30s, need uninstall over. Start-Sleep -s 30 Write-Host -ForegroundColor Green "End uninstall solution!" } Write-Host -ForegroundColor Green "Start remove solution..." Remove-SPSolution -Identity $solutionName -Confirm:$false Write-Host -ForegroundColor Green "End remove solution!" } } Write-Host -ForegroundColor Green "Start add solution..." Add-SPSolution -LiteralPath ($path + '\' + $solutionName) Write-Host -ForegroundColor Green "End add solution!" Write-Host -ForegroundColor Green "Start deploy solution..." Install-SPSolution -Identity $solutionName -WebApplication $webUrl -FullTrustBinDeployment Write-Host -ForegroundColor Green "End deploy solution!" Write-Host -ForegroundColor Green "End all executed."
註釋:因爲我的解決方案被定義爲程序集部署目標到WebApplication,所以dll我希望部署到網站集的bin目錄下,如果您的解決方案希望部署到GAC,以用於將方案部署的全局,則需要將程序集部署目標到GlobalAssemblyCache。