March 29, 2016

SharePoint Site Column Using Powershell

Hi,
 
I am working on PowerShell scripts to automate deployment for one of client,
 
Here is first post :: Create Site Columns using PowerShell,
 

# Add SharePoint Snapin to PowerShell          
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {          
  Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
 
#Create Multi Select Lookup Column
function CreateCategory
{
    param ($sSiteCollectionUrl)  
    try
    {
        $web = Get-SPWeb $SiteURL
      
        #Configuration
        $lookupFieldWebGuid=$web.ID
        $lookupList = $web.Lists.TryGetList("CategoryMaster")
        $lookupListGuid = $lookupList.ID
        $fieldDisplayName="Category"
        $fieldInternalName="BICategory"
        $sitecolGroup = "OwnColumns"
        $showField = "Title"
        $fieldDescription = "'Defines content for users' search functionality. Select multiple options, if necessary.'"
        $fieldHidden="FALSE"
        $fieldReadonly = "FALSE"
        $fieldMulti = "TRUE"
        $fieldRequired = "TRUE"
        $unlimitedLength = "FALSE"
        #End of configuration
 
        $lookupXMLString = '
        Required="' + $fieldRequired + '" EnforceUniqueValues="FALSE" WebId="' + $lookupFieldWebGuid + '" ShowField="' + $showField + '" UnlimitedLengthInDocumentLibrary="' + $unlimitedLength + '"
        Group="' + $sitecolGroup + '" Mult="' + $fieldMulti + '" Sortable="FALSE" StaticName="' + $fieldInternalName + '" List="' + $lookupListGuid + '"
        Name="' + $fieldInternalName + '" Hidden="' + $fieldHidden + '" ReadOnly="' + $fieldReadonly + '">
' 
        #Create site column from XML string
        $web.Fields.AddFieldAsXml($lookupXMLString)
        Write-Host "The Category site column has been added." -foregroundcolor green
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}
 
#Create Date Column
function CreateDate
{
    param ($sSiteCollectionUrl)  
    try
    {
        $web = Get-SPWeb $SiteURL 
    
        $fieldXML = '
        Name="Report_x0020_Date"
        Description="Report Publish Date"
        DisplayName="Report Date"
        StaticName="Report Date"
        Format="DateOnly"
        Group="OwnColumns"
        Hidden="FALSE"
        Required="FALSE" 
        Sealed="FALSE"
        ShowInDisplayForm="TRUE"
        ShowInEditForm="TRUE"
        ShowInListSettings="TRUE"
        ShowInNewForm="TRUE">
'  
        #Create site column from XML string
        $web.Fields.AddFieldAsXml($fieldXML)  
       
        write-Host "Added the field Report Date to the Site" -foregroundcolor green           
           
        #Disposing SPSite and SPWeb objects
        $web.Dispose()  
        
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}
 
#Create Choice Column
function CreateFrequency
{
    param ($sSiteCollectionUrl,$sFieldDisplayName, $sFieldInternalName, $sFieldType)  
    try
    {
        $spSite = Get-SPSite -Identity $sSiteCollectionUrl
         $spWeb = $spSite.OpenWeb()   
 
    
        #We check the field type is not null
        if($sFieldType -ne '')
        {
            $spWeb.Fields.Add($sFieldInternalName,$sFieldType,$false)
            $spChoiceField=$spWeb.Fields.GetField($sFieldInternalName)
            write-Host "Added the field Frequency to the Site" -foregroundcolor green
            $spChoiceField.Group="OwnColumns"
            $spChoiceField.Choices.Add("Weekly")
            $spChoiceField.Choices.Add("Months")
            $spChoiceField.Choices.Add("Annually")
            $spChoiceField.Choices.Add("Adhoc")
            $spChoiceField.Title=$sFieldDisplayName
            $spChoiceField.DefaultValue=""
            $spChoiceField.Required = "TRUE"
            $spChoiceField.FillInChoice=$false
            $spChoiceField.Update()
        }
                     
        #Disposing SPSite and SPWeb objects
        $spWeb.Dispose()
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}

#Create User Column
function CreateOwner
{
    param ($sSiteCollectionUrl)  
    try
    {
        $web = Get-SPWeb $SiteURL 
    
        $fieldXML = '
        Name="BIOwner"
        Description="Main business owner of published content."
        DisplayName="Owner"
        List="UserInfo"
        ShowField="ImnName"
        UserSelectionMode="PeopleOnly"
        UserSelectionScope="0"
        StaticName="BIOwner"
        Group="OwnColumns"
        Hidden="FALSE"
        Required="TRUE"
        Sealed="FALSE"
        ShowInDisplayForm="TRUE"
        ShowInEditForm="TRUE"
        ShowInListSettings="TRUE"
        ShowInNewForm="TRUE">
' 
        #Create site column from XML string
        $web.Fields.AddFieldAsXml($fieldXML)  
       
        write-Host "Added the field Owner to the Site" -foregroundcolor green           
           
        #Disposing SPSite and SPWeb objects
        $web.Dispose()  
        
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}

# get Site URL

$SiteURL = Read-Host -Prompt 'Enter Root Site URL and / @ end'
$rootWeb = Get-SPWeb $SiteURL
CreateCategory -sSiteCollectionUrl $SiteURL
CreateDate -sSiteCollectionUrl $SiteURL
CreateOwner -sSiteCollectionUrl $SiteURL
CreateFrequency -sSiteCollectionUrl $SiteURL -sFieldDisplayName "Frequency"  -sFieldInternalName "Frequency" -sFieldType "Choice"

Highlighted in blue color can be change as per your requirement.

Thanks

No comments: