May 3, 2016

How to do with SharePoint Library using Powershell

Hi,
 
I am working on PowerShell scripts to automate deployment for one of client,
 
Here is post :: How to do with SharePoint Library using Powershell,


# Create Document Library

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

function CreateLibrary
{
    param ($LibName)
    try
    {
        $spListCollection  = $OpenWeb.Lists 
        $spLibrary  = $spListCollection.TryGetList($LibName)

        if($spLibrary -ne $null) {
            Write-Host  -f Yellow "Library $LibraryName already exists in the site"
        }
        else
        {      
            $spListCollection.Add($LibName, "", $listTemplate)
            $spLibrary  = $OpenWeb.GetList($OpenWeb.ServerRelativeUrl+"/"+$LibName)
            Write-Host -f Green "Created Document Library $LibName"
        }
    }
    catch [System.Exception]
    {
 write-host -f red "Not set Likes Setting on " $_.Exception.ToString()
    }
}

$SiteURL = Read-Host -Prompt 'Enter Root Site URL and / @ end'
$OpenWeb = Get-SPWeb $SiteURL
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
CreateLibrary -LibName "Department"

#enable Content Types

$spLibrary.ContentTypesEnabled = $true
$spLibrary.Update()

#Add Content Type

$ctype = "Report Data Source"
$ctToAdd = $rootSite.ContentTypes[$ctype]
$ct = $spLibrary.ContentTypes.Add($ctToAdd)

#Enable Versioning

$spLibrary.EnableVersioning = $true
$spLibrary.EnableMinorVersions = $false
$spLibrary.Update();

# Enable Versioning with Limit

$spLibrary.EnableVersioning = $true
$spLibrary.EnableMinorVersions = $false
$spLibrary.MajorVersionLimit = 3
$spLibrary.Update();

#Order Columns

$FieldOrder = New-Object System.Collections.Specialized.StringCollection
$FieldOrder.Add("ContentType")
$FieldOrder.Add("FileLeafRef")
$FieldOrder.Add("Title")
$FieldOrder.Add("CategoryDescription")
foreach ($ctype in $spLibrary.ContentTypes)
{
      if($ctype.Name -ne "Folder")
      {
           $ctype.FieldLinks.Reorder($FieldOrder);
           $ctype.Update($false);
     }
}

#Enable Moderation and Draft Version

$spLibrary.EnableModeration = $true
$spLibrary.DraftVersionVisibility = 2
$spLibrary.Update();

# Enable Rating Settings

$assembly=[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
$reputationHelper =$assembly.GetType("Microsoft.SharePoint.Portal.ReputationHelper");
$bindings = @("EnableReputation", "NonPublic", "Static");
[System.Reflection.BindingFlags]$flags = [System.Reflection.BindingFlags]::Static -bor [System.Reflection.BindingFlags]::NonPublic;
$methodInfo = $reputationHelper.GetMethod("EnableReputation", $flags);
$values = @($spLibrary, "Likes", $false);
$methodInfo.Invoke($null, @($values));

# Create View

$viewTitle = "Dashboard_User" #Title property
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add("DocIcon") > $null
$viewFields.Add("LinkFilename") > $null
$viewFields.Add("CategoryDescription") > $null
$viewFields.Add("BIKeywords") > $null
$viewFields.Add("LikesCount") > $null
$viewQuery = ""
$viewRowLimit = 5
$viewPaged = $true
$viewDefaultView = $false

$newview = $spLibrary.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
 

No comments: