Wednesday, October 29, 2014

Archive SharePoint Library Files into Sub-Folders using PowerShell

Requirement: We've a very large library with more than 20,000 files stored in the root of the document library without any sub-folders. Got to move files into Sub-folders to improve performance.

Solution: We analyzed the library content and decided to move files into sub-folders based on file's created date.

PowerShell script to move files into sub-folders in SharePoint document library:
#Import PowerShell Snap-in
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Create a custom function to archive files
Function Archive-Files($WebURL, $ListName)
{
 #Get the web and List
 $Web = Get-SPWeb $WebURL
 $List= $web.Lists[$ListName]

 $RootItems = $List.RootFolder.files 
 Write-host "Total number of files found in the Root: "$RootItems.count

 #Move each file in the root folder to archive folder
 foreach($item in $RootItems)
 {
   #Get the File
   $file = $Web.GetFile($item.URL)

   #Get the Month & Year values from the File's crated date
   $MonthYearValue = $file.TimeCreated.ToString('MMM-yyyy')
   
   # Try to Get the Archive Sub-Folder in the target Library
   $TargetFolder = $List.ParentWeb.GetFolder($List.RootFolder.Url + "/" +$MonthYearValue);
   
   #If the Archive folder doesn't exist, Create it!
   if ($TargetFolder.Exists -eq $false)
    {
       $TargetFolder = $List.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $MonthYearValue)
       $TargetFolder.Update() 
    }
    #Move the File
    $file.MoveTo($TargetFolder.Url + "/" + $File.name)  
  }
}

#Call the function to Archive Files
Archive-Files "http://sharepoint.crescent.com/sites/marketing/" "Invocies"
This script preserves Metadata, BTW! You can also move files into sub-folders of an another SharePoint library for archival.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Monday, October 27, 2014

Get SharePoint Document Library Inventory with PowerShell

This PowerShell script generates detailed report on the given SharePoint library's inventory in addition with its storage data. The output report contains:
  • File Name
  • File Size(KB)
  • Versions Size(KB)
  • Total File Size(KB)
  • Created on
  • Last Modified
  • Created by
  • Parent Folder
  • URL
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

# Function to retrieve detailed Library storage Report
Function GetLibraryStorageRpt($Folder)
{
    #Array to hold Storage data for all files
    $StorageDataCollection = @()
    
    $FileSize = 0
 $TotalFileSize = 0
    foreach ($File in $Folder.Files)
    {
        #Get File Size
        $FileSize = $File.TotalLength
        
        $VersionSize = 0
        #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
        {
            $VersionSize +=$FileVersion.Size
        }
        $TotalFileSize = $FileSize + $VersionSize
  
        $StorageDataResult = New-Object PSObject
 
        $StorageDataResult | Add-Member -type NoteProperty -name "File Name" -value $File.Name
        $StorageDataResult | Add-Member -type NoteProperty -name "File Size(KB)" -value $($FileSize/1KB)
        $StorageDataResult | Add-Member -type NoteProperty -name "Versions Size(KB)" -value $($VersionSize/1KB)
        $StorageDataResult | Add-Member -type NoteProperty -name "Total File Size(KB)" -value $($TotalFileSize/1KB)
        $StorageDataResult | Add-Member -type NoteProperty -name "Created on" -value $File.TimeCreated 
        $StorageDataResult | Add-Member -type NoteProperty -name "Last Modified" -value $File.TimeLastModified 
        $StorageDataResult | Add-Member -type NoteProperty -name "Created by" -value $File.Author.Name        
        $StorageDataResult | Add-Member -type NoteProperty -name "Parent Folder" -value $File.ParentFolder.URL
  $StorageDataResult | Add-Member -type NoteProperty -name "URL" -value $File.URL

        $StorageDataCollection += $StorageDataResult
 #Write-Host "Processing File:"$File.Name
    } 
      #Get Files in Sub Folders
        foreach ($SubFolder in $Folder.SubFolders)
        {   
         if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.
             {
                GetLibraryStorageRpt($SubFolder)          
             }
        }   
    return $StorageDataCollection
}

#Input Variables 
$WebURL = "http://sharepoint.crescent.com/sites/operations" 
$ListName ="Documents"

#Get the List
$List = Get-SPWeb $WebURL | Select -ExpandProperty "Lists" | Where-Object{$_.Title -eq $ListName}

#Call the function to get data 
$StorageDetails = GetLibraryStorageRpt($List.RootFolder)

write-host "Total Number of Files:" $List.ItemCount 
write-host "Library Created by:" $List.Author

#Calculate the Total Size
$TotalSize = ($StorageDetails | Measure-Object 'Total File Size(KB)' -Sum | Select -expand Sum)  
Write-host "Library Size in MB: "([Math]::Round( ($TotalSize/1024),2))

#Export the data to CSV File
$StorageDetails | sort-object "Total File Size" -descending | Export-csv "$($ListName)_LibraryStroageRpt.csv" -notypeinformation
Write-Host "Storage Report has been Generated!"
 


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Wednesday, October 22, 2014

Copy List Permissions in SharePoint using PowerShell

Ever wanted to clone permissions between SharePoint lists or Libraries? Well, Here is the nifty PowerShell script to copy permissions between SharePoint lists:
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#PowerShell Function to copy permissions between Lists in SharePoint
Function Copy-ListPermissions()
{
 param(
     $WebURL,
     $SourceListName,
     $TargetListName
 )
 #Get the Web
 $Web = Get-SPweb $WebURL

 #Get Source and Target Lists
 $SourceList = $Web.lists[$SourceListName]
 $TargetList = $Web.lists[$TargetListName]

 #if permissions are Inherited in Source, apply it in Target list 
  if($SourceList.Permissions.Inherited)
   {
      $TargetList.ResetRoleInheritance()
   }
  else #Copy permissions from Source to Target List
  {
     #Reset the Inheritence in Target List
     $TargetList.BreakRoleInheritance($false)

     #Copy Source list permissions to Destination List
     $SourceList.RoleAssignments | foreach-object {
     $targetList.RoleAssignments.Add($_)
      }
  }
 $TargetList.Update()
}
#Call the function to copy list permissions 
Copy-ListPermissions "http://sharepoint.crescent.com/sites/operations/us" "Documents" "Invoice"
This script copies all users and groups from the source list to target list. Any custom permissions already applied on the target list will be lost!

The above method can be used to copy permissions between SharePoint list items and sites.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Tuesday, September 16, 2014

Determine the Installed SharePoint Edition: Standard or Enterprise

Here is how to check whether standard or enterprise edition is installed in SharePoint 2013.

Go to Central Admin >> Upgrade and Migration >> Click on "Enable Enterprise Features"
Determine the Installed SharePoint Edition: Standard or Enterprise

This procedure stays same to check standard or enterprise SharePoint 2010 and SharePoint 2007:
  • When the “Enterprise” is selected and grayed-out, it’s an Enterprise edition of SharePoint!
  •  Here is the Standard edition screen: If, Standard option is enabled, then we can determine its a Standard edition.

SharePoint 2013 standard to enterprise upgrade:
You can use this page in SharePoint to upgrade standard to enterprise. But you can't downgrade SharePoint enterprise to standard.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Saturday, September 13, 2014

How to Disable Sync button in SharePoint 2013

Sync option in SharePoint 2013 enables users to keep copies in sync between SharePoint libraries and user's local disk. Once Synched, You get the flexibility to edit documents straight from your local machine.

On clicking the "Sync" button from any document library, triggers the client application "SharePoint workspace" - which is a successor of Microsoft Groove.
How to Disable Sync button in SharePoint 2013
In some cases, you may want to disable this option per your company policies. Follow these steps, If you want to disable Sync button in SharePoint 2013:
  • Go to your SharePoint site Settings
  • Click on "Search and Offline Availability" link under "Search" group
  • Choose "No" for "Allow items from this site to be downloaded to offline Clients?". Choosing "Yes" enables the Sync option back.disable sync in SharePoint library
  • Click OK to save your changes.

That's all. This disables Sync option in SharePoint 2013 site level. PowerShell can be utilized to Turn it OFF/ON globally for the entire farm using:
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

Get-SPSite -limit all | get-SPWeb -limit all | Foreach { $_.ExcludeFromOfflineClient=1; 
$_.Update()} 

You can also enable-disable sync for a particular SharePoint library.
  • Go to Library settings 
  • Click Advanced Settings under the General Settings category.
  • Under Offline Client Availability category, select No to prevent users from download the documents.


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


You might also like:

Related Posts Plugin for WordPress, Blogger...