Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Saturday, October 19, 2013

Enable Publishing Feature recursively in SP 2010 using PowerShell

So now that we saw an inconsistency among multiple sites, with some having Publishing feature enabled while others not, we decided to run a script which combs through each site within a site-collection and enables this feature, if not already enabled.

Given the situation that we had approx. 25-50 sites/sub-sites in each of the 7 site-collections, this appeared fair-enough to tackle one site-collection at-a-time.

To meet this requirement, we had to perform the below 2 high-level tasks:

1. Check and enable the [SharePoint Server Publishing Infrastructure] at the root site-collection level, if not already enabled. (Can be seen from “Site collection features” on “/_layouts/settings.aspx” page).

image

2. Check and enable the [SharePoint Server Publishing] at the site-level, if not already enabled. (Can be seen from “Manage site features” on “/_layouts/settings.aspx” page).

image

Enabling these features, creates the below 4 Lists & Libraries.

image

image

Now the script:

Step 1 was achieved by the script below:

# The URL to the Site Collection
$spSite = Get-SPSite “http://siteurl/”
Write-Host "Root Site Collection: " $spSite

$PublishingSitefeature = Get-SPFeature PublishingSite
$siteCollection = Get-SPSite $siteUrl

#Check and Enable the feature on the site collection
if((Get-SPFeature $PublishingSitefeature -Site $siteCollection.Url -ErrorAction SilentlyContinue) -eq $null )
   {
       write-host -foregroundcolor Yellow "Activating the PublishingWeb feature on " $siteCollection.Url
       Enable-SPFeature $PublishingSitefeature -Url $siteCollection.Url   
       write-host -foregroundcolor Green "Activated the PublishingWeb feature on " $siteCollection.Url
   }
   else
   {
      write-host -foregroundcolor red "PublishingWeb feature already activated on : " $siteCollection.Url
   }

Step 2 was achieved by the following script:

$siteCollection | Get-SPWeb -limit all | ForEach-Object{
   if((Get-SPFeature "PublishingWeb" -Web $_.Url -ErrorAction SilentlyContinue) -eq $null )
   {
      write-host -foregroundcolor Yellow "Activating the PublishingWeb feature on " $_.Url
      Enable-SPFeature -Identity "PublishingWeb" -Url $_.Url
      write-host -foregroundcolor Green "Activated the PublishingWeb feature on " $_.Url
   }
   else
   {
      write-host -foregroundcolor red "PublishingWeb feature already activated on : " $_.Url
   }
}

But as every project is seen over-running the estimates. here too we did not have luck to our side.

After Step 1 was performed successfully, we hit the first issue when started the Step 2.

But in my case, I got the below error, and could see that only [Pages] library got created (just few mins ago) and [Images] library pre-existed ( not sure who created that 6months ago).

“Enable-SPFeature : Provisioning did not succeed. Details: Failed to create the 'Images' library. OriginalException: The feature failed to activate because a list at 'PublishingImages' already exists in this site. Delete or rename the list and try activating the feature again. At C:\Users\abcd\Documents\PS\ActivatePublishingFeatureonAllSites.ps1:86 char:23
+ Enable-SPFeature <<<< -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature + CategoryInfo : InvalidData: (Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature”

So I deleted this [Images] library using SPD 2010.

image

image

Deleting [Pages] required me to delete the [Forms] sub-folder and then all the sub-folders and files as below within the [Forms] sub-folder:

image

The above files were deleted using SPD 2010, but couldn’t delete the [Forms] folder. So I renamed the [Pages] library to [Pages2] and tried to re-run the PS script again. But this did not solve the issue. This time I received the below error:

“Enable-SPFeature : Provisioning did not succeed. Details: Failed to create the 'Pages' library. OriginalException: There can only be one instance of this list type in a web. An instance already exists. At C:\Users\abcd\Documents\PS\ActivatePublishingFeatureonAllSites.ps1:86 char:23
+ Enable-SPFeature <<<< -Identity "PublishingWeb" -Url $_.Url #where the PublishingWeb is the internal name of the SharePoint Server Publishing feature + CategoryInfo : InvalidData: Microsoft.Share...etEnableFeature:SPCmdletEnableFeature) [Enable-SPFeature], SPException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletEnableFeature”.

The reason for this being that you cannot delete few libraries like [Pages] in a site which have the publishing feature enabled. For the case of libraries like the [Images], [Documents] or even the [Site Collection Documents], it is not possible to remove them from the lists settings page or SPD or the manage content and structure tool.

It appears that these lists and libraries have a property AllowDeletion, which has been set as False.

We can use the below script to verify this:

$web = Get-SPWeb “http://siteurl/abcd/”
$lib = $web.Lists[“Pages2”]
$lib.AllowDeletion

This returns “False” as seen below:

image

So what do we do?

PowerShell to rescue again. This allows you to set the property to “True” and then successfully delete the library.

Use the below script to set the property to “True”:

$web = Get-SPWeb “http://siteurl/abcd/”
$lib = $web.Lists[“Pages2”]
$lib.AllowDeletion = $True
$list.Update()

I could then easily use SPD or the browser to delete the [Pages] library (in my case I renamed it to [Pages2]).

Finally I re-executed my script successfully and got the feature enabled.

Sunday, May 26, 2013

Export Term Store to csv using PowerShell

This is another approach towards exporting the term stores to a CSV file format using power shell. I have tried to modify and enhance my previous post to achieve this requirement of generating a  Microsoft Excel format.

So here it is.

#creating a header-row for the csv file

"termStore `t" + "termGroup `t" + "termGroup-LastModifiedDate `t" + "termSet `t" + "termSet-LastModifiedDate `t" + "termSetGUID `t" + "terms `t" + "termsGUID `t" + "terms-LastModifiedDate" >> C:\ExportTermStore.csv

$termSet = Get-SPTaxonomySession -Site http://mydomain/sites/cthub
$termStore = $termSet.TermStores[0]
$termStore.Groups | FT -Autosize Name
$termGroup = $termStore.Groups["myTermStoreGroupName"]   #This is the case when we know the name of the group and want just that. We can also have a foreach instead, to loop-through each of the TermStore Group. I chose the first.
$termStore = $termStore.Name #saving the termStore “Name” in the variable $termStore
$termGroup.TermSets | FT -Autosize Name

foreach ($termGroups in $termGroup.TermSets)
{
  foreach ($termSets in $termGroups.Terms)
  {
    foreach ($terms in $termSets.Terms)
    {
      $termStore + "`t" + $termGroups.Name + "`t" + $termGroups.LastModifiedDate + "`t" + $termSets.Name + "`t" + $termSets.LastModifiedDate + "`t" + $termSets.ID + "`t" + $terms.Name + "`t" + $terms.ID + "`t" + $terms.LastModifiedDate >> C:\ExportTermStore.csv
    }
  }
}

Saturday, March 23, 2013

Export Term Store to xml using PowerShell

This seemed so obvious while using Central Admin, but soon I realized that there is so much left for SharePoint team at Microsoft to accomplish.

I needed to discuss the Site Taxonomy with my colleagues during our weekly call, when suddenly I found myself searching for the script that would do the magic.

TechNet has this:

http://social.technet.microsoft.com/wiki/contents/articles/17874.sharepoint-2010-how-to-manage-the-term-store-via-powershell.aspx

Gary LaPointe has this:

http://blog.falchionconsulting.com/index.php/2012/03/exporting-and-importing-sharepoint-2010-terms/

Thanks to the above. This was sufficient for me to quickly get my script in-place:

Export-SPTerms  -Group (Get-SPTaxonomySession -Site "http://mydomain/sites/cthub").TermStores[0].Groups[2] -OutputFile "D:\CTHubExport\site_terms.xml" –Verbose

You probably will need to work with the parameters for TermStores[0] & Groups[2] to see that you are correctly referring to your Term Store and the needed Group within the TermStore.

This generated a beautifully formatted xml file.

Sunday, September 23, 2012

Site Image Logo in SP 2010

 

Seems very simple, when it comes to setting up a Logo Image for your SP 2010 Site and its sub-site.

Use the LogoImageUrl to set the path of the image (say in “Layouts” folder) in the <SharePoint:SiteLogoImage> as seen below:

<SharePoint:SPLinkButton runat="server" NavigateUrl="~sitecollection/">
             <SharePoint:SiteLogoImage LogoImageUrl="/_layouts/mysitename/my_site_logo.gif" runat="server"/>
</SharePoint:SPLinkButton>

But things get a little complicated, if a Site Admin sets a different image in the [Site Settings] – [Title, Description and Icon] under “Look and Feel”.

image

It seems that this property tends to override the master-page LogoImageUrl in the <SharePoint:SiteLogoImage> as shown in the code above.

So the Solution:

- Clear the [Title, Description and Icon] field under “Look and Feel” section in [Site Settings]. I mean set it to blank or update it to the same image as set in the master-page.

But what if you have 100s of sites/sub-sites (in my case, there were 1500 sub-sites).

Power-shell to our rescue….

PS script can help us do the update needed very conveniently. That’s why we all love PS in SP 2010.

So here it is. Simply replace your own site URL and the Image path and run this script.

$site = Get-SPSite http://www.mysitename.com
$site.AllWebs | foreach { $_.SiteLogoUrl = "/_layouts/mysitename/my_site_logo.gif"; $_.Update() }
$site.Dispose()

That’s all we need to do.Have a nice day !!!

Friday, January 7, 2011

Working with PowerShell “cmdlets”

Pronounced “Command-lets”, cmdlets are a great way of scripting Administrative tasks in SharePoint 2010. I personally feel that I was missing such a tool till last year working with MOSS 2007. But with cmdlets, it looks extremely promising that Microsoft has ultimately provided the SharePoint Administrators – a single platform to script their tasks and run commands remotely/locally.

Recently, I was trying to look into these cmdlets and here are some quick view into some of the most basic ones.

Interestingly, these cmdlets are in a format that resembles the verb-noun pair. The noun generally starting with a “SP” for all SharePoint cmdlets.

#1. Get-SPContentDatabase: This gives you a list of all the Content DBs in your farm, with information on ID, Name, Server and Site count.

Okay, now you must be thinking that I am going to get nasty by listing down all these cmdlets, uhh… you are definitely wrong.

Instead, I just figured-out how to retrieve a list of such SP cmdlets and also how to pull more information about each of them.

Get-Command and Get-Help can be really handy for anyone starting on these. Here’s an example of how to use the Get-Command.

Get-Command –PSSnapin “Microsoft.SharePoint.PowerShell”

Further-more, if you start creating your own scripts, you would also need to start using these two very often:

> Measure-Command: This gives you the cmdlets execution time.

> Trace-Command: gives you the ability for debugging short scripts.

Technorati Tags: ,,