Creating SharePoint 2010 site structure automatically with PowerShell

March 10th, 2010
Share this on Twitter

Just came back from a usergroup meeting where Charles Cote was talking about PowerShell and got inspired to share a piece of script that does site auto provisioning in SharePoint 2010.

Whether you have a complex site hierarchy or just want to automate simple deployment having an automated script that reads from configuration file and creates a site structure and activates relevant features is a real time saver.

Here is a simple XML file I created to define my site structure and any other relevant info (FYI: it`s really up to you how you want this structure to look like – I just created it from the top of my head)

<Setup WebAppUrl=”http://mywebappurl”>
<SiteCollection Name=”Site Collection Name” Url=”/sites/mysitecollection” OwnerAlias=”my_install_username” Template=”STS#0″>
<Site Name=”MySite” Url=”MySite” Template=”STS#0″>
<Feature>FeatureName</Feature>
</Site>
<Site Name=”MySite1″ Url=”MySite1″ Template=”STS#0″>
</Site>
</SiteCollection>
</Setup>

<Setup WebAppUrl=”http://mywebappurl”>

<SiteCollection Name=”Site Collection Name” Url=”/sites/mysitecollection” OwnerAlias=”my_install_username” Template=”STS#0″>

<Site Name=”MySite” Url=”MySite” Template=”STS#0″>

<Feature>FeatureName</Feature>

</Site>

<Site Name=”MySite1″ Url=”MySite1″ Template=”STS#0″>

</Site>

</SiteCollection>

</Setup>

Now, in the script below, assuming you have a basic PowerShell 2.0 knowledge, I will read the XML and run through the loop of defined structure elements to create a new site based on parameters provided in the XML (SiteStructure.xml):

[xml]$SiteStructure = get-content SiteStructure.xml

$WebAppUrl = $SiteStructure.Setup.Attributes.Item(0).Value

$SiteCollectionUrl = $SiteStructure.Setup.SiteCollection.Attributes.Item(1).Value

$SiteUrl = $WebAppUrl + $SiteCollectionUrl

for ($i=0; $i -lt $SiteStructure.Setup.SiteCollection.ChildNodes.Count; $i++ )

{

$childsite = $SiteStructure.Setup.SiteCollection.ChildNodes.Item($i);

$WebName = $childsite.Attributes.Item(0).Value

$WebUrl = $childsite.Attributes.Item(1).Value

$WebTemplate = $childsite.Attributes.Item(2).Value

Write-Host “Creating new web at” $SiteUrl/$WebUrl

$NewWeb = New-SPWeb $SiteUrl/$WebUrl -Template $WebTemplate -Addtotopnav -Useparenttopnav -Name $WebName

Write-Host “Web created successfully”

Write-Host “Title:” $NewWeb.Title -foregroundcolor Green

Write-Host “URL:” $NewWeb.Url -foregroundcolor Green

$features = $SiteStructure.Setup.SiteCollection.ChildNodes.Item($i)

if($features.Feature.Length -gt 0)

{

foreach ($WebFeature in $features.Feature)

{

$ActivatedFeature = Enable-SPFeature $WebFeature -url $NewWeb.Url

Write-Host “Enabled Feature:” $WebFeature -foregroundcolor Green

}

}

Write-Host “…”

}

In here I showed how you can create webs, but hey, you can really create site collections too following the same pattern. Hope this will help you to streamline your deployments!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , ,

Interview: Sing Chan on branding for SharePoint 2010

March 9th, 2010
Share this on Twitter

You know that feeling when you finish your next public website that will be visited by hundreds and thousands of people each day, well, neither do I but I can imagine; that’s why I decided to talk to Sing today (his blog http://www.singchan.com) and figure out his process and what tools he’s using. We focused on SharePoint 2010 but there are general guidances too that you can use in any website production process. Check it out – 12 min!

Sing Chan on general and SharePoint 2010 branding from Yaroslav Pentsarskyy on Vimeo.

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 ,

Removing metadata from SharePoint 2010 search results

March 8th, 2010
Share this on Twitter

Recently I have been involved in a solution where I needed to customize my Search Core Results webpart. In short; I had to remove Created By and replace it with a piece of metadata called alias.

Seems like a trivial task – all you need to do is to adjust XSL of the Search Core Results and remove or replace Author field, but there is a catch.

First, open your Search Core Results XSL:

xsl editor

I recomment you copy and paste the contents of the XSL editor into notepad (at least) and find the following section:

<!– Main body template. Sets the Results view (Relevance or date) options –>

this section denotes a start of the XSL responsible for rendering the body of the results. In here you will find all of the components that put together a search result. In my case I found and replaced the document author retrieval  implementation with the following:

<xsl:call-template name=”DisplayAuthors”>
<xsl:with-param name=”alias” select=”alias” />
</xsl:call-template>

<xsl:call-template name=”DisplayAuthors”>

<xsl:with-param name=”alias” select=”alias” />

</xsl:call-template>

After the XSL was saved, this worked well but I was still getting the actual CreatedBy in my search results. The created by information turned out to be coming from  the property called hithighlightedsummary

If you take a look at the definition of this property in your search service application: http://[central admin url]/_admin/search/listmanagedproperties.aspx

you will notice that hithighlightedsummary doesn`t have any of the crawled properties defined explicitly. In fact, this property is a dynamic summary that has a collection of highlighted word pieces that best describe the result and give user the text around the keywords they were searching. Among those calculated pieces of metadata we have CreatedBy and any other relevant metadata.

This means that no matter how much you customize your XSL – you will still receive some parts of the metadata as part of the hithighlightedsummary . One compromise is to remove this property from your XSL and replace it with a item description, body, or any other metadata that is relevant in your case.

Here is the template and selector that call hithighlightedsummary  from the XSL:

<xsl:when test=”hithighlightedsummary[. != '']“>

<xsl:call-template name=”HitHighlighting”>

<xsl:with-param name=”hh” select=”hithighlightedsummary” />

</xsl:call-template>

</xsl:when>

To create your new custom summary field you would define the managed property that will incorporate relevant crawled properties in here: http://[central admin url]/_admin/search/listmanagedproperties.aspx and then reference it in the template as shown above.

Don`t forget to include your newly created managed property in a list of fetched columns  within your Search Core Results:

fetched columns

Good luck!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , , , ,

Dynamically changing SharePoint 2010 list form rendering templates

March 6th, 2010
Share this on Twitter

Here is the scenario: you have few lists in your portal that`s already deployed in production environment and contain data. Here you realize that you need to adjust one of the lists` rendering template to remove some information on a Displayor Edit form.  You can`t change the schema.xml of the list because you will have to recreate the it and your customer will loose all the data.

Assuming you already have a rendering template ready or have just deplo0yed it – here is one quick way to rescue the day:

1. Open site in SharePoint Designer.

2. Click Lists and Libraries.

3. Click on the list you`d like to change the template on (Comments in my example)

attachment.ashx

4. Select the View of your interest, in my case I`ll be changing Display Form (ViewComments.aspx in case of Comments list)

attachment.ashx (1)

5. The designer will load the view. Make sure you pick a split view to see visual and code of the form.

attachment.ashx (2)

6. Click on the form as shown below, that way SharePoint Designer will show the piece of code representing the display form webpart.

attachment.ashx (3)

7. In the “code” window find TemplateName in the highlighted section and find the “ListForm” property in that section and replace it with the rendering template that you have provisioned to the site.

That`s it, when done, just save the form and refresh the actual form page in your browser – the new form will be picked up right away.

attachment.ashx (4)

Good luck!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , ,