Pages

Friday 13 September 2013

Copying Files and Folders from File System (your local machine) to SharePoint 2013 Document Library Using PowerShell.

 Hi All,

Again got a chance to play around Power Shell.
My requirement was to copy Files and Folders(including files) to a SharePoint Document Library.

Snippet File Name : CopyShellScript.ps1
Location of Snippet in C: (C drive )

Parameters:
1.  webUrl - give your web url here
2. docLibraryName - Name of Document Library (e.g. Shared Documents)
3. docLibraryUrlName - URL name of Document Library(e.g. Shared%20Documents)
4. localFolderDataPath - Location of the folder from where you want to copy(local machine path.)


Script Execution:

PS C:\> .\CopyShellScript.ps1 -webUrl "http://yourURL.local" -docLibraryName "TargetLib" -docLibraryUrlName "TargetLib" -localFolderDataPath "C:\temp"


Below is the complete script. Do work around on $localFolderPath if you want to go for Nth level depth. Iam showing here till nesting of folder level 1.


Param(
  [string]$webUrl,
  [string]$docLibraryName,
   [string]$docLibraryUrlName,
  [string]$localFolderDataPath
)
  Add-PSSnapin Microsoft.SharePoint.PowerShell
#Script settings

#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderDataPath)).GetFiles()
copyfileInFolder $files

$Items = Get-ChildItem -Path $localFolderDataPath

# enumerate the items array

foreach ($item in $Items)

{
      # if the item is a directory, then process it.

      if ($item.Attributes -eq "Directory")

      {
          $create_folder = $docLibrary.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$item)
          $create_folder.Update();
          Write-Host $item.Name "is created"

          $localFolderPath = $localFolderDataPath+"\"+$item.Name
          $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

          copyfileInFolder $files $item

     }
}


function copyfileInFolder
{
$files=$args[0];
$item=$args[1];
   ForEach($file in $files)
  {

    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName+"/"+$item.Name)
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)

    #Close file stream
    $fileStream.Close();
  }
}

#Dispose web

$web.Dispose()

Thursday 12 September 2013

Backup and Restore SharePoint 2013 Farm using Power Shell

Hi All,


For taking backup for you SharePoint farm you can flow below URLs from MS.





Example for taking backup:
Backup-SPFarm -Directory C:\Backup -BackupMethod full -BackupThreads 10 -Force
make sure backup folder should exist before executing the command.

Example for Restoring backup:
Restore-SPFarm -Directory C:\Backup -BackupId 12345678-90ab-cdef-1234-567890abcdef -RestoreMethod overwrite -RestoreThreads 10 -Force
BackupID

In above command replace "12345678-90ab-cdef-1234-567890abcdef" with your backup id.
The type must be a valid GUID, in the form 12345678-90ab-cdef-1234-567890abcdef.
It Specifies the GUID of the backup package that is to be restored. Each backup package has a unique GUID associated with it. The GUID can be seen by using the Get-SPBackupHistory cmdlet to view the backup history. If this parameter is not specified, the most recent backup package in the path that is specified with the Directory parameter is used.
or check for XML created in backup folder.

Refer below links :
Backup-SPFarm

http://technet.microsoft.com/en-us/library/ff607881.aspx