Tuesday, November 20, 2012

Enable the SharePoint Server Publishing Feature in SharePoint 2010 using Powershell

To use the SharePoint Server publishing feature in a SharePoint site, we need to activate the PublishingSite in site collection level as a first step and finally activate the publishing web to all webs in that site collection.


Script to Activate the SharePoint Server Publishing Feature

#Step 1: The SharePoint Server Publishing Infrastructure Feature needs to be enabled in site collection level
$PublishingSitefeature = Get-SPFeature PublishingSite #where the PublishingSite is the inernal name of the SharePoint Server Publishing Infrastructure Feature
write-host "The feature name is " $PublishingSitefeature.DisplayName
$siteCollection = Get-SPSite $siteUrl #Into Site Collection level
write-host "Activating " $PublishingSitefeature.DisplayName " on "  $siteCollection.Url
Enable-SPFeature $PublishingSitefeature -Url $siteCollection.Url #Enable the feature to the site collection
write-host "Activated " $PublishingSitefeature.DisplayName " on "  $siteCollection.Url
$siteCollection.Dispose()

#Step 2: Then enable the SharePoint Server Publishing feature to all webs in Site collection.
$siteCollection = Get-SPSite $siteUrl #Into Site Collection level
$siteCollection | Get-SPWeb -limit all | ForEach-Object{
write-host "Activating the PublishingWeb feature on " $_.Url
Enable-SPFeature -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature
write-host "Activated the PublishingWeb feature on " $_.Url
}
$siteCollection.Dispose()

#Note: Publishing feature needs to be enabled in site collection before enabling it to all the webs

Remove a WebPart from SharePoint Pages using Powershell

The following script is very useful in removing a webpart from SharePoint page.


if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$spWeb = Get-SPWeb $siteUrl -ErrorAction Stop

#Declare the absolute path to the SharePoint page
$pagePath = "/SitePages/Home.aspx"
$pageUrl = $siteUrl + $pagePath
write-host "Processing site: ", $siteUrl
write-host "Processing page: ", $pageUrl

#Initialise the Web part manager for the specified profile page.
$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

#List all the Webparts in the specified page
foreach ($webpart in $spWebPartManager.WebParts)
{
    write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID
    break;
}
#Remove the Share Documents Web part from that page
foreach ($webpart in ($spWebPartManager.WebParts | Where-Object {$_.Title -eq "Shared Documents"}))
{
    write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID
    $webpart1 = $webpart
    break;
}
#Delete the existing webpart
$spWebPartManager.DeleteWebPart($spWebPartManager.WebParts[$webpart1.ID])
write-host "Deleted the existing Shared Document web part."

$spWeb.Dispose()

Delete Navigation nodes using Powershell in SharePoint 2010 QuickLaunch.

This script is used to delete the navigation nodes in the  SharePoint Quick Launch (Left side - navigation links) in the site.


if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}


$spWeb = Get-SPWeb $siteUrl -ErrorAction Stop
Foreach($node in ($spWeb.Navigation.QuickLaunch)){
write-host "The node title is "$node.Title
$node.Delete()
}
$spWeb.Dispose()

Wednesday, November 14, 2012

Unable to update SharePoint 2010's managed metadata field in custom developed Content Types/Library on SharePoint 2010


We have developed a Custom SharePoint 2010 site definition with all the libraries, lists and content types. Its been successfully deployed to production too.

Then we had an unusual problem, i.e.Updating the document managed metadata(Taxonomy field) property via 3rd party tool Colligo, doesn't get updated in the SharePoint library. 

Initially thought it would be colligo problem, but later in testing realised that, browsing directly in custom SharePoint site too, it updates only once then the managed metadata property doesn't get updated.

Went through lots of sites/blogs, about taxonomy field. 
Recommend to read this link" Dissecting-the-SharePoint-2010-Taxonomy-fields " for understanding internal of taxonomy field.

Everybody mentioning about the taxonomy hidden list and timer jobs, but it didn't solve the problem.

On testing the same taxonomy field in normal team site it works perfectly.

Later got revealed the taxonomy hidden facts, i,e, there are some in-built Taxonomy Event Receivers which needs to get added to the list/library to get it working similar in normal Team site.

The code for adding the taxonmy event reciever via a normal application
using (SPSite projectSite = new SPSite(siteText.Text.Trim()))
{
using (SPWeb projectWeb = projectSite.OpenWeb())
{
SPList projectList = projectWeb.Lists[Custom List Title];
//Initialise all the event recievers of the above list to the a collection
        SPEventReceiverDefinitionCollection listEventRecieverCollection = projectList.EventReceivers;
//Add the TaxonomyItemSynchronousAddedEventReceiver to the list (Built in Event Reciever from Microsoft - not custom code reciever)
SPEventReceiverDefinition AddedEventdef = listEventRecieverCollection.Add();
AddedEventdef.Assembly = "Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
AddedEventdef.Class = "Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver";
AddedEventdef.Name = "TaxonomyItemSynchronousAddedEventReceiver";
AddedEventdef.Type = SPEventReceiverType.ItemAdding;
AddedEventdef.SequenceNumber = 10000;
AddedEventdef.Synchronization = SPEventReceiverSynchronization.Synchronous;
AddedEventdef.Update();
//Add the TaxonomyItemUpdatingEventReceiver to the library (Built in Event Reciever from Microsoft - not custom code reciever)
SPEventReceiverDefinition UpdatingEventdef = listEventRecieverCollection.Add();
UpdatingEventdef.Assembly = "Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
UpdatingEventdef.Class = "Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver";
UpdatingEventdef.Name = "TaxonomyItemUpdatingEventReceiver";
UpdatingEventdef.Type = SPEventReceiverType.ItemUpdating;
UpdatingEventdef.SequenceNumber = 10000;
UpdatingEventdef.Synchronization = SPEventReceiverSynchronization.Synchronous;
UpdatingEventdef.Update();
}
}


Add the above in-built Taxonomy Event receiver while developing any custom solution with lists/libraries.

This solves a huge problem in updating the managed metadata in custom sites based solution.