Sunday, December 17, 2017

Remove List Column using PnP PowerShell

Hi,

There was a requirement of deleting list column using PnP Powershell

For this, I have written the PnP PowerShell script to take column name from xml and web urls from csv as below.

Please make sure to have all the files are in the same folder

//RemoveListColumnInputs.xml
<Inputs>
<ConnectSPOnline UserName="username" Password="password" RemoveColumnName="Phase"></ConnectSPOnline>
</Inputs>

//RemoveListsColumnInputs.csv
SiteCollectionUrl,WebUrl,ListTitle,ListUrl,ContentTypeName,ContentGroup
sitecollectionurl,weburl,listrelativeurl,contenttypename,contenttypegroup

//RemoveListsColumnScript.ps1
############################################################## Logging #########################################
  
$date= Get-Date -format MMddyyyyHHmmss 
start-transcript -path .\Log_$date.doc  

################################################### Get input parameters from XML ###############################  
# Get content from XML file 
[xml]$xmlData = Get-Content ".\RemoveListColumnInputs.xml" 

# ConnectSPOnline node 
[System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline 
$userName=$connectSPOnline.UserName 
$password=$connectSPOnline.Password
$removecolumnname=$connectSPOnline.RemoveColumnName

$weburlinputs= Import-CSV .\RemoveListsColumnInputs.csv

function RemoveListsColumn() 
{  
               
                try
                {
                                $weburlinputs | foreach-object {
                                $WebUrl = $_.WebUrl
                                $ListTitle =$_.ListTitle    
               
                                write-host $WebUrl
                                write-host $ListTitle       
                               
                                #Convert password to secure string        
                                $secureStringPwd = ConvertTo-SecureString $password -AsPlainText -Force         
                               
                                # Get the credentials                                                                                     
                                $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd  
                               
                                write-host -ForegroundColor Green "Connecting to Site Web URL":$webURL
                               
                                Connect-PnPOnline -Url $WebUrl -Credentials $credentials          
                               

                                write-host -ForegroundColor Green $removecolumnname "is being removed from the list":$ListTitle
                               
                                Remove-PnPField -List $ListTitle -Identity $removecolumnname -Force                  
                               
                                write-host -ForegroundColor Green $removecolumnname "has been removed from the list":$ListTitle
                                }
                }
                catch
                {
                write-host $_.Exception.Message;
                }

}
               

#################################################################  Initiate #####################################
function Initiate() 
     write-host -ForegroundColor Green "Initiating the script.................. "  
                 RemoveListsColumn
                Disconnect-PnPOnline 
                 write-host -ForegroundColor Green "Completed!!!!"  
}
#################################################################################################################

Initiate 
Stop-Transcript 
  


Thanks & Regards,

Naga Sudheer M

No comments:

Post a Comment