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.

 

Failed to open the default internet browser SharePoint : Resolved

Recently i uninstalled Chrome browser from my machine. After that when i tried to open Central Administration then below mentioned

error popped up as message.
Failed to open the default internet browser. An Internet browser is not  installed or has not been configured to be your default browser
I checked the target of Central Administration and found it was correct i.e. “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\psconfigui.exe” -cmd showcentraladmin”

So issue was that Central Administration application was unable to detect default browser. Though i have 2 different browsers on my machine including IE 9.

Below are the steps to fix the issue. Though these steps are specific to Windows 7 but you can use them with any Windows OS.

  1. Go to Control Panel.
  2. Click on “Default Programs“.
  3. Click on “Set your default programs“.
  4. Click “Internet Explorer” under Programs section (Left hand side in Windows 7) and click on “Set this program as default“.

You are done. Happy SharePointing 🙂

Custom Site Definition for Creating Sub Site

Suppose you create a Custom Site Definition.  Now when the user creates an instance of this you also want to create a SubSite (from a different Site Definition) in the current site. Below is the solution to achieve this.

The way to do this is through the publishing assemblies in the webtemp file of your Site Definitions. SharePoint uses the webtemp files to point to the correct site definition when creating the sites. Usually the webtemp files contain an xml section that points to one site definition. However, the publishing sites point to an assembly by filling in the provision assembly attribute of the xml. It also takes in an xml file as the parameter for the assembly. This xml file has the structure of the subsites you want to create. I recommend looking at the webtemp files for the publishing sites to see how this is done.

Here is a quick example of an xml node out of a webtemp file I use for demos:

<Template Name=”DemoCompanySiteDefinitionPortal” ID=”10001″>

<Configuration ID=”0″ Title=”Custom Site Definition with subsites” Hidden=”FALSE”

ImageUrl=”/_layouts/images/blankprev.png”

Description=”Builds a custom site definition with subsites”

ProvisionAssembly=”Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”

ProvisionClass=”Microsoft.SharePoint.Publishing.PortalProvisioningProvider”

ProvisionData=”xml\\MyCustomSubsiteStructure.xml”

RootWebOnly=”TRUE”

DisplayCategory=”DemoCompany” Type=”0″ />

</Template>

Notice the ProvisionAssembly, ProvisionClass and ProvisionData. The ProvisionAssembly and ProvisionClass are the out of the box assemblies SharePoint provides for you. So, don’t change them. The ProvisionData class is the parameter that the ProvisionClass takes in. This is the xml file that defines your subsite structure.

<?xml version=”1.0″ encoding=”utf-8″ ?>

<portal xmlns=”PortalTemplate.xsd”>

<web name=”Home”

siteDefinition=”DemoCompanySiteDefinition#0″

displayName=”$Resources:cmscore,IPPT_Portal_Root_DisplayName;”

description=”$Resources:cmscore,IPPT_Portal_Root_Description;” >

<webs>

<web name=”SubSite1″

siteDefinition=”DemoCompanySiteDefinitionSubSite#0″

displayName=”Sub Site”

description=”Custom Demo Company sub site.” />

</webs>

</web>

</portal>

Notice that the xml structure of the ProvisionData contains the schema PortalTemplate.xsd. This schema is a simple one that just has “Webs” and “Web” nodes. You need one root web and then you can build your subsite structure as deep as you want. Each web needs to point to a Site Definition and you have to put the #{number} to define the configuration in the Site Definition that you want to use.

Steps to search a specific custom list

Step 1: Go to “Site Actions“, “Site Settings” and under “Site Collection Administration” click on “Search Scopes“.

Step 2: Click on “New Scope” and give it a title and leave other fields intact then click ok.

Step 3: Now add rule to this Newly created scope, click add rule.

Step 4: In Add Scope Rule window.
Select Scope Rule Type: Web Address (http://server/site)
In Web Address select Folder and type the url of custom list e.g. http://moss2007machine:55555/Lists/KallisCustomList
In Behavior select Include click OK

Step 5: You will see New scope-Ready after next update (starts in 3 minutes). Wait for update.

Step 6: After update process select Edit Properties and Rules from the context menu of new scope.

Step 7: In Scope Properties and Rules window click on New Rule.

Step 8: In Add Scope Rule Window,
Select Scope Rule Type is Property Query(Author=John DOe)
In Property Query section,
Select contentclass in Add property restrictions combo box.
In text box type STS_ListItem_GenericList
In Behavior secton now select Require. and finally click OK.

Step 9:You will see New scope-Ready after next update (starts in 3 minutes). Wait for update.

Step 10: Click on “New Display Group” and give it a title and a description. In the “Scopes” section select the scope you created in Step 2. The “Default Section” should automatically become the scope you selected.

Step 11: Wait for the server to recognize the new scope and display group.

Step 12: Go back to the page you wish to add a search box. Go to “Site Actions“, “Edit Page“, click on “Add a Web Part“. Select the “Search Box” web part and click “Add“.

Step 13: The page should be in “Edit Mode“. In the “Search Box” web part click on the “edit” button and go to “Modified Shared Web Part”.

Step 14: Expand the “Miscellaneous” section. UncheckUse site level defaults“. In the “Scope Display Group” sub-section the default display group scope will be “Search Dropdown” change this to the display group you created in Step 10. You cannot browse through the display groups so you will have to type your display group in exactly. Click on “OK“.

Step 15: Exit out of “Edit Mode“. Your search box should now have the new search scope you created in Step 2 as the default choice.

Step 16: Now by using Central Administration crawl the content of site, after crawling full
content your search scope is now ready to use. Select the new scope and in searchbox type search term
You will get the results from only specific custom list and only list item will return back.