Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $siteUrl = "http://bot-sp2016"
  2. $salesWebUrl = "SalesManagement"
  3. $knowledgeWebUrl = "KnowledgeBase"
  4.  
  5.  
  6. $customersListUrl = "Egrul";
  7. $customerFieldName = "KsupEgr_Customer"
  8. $sellerFieldName = "KsupSeller"
  9.  
  10. $presaleActivityListUrl = "PresaleActivity";
  11. $customerSellerCtId = "0x0100E737C3D7E2C747AA8DD044E37D606A4E009D777DEF9CEB4D8E8B7937761C50A94B"
  12. $opBaseCtId = "0x0100E737C3D7E2C747AA8DD044E37D606A4E004A65E904D62C43AE9209A623094F1590"
  13.  
  14.  
  15. function GetReadyForSp() {
  16.     $ver = $host | select version
  17.     if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
  18.     if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
  19.     {
  20.         Add-PSSnapin "Microsoft.SharePoint.PowerShell"
  21.     }
  22. }
  23.  
  24. function FindListsUsingCt($web, $ctId) {
  25.     $foundLists = @()
  26.     foreach($list in $web.Lists) {
  27.         $ct = $list.ContentTypes | Where {$_.Id.ToString() -like "$ctId*" }
  28.         if ($ct -ne $null) {
  29.             Write-Host "[x] list found $list" -ForegroundColor Cyan
  30.             $foundLists += $list
  31.         }
  32.     }
  33.  
  34.     return $foundLists
  35. }
  36.  
  37. function UpdateLookupField($field, $list) {
  38.     $field.LookupWebId = $list.ParentWeb.ID
  39.     $field.LookupList = $list.ID
  40.     $field.Update($false)
  41. }
  42.  
  43. function RemoveFieldLookupField($field) {
  44.     $schema = [xml]$field.SchemaXml
  45.     $propertyToChange = $schema.Field.Customization.ArrayOfProperty.Property | Where {$_.Name -eq "ChildLookupFields"} | ForEach-Object { [void]$_.ParentNode.RemoveChild($_) }
  46.  
  47.     $field.SchemaXml = $schema.OuterXml
  48.     $field.Update($false)
  49. }
  50.  
  51.  
  52. function SetFieldService($field) {
  53.     $deleteProps = "LookingFields", "ParentLookupField"
  54.    
  55.     $schema = [xml]$field.SchemaXml
  56.  
  57.     $propertyToChange = $schema.Field.Customization.ArrayOfProperty.Property | Where {$_.Name -eq "WcfService"}
  58.  
  59.     $propertyToChange.Value.InnerText = "/_vti_bin/Portal/LanitKsupService.svc/GetItemsByTitleWithoutArchive"
  60.      
  61.     $propertyToChange = $schema.Field.Customization.ArrayOfProperty.Property | Where {$deleteProps -contains $_.Name } | ForEach-Object { [void]$_.ParentNode.RemoveChild($_) }
  62.  
  63.     $field.SchemaXml = $schema.OuterXml
  64.     $field.Update($false)
  65. }
  66.  
  67. function FixList($list, $customersList) {
  68.     $fieldCustomer = $list.Fields | Where {$_.InternalName -eq $customerFieldName }
  69.     $fieldSeller = $list.Fields | Where {$_.InternalName -eq $sellerFieldName}
  70.  
  71.     Write-Host "[x] fixing field $fieldCustomer" -ForegroundColor Cyan
  72.  
  73.     if($fieldCustomer) {
  74.         UpdateLookupField -field $fieldCustomer -list $customersList
  75.         SetFieldService -field $fieldCustomer
  76.     } else {
  77.         Write-Host "Skipping customer field for list $list" -ForegroundColor Red
  78.     }
  79.    
  80.     if($fieldSeller) {
  81.         RemoveFieldLookupField -field $fieldSeller
  82.     } else {
  83.         Write-Host "Skipping seller field for list $list" -ForegroundColor Red
  84.     }
  85.    
  86.  
  87.     Write-Host "[x] field fixed!" -ForegroundColor Green
  88. }
  89.  
  90. GetReadyForSp
  91.  
  92. $salesWeb = Get-SPWeb "$siteUrl/$salesWebUrl"
  93. $knowledgeWeb = Get-SPWeb "$siteUrl/$knowledgeWebUrl"
  94. $customersList = $knowledgeWeb.GetList($knowledgeWeb.Url + "/lists/$customersListUrl")
  95.  
  96. $customerSellerLists = FindListsUsingCt -web $salesWeb $customerSellerCtId
  97. $opLists = FindListsUsingCt -web $salesWeb $opBaseCtId
  98.  
  99. $allLists = $customerSellerLists + $opLists
  100.  
  101. foreach($list in $allLists) {
  102.     Write-Host "[x] list $list in process"
  103.     FixList -list $list -customersList $customersList
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement