So far I've shown you guys how to deploy web parts and add them to pages with all of the related files. What I haven't shown you is how to deploy these things in a UAT or Production environment where STSDEV in Visual Studio won't be available.
So how should it be done? Well there are a few approaches, but by far the newest and coolest way is to use Powershell because it gives you all the advantages of the command prompt and batch files but also gives you access to the API. There's plenty out there to get you started with Powershell and I was amazed at the power of it...it's saved me heaps of time in debugging because you can simply use it to inspect objects without boosting up and attaching to the process.
Once you've installed Powershell the first thing I did was create a new .ps1 script and put it in my DeploymentFiles folder and add these lines of script:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Then you have full access to the API...so I can create a SPSite object:
$weburl = "http://jt-asuslaptop"
$web=new-object Microsoft.SharePoint.SPWeb($weburl)
Scripts
Zach Rosenfield has some great scripts already written to access certain objects which I'll use. These are more intuitive and I've placed them in my default Powershell profile file which loads automatically when you start Powershell.
$weburl = "http://jt-asuslaptop"
$web = global:Get-SPWeb($weburl, $null)
Execution Policy
There is alot out there on this subject so I'll leave it to the Powershell pros, but basically you need to change the policy to get it to run scripts. I simply ran to give me god mode on my developer machine (obviously on production you'd want to be a bit more careful):
set-executionpolicy unrestricted
Inspecting Objects
Fire this off will give you heaps of information back on the SPWeb object...too much to copy and paste here...but what you can start doing is using the format-table function to output selected things:
$web | ft Title, Url
This will give you more targeted information:
Title Url
----- ---
Home http://jt-asuslaptop
So you can see the power in this for inspecting properties etc.
Firing methods
The other thing it is really great for is firing methods...for instance deleting SPWebs. As I discussed in a previous article, stsadm –o deleteweb won't work if the web has subweb's so you need to wipe them all recursively. The nice way of doing this is below...which only deletes the subwebs recursively:
############################################
# Delete Web -url <url>
############################################
function global:Delete-SPWeb($url){
$web = global:Get-SPWeb($url, $null)
foreach($subweb in $web.Webs)
{
global:Delete-SPWeb($subweb.Url);
$subweb.Delete();
}
$web.Delete();
}
Populating Lists with Test Data
The other great thing is populating lists with test data, these rigs will save you heaps of time with postbacks in interface...no one can enter data as quick as this:
#create some sample categories
$spCategoryList = $web.Lists["Categories"]
$categories = "Travel","Recruitment","The Way We Work"
foreach($category in $categories)
{
$spitem = $spCategoryList.Items.Add()
$spitem["CategoryName"] = $category
$spitem.Update()
}
I'll be posting more full examples of this stuff very shortly that shows off more of the Powershell arsenal!