Monday 13 February 2017

SharePoint: How to Import Site collection and export same to New csv for each SC

SharePoint: How to Import Site collection and export same to New .csv for each Site Collection in details

Today I am writing this article about “SharePoint: Import and export Site Collection details in .CSV (comma-separated values) file” in description how to Get SharePoint Site collection and Sub site details by importing from .CSV (comma-separated values) file and generate new .CSV (comma-separated values) file for each Site collection with all required details.
So let me describe about this task: we need a details for site collection and their subsite details like, site collection URL, Sub Site URL, Content DB name, UID (GID), Size of Content DB etc...
We need all details as above first scenario in simple way by running script, in second scenario we have a List (.CSV (comma-separated values)) of site collection details from their farm of SharePoint where thousands of Site collections are deployed, now we are looking to get all details for each SP site collection and result should be in separate .CSV (comma-separated values) file for each collection. Given site collection URLs are stored in .CSV (comma-separated values) file.
Below are the case note for more details in screen shots:
  • Client input is like below in csv file from different web Application: A .CSV (comma-separated values) file with full of site collection URLs.


  • And the looked-for output should be like below screen shot:
We need PowerShell script command to complete the above requirement:

I am going to perform this task step by step with screen shots and also we will add code shipset as well.
  • In case if you want to perform this on demo environment, Login to server and copy all site collection to the csv file to do the same we need to run Get-SPSite command and copy all site collection.
  • Once .CSV (comma-separated values) file is ready with details we need to write a PowerShell script for the same.
  • Open PowerShell ISE with administrator permission and copy the written code to the same with required modification like Path, Location of File, View which you want to get in csv file etc….
  • We can see in the code clearly mentioned “Clear-Host” in the first only so we will not get other code details on the screen.
  • Moving to the next we need to write code for reading csv file which you copied to .CSV (comma-separated values) file and saved the same location to the code. See below screen shot for more details:
  • Once  this done code for start looping and checking existing site collection details one by one
  • Generate the file ddmmyy format  with ID (it’s your option if you want you can change also as you need)
  • Pass the variable to get all SharePoint site collection details from the csv file
  • Following the same we will get for DB Name, Size, Subsite etc...
  • Once all done we will write code to get exported for each site collection for each csv file you will getting all details from the attached code.
  • And save this file to same location where you have save .CSV (comma-separated values) (not Mandate)
  • Now go to the SharePoint server, make sure you have farm administrator permission to execute this script and copy the same file to the same server drive.

###########################################################################################################
#SharePoint: How to Import Site collection and export same to New .csv for each Site Collection in details#
#Created Details: February 2017                                                                           #
#Created By: Prashant Kumar                                                                                #
###########################################################################################################
#First clear whole screen    
Clear-Host
#Variables
$Path = "C:\Users\test\Desktop\mytest"
$importLocation="C:\Users\test\Desktop\mytest\sss.csv"
$csv=Import-Csv $importLocation
write-host "Reading csv from :" $importLocation;
foreach($row in $csv)
{
  $url = $row.URL;
  write-host "Processing siteurl:" $url;
  $s = Get-SPSite -Identity $url
  $FileLocation = "$($path)\$($s.id)_$(get-date -f yyyyMMdd).csv" 
  $SPWebs = $S | Get-SPWeb -Limit all
  $ContentDB = Get-SPContentDatabase -site $url 
  foreach ($web in $SPWebs){
    write-host "Processing web url:" $url;
  $properties = @{
                                    'Site Collection URL'="$($Url)";
                                    'Sub Site URL'="$($web.Url)";
                                    'ID'="$($web.ID.Guid)";
                                    'ContentDBName'="$($ContentDB.Name)";
                                    'ContentDBSize'="$($ContentDB.DiskSizeRequired /1024/1024)";
                                                }#properties
                    $obj1 = New-Object –TypeName PSObject –Property $properties
                    Write-Output $obj1|Select-Object 'Site Collection URL','Sub Site URL','ID','ContentDBName','ContentDBSize'|Export-Csv -Path $FileLocation -Delimiter "," -Encoding UTF8 -Append -NoTypeInformation -Force

   }
}
  • Open SharePoint Management shell with admin rights and drag and drop the same file to PowerShell Management shell and Navigate to the path, see below:
  • Once file has been copied to the path Hit enter and wait for the result, it will run successfully and result would be like below screen shot:
  • Now Opened any one of the .CSV (comma-separated values) file and verified we are getting expected result


Also we can check the above sub site collections are exist under the given site collection:




Thanks






No comments:

Post a Comment