March 29, 2016

Create SharePoint Web Part Page using PowerShell

Hi,
 
I am working on PowerShell scripts to automate deployment for one of client,
 
Here is post :: Create SharePoint Web Part Page using PowerShell,


# Add SharePoint Snapin to PowerShell          
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {          
  Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#function to create web part page in Site Pages
function CreateBlankWebpartPage          
 
{
    Param($WebPartPageName)               
    try
    {    
        $pageLayout = 1  
        $spListCollection  = $OpenWeb.Lists 
        $spLibrary  = $spListCollection.TryGetList("Site Pages")       
        $xml = "" + $spLibrary.ID + "NewWebPageNewWebPartPage" + $pageLayout + "true" + $WebPartPageName + ""
        $OpenWeb.ProcessBatchData($xml)
        foreach($listItem in $spLibrary.Items)
        {
            if($($listitem.URL).Contains("$WebPartPageName" + ".aspx"))
            {
                $myListItem = $listItem
                break;
            }
        }
        $pageUrl =  $($myListItem.URL)
        Write-Host -f Green "New page is Created with Url - $pageUrl"
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()
    }
}
 
 
$SiteURL = Read-Host -Prompt 'Enter Root Site URL and / @ end'
 
$OpenWeb = Get-SPWeb $SiteURL
 
CreateBlankWebpartPage -WebPartPageName "My Favorites"
CreateBlankWebpartPage -WebPartPageName "Pending Approval"
CreateBlankWebpartPage -WebPartPageName "My Tasks"
CreateBlankWebpartPage -WebPartPageName "My Reports"

SharePoint Left Navigation Using PowerShell

Hi,
 
I am working on PowerShell scripts to automate deployment for one of client,
 
Here is post :: Creating SharePoint Left Navigation using PowerShell,

 
First step will be creating XML File as below,
 
<?xml version="1.0" encoding="utf-8"?>
<Navigation>
  <Headings>
    <Heading Title="Heading1" Url="" Description=""  Permission="domain\UserGroup">
      <NavLink Title="HeadNav1" Url"/>
      <NavLink Title="HeadNav2" Url=""/> 
    </Heading>
    <Heading Title="Heading2" Url="" Description="" Permission="domain\UserGroup" >
      <NavLink Title="HeadNav3" Url=""/>
      <NavLink Title="HeadNav4" Url=""/>
    </Heading>
    <Heading Title="Heading3" Url="" Description="" Permission="domain\UserGroup" >
    </Heading>
  </Headings>
</Navigation>
 

Save the xml file, now time for PowerShell script to read the xml and build the left navigation.
 
PowerShell script will first clear the existing Left Navigation and then Create New left Navigation with
 
  1. Heading
  2. NavLink Under Heading
  3. Description for Heading
  4. URL link for Heading and NavLink
  5. Permission (Target Audience) for Heading

# Add SharePoint Snapin to PowerShell          
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {          
  Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

function LeftNavBar{
 
    $qlNav = $OpenWeb.Navigation.QuickLaunch
 
    #Get XML File
    $xmlFilePath = "$PSScriptRoot\.xml"
    $xmlFile = [xml](Get-Content($xmlFilePath))
    $oldWebUrl = $OpenWeb.Url
    $currentLinks = @()
 
    #Clear Quick Launch links
    $qlNav | ForEach-Object {
        $currentLinks = $currentLinks + $_.Id
    }
    $currentLinks | ForEach-Object {
        $currentNode = $OpenWeb.Navigation.GetNodeById($_)
        write-host -f Yellow "Deleting" $currentNode.Title "and all child navigation links..."
        $qlNav.Delete($currentNode)
    }
 
    #Create Quick Launch Links
    $xmlFile.Navigation.Headings.Heading | ForEach-Object {
        $headingNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($_.Title, $_.Url)
        #$headingNode.Properties["Description"] = $_.Description
        #$headingNode.Update();
        write-host -f Green "Creating Heading:" $_.Title
        $heading = $qlNav.AddAsLast($headingNode)
        $heading.Properties["Audience"] = $_.Permission
        $heading.Update();
      
        $_.NavLink | ForEach-Object {
            if($_.Title -ne $null) {
            $linkNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($_.Title, $_.Url)
            write-host -f Green "Creating Navigation Link:" $_.Title
            $link = $heading.Children.AddAsLast($linkNode)
            }
        }
    }
  
 
    $OpenWeb.Dispose()
}


$SiteURL = Read-Host -Prompt 'Enter Root Site URL and / @ end'
 
$OpenWeb = Get-SPWeb $SiteURL

LeftNavBar

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