Below script will helps us on how to create single lookup and multi lookup fields in SharePoint using PowerShell. please refer my previous posts on below scenarios:
Please correct the syntax as below for the scripts.
e.g: Replace displayname=""+ $DisplayName +"" to DisplayName='"+ $DisplayName +"'
==================================================================
Thanks,
Sasi Kumar Reddy
- Create a new list in SharePoint using powershell?
- Check whether the list exits or not?
- Create a new field in SharePoint list using powershell
- Add items to the list in SharePoint using powershell?
- Create single lookup field in SharePoint using powershell?
- Create multi lookup field in SharePoint using powershell?
Please correct the syntax as below for the scripts.
e.g: Replace displayname=""+ $DisplayName +"" to DisplayName='"+ $DisplayName +"'
==================================================================
$listname = "List Name";
==================================================================
$docSite = new-object Microsoft.SharePoint.SPSite($siteURL)
$docWeb = $docSite.OpenWeb()
==================================================================
function Add-SPLookUpField([string]$listName, [string]$DisplayName,[string]$lookupListName, [string]$Hidden, [string]$Readonly, [string]$showField, [string]$Name, [string]$Required)
{
$OpenList = $docWeb.Lists[$listName]
$lookupListGuid= $docWeb.Lists[$lookupListName]
$lookupXMLString = ""
$OpenList.Fields.AddFieldAsXml($lookupXMLString,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
}
function Add-SPMultiLookUpField([string]$listName, [string]$DisplayName,[string]$lookupListName, [string]$Hidden, [string]$Readonly, [string]$showField, [string]$Name, [string]$Required)
{
$OpenList = $docWeb.Lists[$listName]
$lookupListGuid= $docWeb.Lists[$lookupListName]
$lookupXMLString = ""
$OpenList.Fields.AddFieldAsXml($lookupXMLString,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
$fieldname=$OpenList.Fields[$DisplayName]
$fieldname.AllowMultipleValues=$true
$fieldname.update()
}
====================================================================
Write-Host "`n Adding fields to the $listname list `n"
Add-SPLookUpField -listName $listname -Name lstname -lookupListName lookuplistName -DisplayName lookuplstName -Required FALSE -showField Title -Hidden FALSE -Readonly FALSE
Add-SPMultiLookUpField -listName $listname -Name lstname -lookupListName lookuplstName -DisplayName lookuplstName -Required TRUE -showField Title -Hidden FALSE -Readonly FALSE
======================================================================
please let me know if faced any issues..Thanks,
Sasi Kumar Reddy
No comments:
Post a Comment