Thursday, 10 March 2016

PowerShell Script to Get the XML input and set the Managed Metadta of Document library to Default Field values

#Get site object from the input XML
[xml]$siteList = Get-Content C:\Users\svayugun\Desktop\scriptstarbucks\SiteUpdate.xml #Location of Input XML
#Log File
$Logfile = “C:\Users\svayugun\Desktop\scriptstarbucks\DocLibUpdate.log” #Location of Log File Stored
$SummaryLogfile = “C:\Users\svayugun\Desktop\scriptstarbucks\MigrationSummaryLogFile.log” #Location of Migration of Summary File
#Content Type Name
$ctName=”Starbucks Collaboration Document”
#Filed Name of Organization
$fldOrgUnit = “Organizational Unit”
#Filed Name of Brand
$fldBrand = “Brand”
#Fields Name of Country Tag
$fldCountryTag = “Country Tag”
#Field Name Of Data Classificaiton
$fldDataClassification = “Data Classification”
#Field Name of Data Classification
$fldDocClassification = “Document Classification”
#Assign the Termset GUID of Organization from Managed Meta data Service Application
[GUID]$OrgTermsetGuid=”c7c4d78d-75e6-46ba-9033-d860dd221c27″;
#Assign the Termset GUID of Brand from Managed Meta data Service Application
[GUID]$BrandTermsetGuid=”8b74845e-f42d-49b9-9f03-70f383ad14e9″;
#Assign the Termset GUID of Country from Managed Meta data Service Application
[GUID]$CountryTermsetGuid=”2cf96cc6-1c78-40e0-b1da-7badb226072e”;
#Assign the Termset GUID of Document Classificaiton from Managed Meta data Service Application
[GUID]$DocClassificationTermsetGuid=”7014ba9b-72b4-4d07-944f-8a165a970166″;
#Assign the Termset GUID of Audience from Managed Meta data Service Application
[GUID]$AudienceTermsetGuid=”9e1bf3a9-ed94-4f8d-bb41-7d8c0a062f47″;
#Content Type Document Fields Default
$ctDocumentFields=”ContentType”,”SelectFilename”,”FileLeafRef”,”Created”,”Title”,”Modified”,”Modified_x0020_By”,”Created_x0020_By”,”SelectedFlag”
#Default Document Libraries
$defaultDocLibraries=”Site Assets”,”Translation Packages”,”Style Library”,”Site Collection Documents”
#SP Query Object Reference
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = “Scope=’Recursive'”;
$query.RowLimit = 10
#CAML Query to Get the Documents based on ID
$caml = ‘<OrderBy><FieldRef Name=”ID”/></OrderBy>’
#Pass the Query to SPQuery Object
$query.Query = $caml
#Logs Tracking
Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
#Log Summary Tracking
Function LogWriteSummary
{
Param ([string]$logstring)
Add-content $SummaryLogfile -value $logstring
}
Function SetLibrary
{
Param ([Microsoft.Sharepoint.SPDocumentLibrary]$dList)
$logs=”*****Processing List: “+$dList.Title+”**********”
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
Write-Host “Enabling Content Types and Versioning”
#EnableContentTypes and Versioning for the Document Library.
$dList.ContentTypesEnabled = $true
$dList.EnableVersioning= $true
$dList.Update()
#Add site content types to the list
$ctToAdd = $site.RootWeb.ContentTypes[$ctName]
if (($dList.ContentTypes | where { $_.Name -eq $ctName }) -eq $null)
{
$dList.ContentTypes.Add($ctToAdd)
$logs= “list”+ $dList.Title+” updated with Content Type”
$dList.Update()
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
}
else
{
$logs= “Content Type already exists for library:”+$dList.Title
write-host -ForegroundColor green $logs
LogWrite $logs
}
#Add Ste Content type to list
#Adding Fields of Document Content Type to Starbuks Content Type
$docContentType=$dList.ContentTypes[“Document”]
$SbuxContentTypeToUpdate=$dList.ContentTypes[$ctName]
$SbuxContentTypeToUpdate.ReadOnly=$false
foreach ($field in $docContentType.Fields)
{
if($ctDocumentFields -notcontains $field.InternalName)
{
write-host $field.get_InternalName()
write-host $field.Title
write-host $field.InternalName
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$SbuxContentTypeToUpdate.FieldLinks.Add($fieldLink)
}
}
#Adding Fields of Document Content Type to Sbux Content Type
#Set Default Content Type
$currentOrder = $dList.ContentTypes
$result=New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]
foreach ($ContType in $currentOrder)
{
if ($ContType.Name.Contains($ctName))
{
$result.Add($ContType)
}
}
$dList.RootFolder.UniqueContentTypeOrder = $result
$dList.RootFolder.Update()
$SbuxCT = $dList.ContentTypes[$ctName]
$SbuxCTId = $SbuxCT.ID
#Set Defaults for the MMS Fields in the Document Library
$ColumnDefaults = New-Object -TypeName Microsoft.Office.DocumentManagement.MetadataDefaults $dList
$ColumnDefaults.SetFieldDefault($dList.RootFolder, “OrganizationalUnit”, $DefaultValue)
$ColumnDefaults.SetFieldDefault($dList.RootFolder, “Brand”, $DefaultValueBrand)
$ColumnDefaults.SetFieldDefault($dList.RootFolder, “AudienceTag”, $DefaultValueAudience)
$ColumnDefaults.SetFieldDefault($dList.RootFolder, “CountryTag”, $DefaultValueCountry)
$ColumnDefaults.SetFieldDefault($dList.RootFolder, “DocumentClass”, $DefaultValueDocClassification)
$ColumnDefaults.Update()
$logs=”Successfully Updated the Default Value for OrgUnit.”
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
#Set Defaults
#Set Each Item with Values
$listItems = $dList.GetItems()
$TotalItemCount=$listItems.Count
$logs=”Processing the List Items in List:”+$dList.Title + ” ItemCount ” +$TotalItemCount
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
[int]$trackItemUpdated=0
[int]$trackItemCheckedOut=0
[int]$trackItemErrored=0
#Start od Do-While loop to Iterate the Items
do
{
$listItems = $list.GetItems($query)
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
#Loop through each Item
foreach ($item in $listItems)
{
try
{
#Cast to SPListItem to avoid ambiguous overload error
$spItem = [Microsoft.SharePoint.SPListItem]$item;
if(($spItem.File.CheckOutStatus -eq “None”)-or ($spItem.ContentType.Name -eq $ct))
{
#Get the taxonomy field for the list item and set the value
$taxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$fldOrgUnit]
$taxField.SetFieldValue([Microsoft.SharePoint.SPListItem]$spItem,[Microsoft.SharePoint.Taxonomy.Term]$term)
#Get the taxonomy field for the list item and set the value
$taxFieldBrand = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$fldBrand]
$taxFieldBrand.SetFieldValue([Microsoft.SharePoint.SPListItem]$spItem,[Microsoft.SharePoint.Taxonomy.Term]$termBrand)
#Get the taxonomy field for the list item and set the value
$taxFieldAudience = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$fldDataClassification]
$taxFieldAudience.SetFieldValue([Microsoft.SharePoint.SPListItem]$spItem,[Microsoft.SharePoint.Taxonomy.Term]$termAudience)
#Get the taxonomy field for the list item and set the value
$taxFieldCountry = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$fldCountryTag]
$taxFieldCountry.SetFieldValue([Microsoft.SharePoint.SPListItem]$spItem,[Microsoft.SharePoint.Taxonomy.Term]$termCountry)
#Get the taxonomy field for the list item and set the value
$taxFieldDocClassification = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spItem.Fields[$fldDocClassification]
$taxFieldDocClassification.SetFieldValue([Microsoft.SharePoint.SPListItem]$spItem,[Microsoft.SharePoint.Taxonomy.Term]$termDocClassification)
$spItem[“ContentTypeId”]=$SbuxCTId
$spItem.SystemUpdate($false)
$trackItemUpdated=$trackItemUpdated+1;
$logs=”Successfully Updated Item in List:”+$dList.Title + ” Item: ” +$spItem.Name
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
}
else
{
#Track the Checked out Items
$trackItemCheckedOut=$trackItemCheckedOut+1;
$logs=”Item is Checked out in List:”+$dList.Title + ” Item: ” +$spItem.Name
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
}
}
catch
{
$trackItemErrored=$trackItemErrored+1
$logs=”Errror Occured in List:”+$dList.Title + ” Item: ” +$item.Name
LogWrite $logs
Write-Host -ForegroundColor Red $logs
}
} #End of List Item Loop
}#end of Do While loop
#Log the Checked out files and Checked in Files
while ($query.ListItemCollectionPosition -ne $null)
$StartLogging=”**************************************”
LogWriteSummary $StartLogging
$logs=”Total List Items in List:”+$dList.Title + ” ItemCount ” +$TotalItemCount
LogWrite $logs
LogWriteSummary $logs
Write-Host -ForegroundColor Green $logs
$logs=”Successfully Updated the List Items in List:”+$dList.Title + ” ItemCount ” +$trackItemUpdated
LogWrite $logs
LogWriteSummary $logs
Write-Host -ForegroundColor Green $logs
$logs=”Number of Checkedout DOcument in List:”+$dList.Title + ” ItemCount ” +$trackItemCheckedOut
LogWrite $logs
LogWriteSummary $logs
Write-Host -ForegroundColor Green $logs
$logs=”Number of Documents with error in List:”+$dList.Title + ” ItemCount ” +$trackItemErrored
LogWrite $logs
LogWriteSummary $logs
Write-Host -ForegroundColor Green $logs
}
#Loop Through Each Site – Fetch the Data from XML file.
foreach( $siteUrl in $siteList.Sites.Site)
{
#Fetch the Data from XML file.
$sSiteCollUrl=$siteUrl.SiteCollUrl
$sSiteUrl=$siteUrl.SiteUrl
$documentLibraryName=$siteUrl.DocumentLibraryName
[GUID]$OrgUnitTermguid = $siteUrl.OrgUnitTermGuid
[GUID]$BrandTermGuid = $siteUrl.BrandTermGuid
[GUID]$AudienceTermGuid= $siteUrl.AudienceTermGuid
[GUID]$DocClassificationTermGuid = $siteUrl.DocClassificationTermGuid
[GUID]$CountryTermGuid = $siteUrl.CountryTermGuid
#log the Updates
LogWrite “***************Started updates for Site******************* ”
$logs=”SiteUrl is”+$sSiteUrl
LogWrite $logs
Write-Host -ForegroundColor Cyan $logs
# Get the SPSiteObject
$site = Get-SPSite $sSiteCollUrl
}
#Get reference to the Taxonomy session
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($sSiteCollUrl)
#Get the Name of Managed Service Application from Input XML
$termstore = $session.TermStores[$SiteUrl.ManagedMetadataServiceApplName]
#Get Groups
$groupOrgUnit = $termstore.Groups| Where-Object {$_.Name -eq “Organizational Unit”}
Write-Host $groupOrgUnit.Name
$groupBrand = $termstore.Groups[“Product”]
$groupAudience = $termstore.Groups[“Business Process”]
$groupCountry = $termstore.Groups[“Geography”]
$groupDocClassification = $termstore.Groups[“Document Classification”]
#Get TermSets
[Microsoft.SharePoint.Taxonomy.TermSetItem] $termset = $groupOrgUnit.TermSets[$OrgTermsetGuid]
#Write-Host $termset.Name
#Get Product Brand TermSet
[Microsoft.SharePoint.Taxonomy.TermSetItem] $termsetBrand = $groupBrand.TermSets[$BrandTermsetGuid]
#Write-Host $termsetBrand.Name
#Get Product Brand TermSet
[Microsoft.SharePoint.Taxonomy.TermSetItem] $termsetAudience = $groupAudience.TermSets[$AudienceTermsetGuid]
#Write-Host $termsetAudience.Name
[Microsoft.SharePoint.Taxonomy.TermSetItem] $termsetCountry = $groupCountry.TermSets[$CountryTermsetGuid]
#Write-Host $termsetCountry.Name
[Microsoft.SharePoint.Taxonomy.TermSetItem] $termsetDocClassification = $groupDocClassification.TermSets[$DocClassificationTermsetGuid]
#Write-Host $termsetDocClassification.Name
#SetDefaultValues
#Get Reference to Organization Term and Set Default value
$term = $termset.GetTerm($OrgUnitTermguid)
$DefaultValue=”-1″+”;#”+$term.Name+[Microsoft.SharePoint.Taxonomy.TaxonomyField]::TaxonomyGuidLabelDelimiter+$term.Id
$logs=”Default Value for OrgUnit”+$DefaultValue
LogWrite $logs
#Get Reference to Brand Term and Set Default value
$termBrand = $termsetBrand.GetTerm($BrandTermGuid)
$DefaultValueBrand=”-1″+”;#”+$termBrand.Name+[Microsoft.SharePoint.Taxonomy.TaxonomyField]::TaxonomyGuidLabelDelimiter+$termBrand.Id
$logs=”Default Value for Brand”+$DefaultValueBrand
LogWrite $logs
#Get Reference to Audience Term and Set Default value
$termAudience = $termsetAudience.GetTerm($AudienceTermGuid)
$DefaultValueAudience=”-1″+”;#”+$termAudience.Name+[Microsoft.SharePoint.Taxonomy.TaxonomyField]::TaxonomyGuidLabelDelimiter+$termAudience.Id
$logs=”Default Value for Audience”+$DefaultValueAudience
LogWrite $logs
#Get Reference to Country Term and Set Default value
$termCountry = $termsetCountry.GetTerm($CountryTermGuid)
$DefaultValueCountry=”-1″+”;#”+$termCountry.Name+[Microsoft.SharePoint.Taxonomy.TaxonomyField]::TaxonomyGuidLabelDelimiter+$termCountry.Id
$logs=”Default Value for Country”+$DefaultValueCountry
LogWrite $logs
#Get Reference to Classification Term and Set Default value
$termDocClassification = $termsetDocClassification.GetTerm($DocClassificationTermGuid)
$DefaultValueDocClassification=”-1″+”;#”+$termDocClassification.Name+[Microsoft.SharePoint.Taxonomy.TaxonomyField]::TaxonomyGuidLabelDelimiter+$termDocClassification.Id
$logs=”Default Value for OrgUnit”+$DefaultValueDocClassification
LogWrite $logs
#Walk through each site in the site collection
$Web=Get-SPWeb $sSiteUrl
$logs=”Checking site:”+$Web.Url
write-host -ForegroundColor green $logs
LogWrite $logs
if($documentLibraryName -eq “All”)
{
#Go through each List in the site
$lstCollection=$Web.Lists
[int]$i
for($i=0;$i -lt $lstCollection.Count;$i++)
{
$list=$Web.Lists[$i]
if($list.BaseTemplate -eq “DocumentLibrary”)
{
$dList = [Microsoft.Sharepoint.SPDocumentLibrary]$list
Write-Host $dList.Title
if($defaultDocLibraries -notcontains $dList.Title)
{
Write-Host “All”
SetLibrary $dList
}
}# if list is document Library
} # End of each List
}#end of if All Doc Libraries
else
{
$list=$Web.Lists[$documentLibraryName]
if($list.BaseTemplate -eq “DocumentLibrary”)
{
Write-Host “SpecificLib”
$dList = [Microsoft.Sharepoint.SPDocumentLibrary]$list
Write-Host $dList.Title
SetLibrary $dList
}
}
# end of each Web Object
#Dispose of the site object
$Web.Dispose()

No comments:

Post a Comment