Remove orphan feature(s) entry from SharePoint’s ManageFeatures.aspx

At times we need to get rid of feature(s) from SharePoint farm, those are no longer required. This is kind of cleaning and managing your SharePoint Farm. Most of the time due to inappropriate procedure or by using a 3rd party tool, Feature gets removed but it still displays on Feature.aspx page. When you try to search this feature through PowerShell or by tool, this feature becomes unavailable.

The simple reason of this is that it gets degraded and loose its scope and it has an orphaned entry in database.

So below are the steps to resolve this i.e. to clean up the feature reference from ManageFeature.aspx page.

  • First of all open ManageFeature.aspx and copy the title of the feature that needs to be removed from the page. Paste the title in a notepad file (To remove any formatting or extra space while copying the title).
  • Open SharePoint Management Shell as an administrator.
  • Run below commands:

$feature = Get-SPFeature | ? { $_.GetTitle(1033) -eq “Orphaned feature name” }

$feature.Delete()

Happy SharePointing !

Advertisements

Download Files/Images from SharePoint Libraries using PowerShell

I consigned a task to backup all files inside different document libraries of a SharePoint site on hard disk. We need to reorganize the entire site and this site comprises so many document libraries. So downloading documents one by one is a gigantic task as the site has gigs of docs. Alternative option is to open libraries in explorer and start copying them to a place, but this methodology has so many limitations e.g. network speed, installation of office components, much time required etc.
I want to get this job done hastily. So for a moment I thought of Power Shell. I want to give it a try and trust me by using this I was able to download gigs of documents within minute. Below is the code snippet for your reference:

 

######################## Start Variables ########################
######################## Download Files Script######################
$destination = $args[0]
$webUrl = $args[1]

##############################################################
# Replace actual document libraries name with DocLib1, 2, 3 etc.
$listUrls=”DocLib1″,”DocLib2″,”DocLib3″,”DocLib4″,”DocLib5″

$web = Get-SPWeb -Identity $webUrl

foreach($listUrl in $listUrls)
{
$list = $web.GetList($webUrl+”/”+$listUrl)

function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + “/” + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + “/” + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
        }
}

#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}}

 

Copy this to a notepad file and save with extension .ps1 E.g. DownloadFiles.ps1

Open SharePoint Management Shell 2010; navigate to the path of script. Provide destination path as argument 1 and site URL as argument 2 as shown below.  

.\DownloadFiles.ps1 “d:\documents” http://mySite

*This will create separate folders for each document library with library name.

 

Set Welcome Page using PowerShell : SharePoint 2010

By default a SharePoint 2010 publishing site uses default.aspx as its welcome page. There are so many conditions where we use our custom web part page as the default page for our publishing site in place of general default.aspx. The best example for this is an application dashboard as the default page. Though its very easy to set the welcome page in a SharePoint site by navigating to Site Settings and under Look and Feel section click on Welcome Page.

Now the question is why i wrote power-shell script for such a simple task.

Answer 1: I want a one click deployment solution for my custom code that includes features, event receivers, web parts etc. I don’t want to remember steps, just i run a batch file and execute everything inside from that batch file.

Answer 2: In my solution i have custom pages and i wrote the deployment script to create those pages. During development if we change any page for java-script, CSS etc. then we need to redeploy the pages to check the implementation. So we need to delete all pages first and then redeploy them otherwise pages so the double instance of each web parts inside that. Well we know that you can’t delete a page directly that is set as welcome page in SharePoint site so in this case you need to set another page as welcome page first then delete the page you want to. So during my development cycle this was very irritating that before deployment of my custom pages i need to first  set Under_Construction.aspx page as my welcome page then delete my actual welcome page then deploy custom pages and then again set the actual page as welcome page. What is your machine is slow and after any deployment it runs pathetically slow..

Oh my god it sucks.. So for all this i wrote a script to set welcome page so that before and after the deployment of pages i could run this and rest of the work it do automatically. So below is the script:

Add-PsSnapin Microsoft.SharePoint.PowerShell
$assignment=Start-SPAssignment
$web=Get-SPWeb -Identity “http://kailash1689new:9007” -AssignmentCollection $assignment
$rootFolder=$web.RootFolder
$rootFolder.WelcomePage=”Pages/Dashboard.aspx”
$rootFolder.Update()
Stop-SPAssignment $assignment
$web.Update()
$web.Dispose()
Write-Host ‘Welcome Page Set Successfully…’
Write-Host ‘Press any key to exit…’
$x=$Host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)
$Host.SetShouldExit(1)