Friday, 25 July 2014

Update page layout using Power Shell

# Parameters:
# -PageLayoutCurrent     - The page layout that is currently in use and will be updated
# -PageLayoutNew        - The new page layout that pages will be updated to
# -all                     - Update subsites in the site collection
#
# Modifications:
# v1.0 - April 5th, 2011
# Initial version
#
# Settings
set-variable -option constant -name url  -value http://localhost   # Site collection
set-variable -option constant -name comment -value "Batch PageLayout Update"   # Publishing comment

# Function: Update-SPPagesPageLayout
# Description: Update a single page in a Publishing Web
# Parameters: publishingPage, pageLayout, comment
function Update-SPPagesPageLayout ([Microsoft.SharePoint.Publishing.PublishingPage]$publishingPage,
    [Microsoft.SharePoint.Publishing.PageLayout] $pageLayoutNew, [string]$comment)
{
 if($publishingPage.ListItem.File.CheckOutStatus -eq "None")
 {
  Write-Host "Updating the page:" $publishingPage.Name "to Page Layout:" $pageLayoutNew.Title
  $publishingPage.CheckOut();
  $publishingPage.Layout = $pageLayoutNew;
  $publishingPage.ListItem.Update();
  $publishingPage.Update();
  $publishingPage.CheckIn($comment);
  if ($publishingPage.ListItem.ParentList.EnableModeration)
  {
   $publishingPage.ListItem.File.Approve("Publishing Page Layout correction");
  }
 }
 else
 {
  Write-Host "****************************************************************************************************************************************************************"  
  Write-Host "Cannot update the page:" $publishingPage.Name "to Page Layout:" $pageLayoutNew.Title " as the file is checked out by" $publishingPage.ListItem.File.CheckedOutBy
  Write-Host "****************************************************************************************************************************************************************"
 }
}

# Function: Update-AllSPPagesPageLayouts
# Description: Loop through all the pages in a Publishing Web and update their page layout
# Parameters: web, pageLayoutCurrent, pageLayoutNew, comment
# comment Comment to accompany the checkin
Function Update-AllSPPagesPageLayouts ([Microsoft.SharePoint.SPWeb]$web, [Microsoft.SharePoint.Publishing.PageLayout]$pageLayoutCurrent,
    [Microsoft.SharePoint.Publishing.PageLayout]$pageLayoutNew, [string]$comment)
{
    #Check if this is a publishing web
    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true)
    {
      $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
      $pubcollection=$pubweb.GetPublishingPages()
      #Go through all pages checking for pages with the "current" page layout
      for($i=0; $i -lt $pubcollection.count; $i++)
      {
        if($pubcollection[$i].Layout.Title -eq $pageLayoutCurrent.Title)
        {
            Update-SPPagesPageLayout $pubcollection[$i] $pageLayoutNew $comment
        }
      }
    }
    $web.Close();
}

# Check Parameters
if(($args[0] -ne $null) -and ($args[1] -ne $null))
{
    Write-Host "** Update Layout Pages from-" $args[0] "-to-" $args[1] "-on URL" $url
    $pageLayoutNameCurrent = $args[0];
    $pageLayoutNameNew = $args[1];

    $site = new-object Microsoft.SharePoint.Publishing.PublishingSite($url)

    Write-Host "Checking if both page layouts exist in the site..."
    # Check if the current pagelayout exists in this site collection
    $pageLayouts = $site.GetPageLayouts($true);

    $pageLayouts | ForEach-Object {
        if ($_.Title -eq $pageLayoutNameCurrent)
        {
            Write-Host "Found CURRENT page layout: " $pageLayoutNameCurrent
            $pageLayoutCurrent = $_;
        }
    }

    # Check if the new pagelayout exists in this site collection
    $pageLayouts | ForEach-Object {
        if ($_.Title -eq $pageLayoutNameNew)
        {
            Write-Host "Found NEW page layout: " $pageLayoutNameNew
            $pageLayoutNew = $_;
        }
    }      

    # Do not continue if the either pageLayout does not exist
    if(($pageLayoutCurrent -ne $null) -and ($pageLayoutNew -ne $null))
    {
        # Update all subsites
        if($args[2] -eq "-all")
        {
         $site.Site.allwebs | foreach {
            Write-Host "Checking Web: " $_.Title
            Update-AllSPPagesPageLayouts $_ $pageLayoutCurrent $pageLayoutNew $comment
            }
        }
        else
        {
         $site.rootweb | foreach {
            Write-Host "Checking Web: " $_.Title
            Update-AllSPPagesPageLayouts $_ $pageLayoutCurrent $pageLayoutNew $comment
            }
        }
    }
    Write-Host "**Done"
}
else
{
    Write-Host "Missing arguments.  Please check your parameters"
}
#End

Saturday, 12 July 2014

Cannot delete content type :( start by finding its usages.

      
 using (SPSite siteCollection = new SPSite("http://localhost"))  
      {  
       using (SPWeb webSite = siteCollection.OpenWeb())  
       {  
         // Get the obsolete content type.  
         SPContentType obsolete = webSite.ContentTypes["Test"];  
         if (obsolete != null) // We have a content type  
         {  
          IList usages = SPContentTypeUsage.GetUsages(obsolete);  
          if (usages.Count > 0) // It is in use  
          {  
            Console.WriteLine("The content type is in use in the following locations:");  
            foreach (SPContentTypeUsage usage in usages)  
             Console.WriteLine(usage.Url);  
          }  
          else // It is not in use.  
          {  
            // Delete it.  
            Console.WriteLine("Deleting content type {0}...", obsolete.Name);  
            webSite.ContentTypes.Delete(obsolete.Id);  
          }  
         }  
         else // No content type is found.  
         {  
          Console.WriteLine("The content type does not exist in this site collection.");  
         }  
       }  
      }  
      Console.Write("\nPress ENTER to continue...");  
      Console.ReadLine();