Search This Blog

Sunday, January 10, 2016

How to create sharepoint lookup site columns with csv file upload using powershell

Create the CSV file with the following columns and save with the name as you like it









Write the below code in a notepad and save the name as you like it and save the file with extension File1.ps1


Try
{
$create = Import-Csv -path "CsvFilePath"

"Site columns creating please wait..."

ForEach($row in $create) {
$row.SiteCollectionUrl

$web=Get-SPWeb -Identity $row.SiteCollectionUrl
$webID=$web.ID
Write-Host "WebID:" $webID
$list=$web.Lists[$row.ListName]
Write-Host "ListName:" $list
$list
$listID=$list.ID
Write-Host "ListID:" $listID

$fieldType=$row.FieldType
$fieldDisplayName=$row.FieldName
$fieldInternalName=$row.FieldName
$sitecolGroup =$row.GroupName
$showField =$row.LookupColumn
$fieldDescription = $row.Description
$fieldHidden=$row.IsFieldHidden
$fieldReadonly = $row.IsFieldReadOnly
$fieldRequired = $row.IsFieldRequired
$unlimitedLength = $row.IsUnlimitedLength
#End of configuration

$siteCollection = Get-SPWeb -Identity $row.SiteCollectionUrl
$rootWeb = $siteCollection.RootWeb
#Define lookup site column with a XMLString
$lookupXMLString = '<Field Type="' + $fieldType + '" DisplayName="' + $fieldDisplayName + '" Description="' + $fieldDescription + '"
Required="' + $fieldRequired + '" EnforceUniqueValues="FALSE" WebId="' + $webID + '" ShowField="' + $showField + '" UnlimitedLengthInDocumentLibrary="' + $unlimitedLength + '"
Group="' + $sitecolGroup + '" StaticName="' + $fieldInternalName + '" List="' + $listID + '"
Name="' + $fieldInternalName + '" Hidden="' + $fieldHidden + '" ReadOnly="' + $fieldReadonly + '"></Field>'
#Create site column from XML string
$siteCollection.Fields.AddFieldAsXml($lookupXMLString)
Write-Host "The sitecolumn has been added $fieldDisplayName" -foregroundcolor green
}
"Site columns created Successfully"
}
Catch
{
write-host "Exception Message: $($_.Exception.Message)" -foregroundcolor red
}




No comments:

Post a Comment