Exports Site Columns Using Powershell

Hi

Here is I am  Explain you  how to export site column using Power shell

Open your notpade++  and copy past below code inthe file save file as .ps1

in this powerhsell code i have generate log file you can notice it from code snippet

just give Export location as per your convince

I assume that you are aware of how to run power shell script in SharePoint managment Shell

pass webUrl as parameter

Remarks-Change groups name with your Group name which you want toin Code


#Necessary Parameters
#Necessary Parameters
[CmdletBinding()]
param 
(
	[string] $Web
)
#/// <summary>
#/// Remove Default Group Permission
#/// </summary>
Function ExportSiteColumnXml([string] $web)
{ 
    try
	{  
      		if($web -ne "")
			{  
              
			    $sourceWeb = Get-SPWeb $web
				$xmlFilePath = "C:\Script-SiteColumns.xml"

				#Create Export Files
				New-Item $xmlFilePath -type file -force

				#Export Site Columns to XML file
				Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
				Add-Content $xmlFilePath "`n<Fields>"
				$sourceWeb.Fields | ForEach-Object {
					if ($_.Group -eq "Your Site Columns Group Name") {
						Add-Content $xmlFilePath $_.SchemaXml
					}
				}
				Add-Content $xmlFilePath "</Fields>"	                            
             }
             else
             {
                 Write-Host "Please provide proper parameters" -ForegroundColor Red
				LogInfo "Please provide proper parameters"
             }
    }
    catch
    {
      Write-Host "Something went wrong. Please check Logs for additional information" -ForegroundColor Red
        LogError $_

    }
    finally
    { 
        #Dispose your site when process end Successivly
		 if($sourceWeb -ne $null)
			{
				$sourceWeb.Dispose();
			}
			LogInfo "Process Ended"
    }
  

}

#/// <summary>
#/// Prints errors if generated
#/// </summary>
Function LogError($message)
{
     $timeStamp = Get-Date -format "dd/MM/yyyy HH:mm:ss"
     $outContent = "$timeStamp`tError`t`t$message`n"
     Add-Content $Logfile $outContent
}

#/// <summary>
#/// Prints logs in log file
#/// </summary>
Function LogInfo($message)
{
     $timeStamp = Get-Date -format "dd/MM/yyyy HH:mm:ss"
     $outContent = "$timeStamp`tInfo`t`t$message`n"
     Add-Content $Logfile $outContent
}




#/// <summary>
#/// Load the SharePoint Snapin 
#/// </summary>
#/// <returns>
#/// </returns>
Function Load-SharePoint-Powershell
{ 
    Write-Host "TEST"
	if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
	{    
	      Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
          
	}
     
}

$Logfile = "c:\RTED\RTED-$(get-date -f MMddyyyy_HHmmss).log";
Load-SharePoint-Powershell
ExportSiteColumnXml $web


&nbsp;
Advertisements

3 thoughts on “Exports Site Columns Using Powershell

Leave a Reply to Piyush55 Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s