Thursday, June 30, 2016

PowerShell to do CheckIn and publish all items in list/library in SharePoint Online

#Change the highlighted parameters

# Clear the screen
Clear-Host

# Add Wave16 references to SharePoint client assemblies - required for CSOM
#Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
#Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

# Parameters
# Specify the subsite URL where the list/library resides

$SiteUrl = "https://SP.sharepoint.com/sites/Test"

# Title of the List/Library
$ListName = "Test"

# Username with sufficient publish/approve permissions
$UserName = "contoso@abc.com"

# User will be prompted for password

# Set Transcript file name
$Now = Get-date -UFormat %Y%m%d_%H%M%S
$File = "PublishFilesSPO_$Now.txt"
#Start Transcript
Start-Transcript -path $File | out-null

# Display the data to the user
Write-Host "/// Values entered for use in script ///" -foregroundcolor cyan
Write-Host "Site: " -foregroundcolor white -nonewline; Write-Host $SiteUrl -foregroundcolor green
Write-Host "List name: " -foregroundcolor white -nonewline; Write-Host $ListName -foregroundcolor green
Write-Host "Useraccount: " -foregroundcolor white -nonewline; Write-Host $UserName -foregroundcolor green
# Prompt User for Password
$SecurePassword = Read-Host -Prompt "Password" -AsSecureString
Write-Host "All files in " -foregroundcolor white -nonewline; Write-Host $ListName -foregroundcolor green -nonewline; Write-Host " on site " -foregroundcolor white -nonewline; Write-Host $SiteUrl -foregroundcolor green -nonewline; Write-Host " will be published by UserName "  -foregroundcolor white -nonewline; Write-Host $UserName  -foregroundcolor green
Write-Host " "

# Prompt to confirm
Write-Host "Are these values correct? (Y/N) " -foregroundcolor yellow -nonewline; $confirmation = Read-Host

# Run script when user confirms
 if ($confirmation -eq 'y') {

# Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$Context.Credentials = $credentials

# Bind to list
$list = $Context.Web.Lists.GetByTitle($ListName)
# Query for All Items
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = " "
$collListItem = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Context.Load($List)
$Context.Load($collListItem)
$Context.ExecuteQuery()

# Go through process for all items
foreach ($ListItem in $collListItem){
# Adding spacer
 Write-Host " "
 Write-Host "/////////////////////////////////////////////////////////////"
 Write-Host " "
# Write the Item ID, the FileName and the Modified date for each items which is will be published
 Write-Host "Working on file: " -foregroundcolor yellow -nonewline; Write-Host $ListItem.Id, $ListItem["FileLeafRef"], $ListItem["Modified"]

# Un-comment below "if" when you want to add a filter which files will be published
# Fill out the details which files should be skipped. Example will skip all files which where modifed last < 31-jan-2015
#
# if (
# $ListItem["Modified"] -lt "01/31/2015 00:00:00 AM"){
# Write-Host "This item was last modified before January 31st 2015" -foregroundcolor red
# Write-Host "Skip file" -foregroundcolor red
# continue
# }

# Check if file is checked out by checking if the "CheckedOut By" column does not equal empty
if ($ListItem["CheckoutUser"] -ne $null){
# Item is not checked out, Check in process is applied
    Write-Host "File: " $ListItem["FileLeafRef"] "is checked out." -ForegroundColor Cyan
    $listItem.File.CheckIn("Auto check-in by PowerShell script", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
    Write-Host "- File Checked in" -ForegroundColor Green
}
# Publishing the file
Write-Host "Publishing file:" $ListItem["FileLeafRef"] -ForegroundColor Cyan
$listItem.File.Publish("Auto publish by PowerShell script")
Write-Host "- File Published" -ForegroundColor Green

# Check if the file is approved by checking if the "Approval status" column does not equal "0" (= Approved)
if ($List.EnableModeration -eq $true){
# if Content Approval is enabled, the file will be approved
if ($ListItem["_ModerationStatus"] -ne '0'){
# File is not approved, approval process is applied
    Write-Host "File:" $ListItem["FileLeafRef"] "needs approval" -ForegroundColor Cyan
    $listItem.File.Approve("Auto approval by PowerShell script")
    Write-Host "- File Approved" -ForegroundColor Green
}
else {
Write-Host "- File has already been Approved" -ForegroundColor Green
}
}
$Context.Load($listItem)
$Context.ExecuteQuery()
}
# Adding footer
 Write-Host " "
 Write-Host "/////////////////////////////////////////////////////////////"
 Write-Host " "
 Write-Host "Script is done" -ForegroundColor Green
 Write-Host "Files have been published/approved" -ForegroundColor Green
 Write-Host "Thank you for using samarth aaryan's powershell script" -foregroundcolor cyan
 Write-Host " "
 }
# Stop script when user doesn't confirm
else {
 Write-Host " "
 Write-Host "Script cancelled by user" -foregroundcolor red
 Write-Host " "
 }
 Stop-Transcript | out-null

Wednesday, June 22, 2016

Powershell to Export data of Site Collection or Sub Site in a one Go from SharePoint Onprem.

Powershell to export all data of onprem sharePoint (SP2010/2013) site or site collection in one go.


Run that below Powershell in SharePoint Management studio on SP Onprem server(SP 2010/2013)

Note -
Make sure having sufficient space in C drive,exported data resides  in C:\temp
If not having sufficient space in C drive,You can change the location for exported data.

-------------------------------------------------------------------------------------------------------------------

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"


$OutputFileLocation = "C:\temp\DocLib.csv"
Start-Transcript -path $OutputFileLocation -append
 $LogFilePath = "C:\temp\ErrorLog.log"
 $DATE = get-date


 Function exportDocLib($siteurl){
#Get site collection details
#$s = Get-SPSite http://stockxchange.stockland.com.au
#Get all web sites in the site collections
#$wc = $siteurl.AllWebs | ?{$_.url -like "*" + $subsiteName + "*"}
$wc = $siteurl.AllWebs
try{
foreach($web in $wc)
{
#loopthrouh the lists and libraries in the site
#Write-Host "Iterating Site " $web.Url
foreach($list in $web.Lists)
  {
        #Check for only document libraries
        #if($list.BaseTemplate -eq "DocumentLibrary")
        #{      
            Write-Host $list.Title"(Web: "$web.Title")--(webname:"$web.Name")"           
            $srcpath=$web.Url+"/"
            Write-Host "source path is :" $srcpath
            Write-Host 'Checking InternalName is:'     $list.Url
            $path = $list.Url.Substring(36)       
            Write-Host "library path is: " $path
            $itempath=$path+"/"
            Write-Host "New itempath is ::::::"$itempath          
            $folderpath=$path.replace('/','\')
            $filepath="C:\temp"+$folderpath
            Write-Host "Export Location filepath is:"  $filepath
          
            Export-SPWeb -Identity $srcpath -ItemUrl $itempath -Path $filepath -NoFileCompression -IncludeVersions "All"      
      
        #}  
    }
}
}
catch
    {
            Write-Host "Error occured while Exporting List and Libraries. See log file for more details."
            $format= "==================================================================================================="  
            $format | out-file $LogFilePath -append
            $Date | out-file $LogFilePath -append
            "Error occured while Exporting List and Libraries. See log file for more details." | out-file $LogFilePath -append
            "Site Collection URL: "+$siteurl |out-file $LogFilePath -append    
            "List Title: "+$list.Title | out-file $LogFilePath -append
            $("Error caught: " + $_.Exception.GetType().FullName) | out-file $LogFilePath -append
            $("Error caught: " + $_.Exception.Message) | out-file $LogFilePath -append
            $format | out-file $LogFilePath -append
    }

 }




 Function export($web){
try{
    foreach($list in $web.Lists){
        $format= "==================================================================================================="  
        $format | out-file $LogFilePath -append
        $Date | out-file $LogFilePath -append

        Write-Host "ListName:::" $list.Title
        $srcpath=$web.Url+"/"
        Write-Host "sourcepath is ::::::" $srcpath
        Write-Host 'Checking InternalName is: '     $list.Url
        $path = $list.Url.Substring(36)       
        Write-Host "library path is:" $path
        $itempath=$path+"/"
        Write-Host "New itempath is ::::::"$itempath
      
        $folderpath=$path.replace('/','\')
        $filepath="C:\temp"+$folderpath
        Write-Host "Export Location filepath is:"  $filepath      
        Export-SPWeb -Identity $srcpath -ItemUrl $itempath -Path $filepath -NoFileCompression -IncludeVersions "All"
  
    }
}catch{
        Write-Host "Error occured while Exporting List and Libraries. See log file for more details."
        $format= "==================================================================================================="  
        $format | out-file $LogFilePath -append
        $Date | out-file $LogFilePath -append
        "Error occured while Exporting List and Libraries. See log file for more details." | out-file $LogFilePath -append
        "Site URL: "+$web |out-file $LogFilePath -append    
        "List Title: "+$list.Title | out-file $LogFilePath -append
        $("Error caught: " + $_.Exception.GetType().FullName) | out-file $LogFilePath -append
        $("Error caught: " + $_.Exception.Message) | out-file $LogFilePath -append
        $format | out-file $LogFilePath -append
    }
 }



 $inputsiteurl=  Read-Host -Prompt 'Input your sitecollection or site url here'
 $siteurl=Get-SPSite  $inputsiteurl  -ErrorAction SilentlyContinue -ErrorVariable err

 if($siteurl -eq $Null)
{  
    $weburl=Get-SPWeb  $inputsiteurl
    Write-Host "spweb is" $weburl
    export $weburl
}
else
{
    Write-Host "spsite is" $siteurl
    exportDocLib $siteurl
}

 #$subsiteName = Read-Host - Prompt 'Input Sub Site URL'

Stop-Transcript

Tuesday, June 21, 2016

Running Scripts Is Disabled On This System

Error -PowerShell Script Cannot Be Loaded Because Running Scripts Is Disabled On This System




Then run the following command in online management shell:

set-executionpolicy remotesigned


Then type “Y” for yes and press enter

Monday, June 20, 2016

SharePoint Migration to Online (SPO-O365) from On-Premise SharePoint using Azure API as well as Powershell command.

Introduction

The step which is involved in Migrating to SharePoint Online (O365–SPO) from SharePoint Onprem or file share using PowerShell migration Azure APIs.
This involves the script which is helpful in creating SPO Migration Package from SharePoint Onprem Content using PowerShell Commands i.e. exporting the lists, document libraries, form libraries etc. of a site collection or a site.

Business case

Azure APIs are provided by Microsoft. These APIs can be used to save the overall licensing cost of migration tool. Effective Time utilization as well as Work fragmentation by using this API.

Targeted Audience

  • SharePoint Application Developers
  • SharePoint Administrator
  • SharePoint Architect

Technologies used

  • SharePoint onprem (SP2010/SP2013/MOSS-2007)
  • SharePoint Online (Office 365)
  • Online management PowerShell
  • Azure storage, blobs & containers
  • Require as access to Azure Management tool for creation of the Azure Blob’s

Pre-requisites

  • RDP to a WFE for SharePoint Onprem Server
  • Valid Office 365 subscription
  • Valid Microsoft Azure subscription
  • SharePoint Online management shell version 16.0.4017.1200 or above version. Download here More details at https://technet.microsoft.com/en-us/library/fp161372.aspx
  • windows PowerShell 3.0 and above version
  • SharePoint Admin Tenant account access is required.

Migration Steps for SharePoint Online (O365–SPO) from SharePoint Onprem


To keep things simple, let’s divide the entire process in few steps:
For migrating the content from SharePoint On-Premise to SharePoint Online includes five steps as below.
1. Create SPO Migration Package from SharePoint On-Prem Content.
2. Prepare Final SPO Migration Package.
3. Upload SPO Migration Package to Azure Blob Containers.
       4. Submit Migration Job.
       5. Check Migration Job Status (Optional).

Steps for SharePoint Online (O365–SPO) from SharePoint Onprem

Step 1 – Create SPO Migration Package

  1. Create SPO Migration Package from SharePoint On-Premise Content
  2. RDP to a WFE for On-Premise SharePoint Server
  3. Open SharePoint Management Shell as administrator
  4. Export the Web / Site Collection data using the below cmdlet
Export-SPWeb -Identity "http ://<On-PremSPSite>" -ItemUrl "/OnPremDocLib" -Path "C:

\SPOnPremExport" -NoFileCompression -IncludeVersions All

Example -
Export-SPWeb -Identity "http://stockxchange.stockland.com.au/OnlineForms" -ItemUrl /OnlineForms/Documents –Path "C:\Documents" -NoFileCompression -IncludeVersions All




                                                                Fig 1
Note1:
  • Make sure the drive have sufficient space to Export the data.

Note2:
  • In SPWeb put SharePoint 2010 site collection or subsite Url.
  • Item Url parameter has to be a relative path i.e. if absolute URL is http://SharePoint.avande.com/DocLib then –Item Url will be just “/ DocLib”
  • No File Compression is specified so output will be created in a folder and hence -Path is given without .cmp
  • At this time, SPO migration pipe is oriented for list and document library content only. Hence, Item Url is required.


Copy Data: Once you run the above command, it will generate the data files (shown in fig-3) which you need to copy to a folder on your machine which has SharePoint Online Management Shell installed.


                                                                                Fig 2

Step 2 – Prepare SPO Migration Package

Prerequisites:
  • Valid Office 365 subscription
  • Valid Microsoft Azure subscription
  • Install SharePoint Online management shell version 16.0.4017.1200 or above from here. More details at https://technet.microsoft.com/en-us/library/fp161372.aspx
  • Run SharePoint Online management shell as Administrator and connect SPO service using the below cmdlet. The user needs an Admin level access on the Admin Tenant.

Run the below command:-

Connect-SPOService -Url https:\\www.Sharepoint-admin.sharepoint.com



Example-

Connect-SPOService –Url https://sharepoint-admin.sharepoint.com


Prepare data / files to be migrated using below cmdlet

Run the below command:-


ConvertTo-SPOMigrationTargetedPackage -SourceFilesPath "C:\MigrationPackages\SPOTEMP" 
-SourcePackagePath "C:\MigrationPackages\SPOTEMP" 
-OutputPackagePath "C:\MigrationPackages\SPOMigrationPackage" 
-TargetWebUrl "https://<SPOSite>.sharepoint.com/" 
-TargetDocumentLibraryPath "SPOMigration"










  
Example-

ConvertTo-SPOMigrationTargetedPackage -SourceFilesPath "C:\Documents" -SourcePackagePath "C:\Documents" -OutputPackagePath " C:\SPODocuments” -TargetWebUrl "https://sharepoint.sharepoint.com/sites/Test/" -TargetDocumentLibraryPath "Documents"

                                                     
         
                                                             Fig 3

There are six required parameters to enter

  • TargetwebURL: The fully qualified web URL of a web that is the expected target of the import operation. This URL will be used as the web address part of the base URL in the package metadata.
  • SourceFilesPath: The directory location where the package’s source content files exist.
  • SourcePackagePath: The directory location where the package’s source metadata files exist.
  • OutputPackagePath: The directory location where the transformed package metadata files will be saved. If the directory does not exist, then it will be created.
  • TargetDocumentLibraryPath: The web relative document library that is the expected target of the import operation. This URL will be used as the document library part of the URL in the package metadata. The document library must already exist at the specified target web URL as it will be contacted during transformation to determine appropriate values for the package metadata transformation.
  • Credentials: Optional credentials of a site collection administrator to use to connect to the site collection. The credentials should supply the username in UPN format (e.g. user@company.onmicrosoft.com). If this property is not set, the current tenant admin credentials from the session’s previous call to Connect-SPOService will be used to connect to the site collection.



                                                                      Fig-4

Note:-Copy the all .xml files as show in Fig.6 into from source file path to OutputPackagePath




                                                                      Fig-5

Step 3 – Upload SPO Migration Package to Azure Blob Containers

How to create storage account on Azure?

  • Follow the below steps to get Storage Account Name and two container names inside it. If you already have a storage account, you could use the same
  • Login to Azure Portal via https://manage.windowsazure.com or https://portal.azure.com
  • Browse to Storage > Create a new storage account e.g. nO365mig
  • In newly created storage account create two containers e.g. migration-files, migration-package

Prepare to upload the prepared data / files to Azure Blob Containers using below cmdlet

Run the below command:-

$azurelocations = Set-SPOMigrationPackageAzureSource -SourceFilesPath 
"C:\MigrationPackages\SPOTEMP" -SourcePackagePath "C:\MigrationPackages\SPOMigrationPackage
" -FileContainerName migration-files -PackageContainerName migration-package 
-AccountName nO365mig -AccountKey "REAPLCE_WITH_YOUR_AZURE_STORAGE_ACCOUNT_ACCESS_KEY" 
–Overwrite  -AzureQueueName migrationqueue

Example-

$azurelocations = Set-SPOMigrationPackageAzureSource -SourceFilesPath " C:\Documents" -SourcePackagePath "C:\SPODocuments" -FileContainerName migration-files2 -PackageContainerName migration-package2 -AccountName sam365mig -AccountKey " Jo3szIp5qqtDQ1gMuhkpn436i1yJeYkchdjRNIqMxsYEWkhIkX4XRnrmY4+c2HGHlvUAE11L4gcDcYWyAwGyCw==" –Overwrite  -AzureQueueName migrationqueue



                                                               Fig-6

Step 4 – Submit Migration Job on Online Tenant site or subsite

After uploading the files as well as packages to azure now we have to submit the data to our tenant or online site.

Submit Migration Job to transfer data from Source <to> Azure Blob Containers <to> SPO Site Collection using below cmdlet

Run the below command:-

Submit-SPOMigrationJob -TargetWebUrl "https ://<SPOSite>.sharepoint.com/" 
-MigrationPackageAzureLocations $azurelocations


Example-
Submit-SPOMigrationJob -TargetWebUrl "https://sharepoint.sharepoint.com/sites/Test/" -MigrationPackageAzureLocations $azurelocations



                                                                            Fig-7


Step 5 (Optional) – Check Migration Job Status

Check migration job (to transfer data from Azure Blob Containers to SPO Site Collection) status using below cmdlet.

Run the below command:-

Get-SPOMigrationJobStatus -TargetWebUrl https://sharepoint-admin.sharepoint.com


                                                                                 Fig-8

Note
  1. Queue Status: Data been not picked to process. Wait sometime to go under process.
  2. Processing Status: Data will processing from Azure to tenant site.
  3. No Status: If job is successfully completed then it will not show any status.


Showing data has been migrated successfully as below:-



                                                                  Fig-10

Step 6 (Optional) – Remove Migration Job

In case if the migration job is stuck or if it needs to be removed for any reason you could use the below cmdlet (Note: This will work only if the job is not already completed)

Using below command we can easily remove the pending jobs:-


Run the below command:-

Remove-SPOMigrationJob -JobId e79c5022-3aa1-446c-a8e6-2431063706fa -TargetWebUrl 
https://sharepoint.sharepoint.com



Benefits/value adds

  • While migration from SharePoint onprem to SharePoint online i.e.  Office 365 using Azure API is very efficient because it doesn’t require any migration tool having too much costing.
  • No third party tool was used for migration as well as reporting. 
  • Effective Time utilization by using this API as well as Work fragmentation.


Conclusion

I wish your migration has gone very smoothly. If it does not, then feel welcome to contact me via the comments section below. I will try to reply within a short time.