Tuesday, August 2, 2011

Update Workspace URL using Powershell

Below script for it loops through all of your sites in the server

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
$WebAppUrl = "servername"
$site = new-object Microsoft.SharePoint.SPSite($WebAppUrl)
$web = $site.OpenWeb()
$list = $web.Lists["Projects List"]
$query = new-object Microsoft.SharePoint.SPQuery
$query.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Choice'>Success</Value></Eq></Where><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>"
#$query.RowLimit = 2
[xml]$viewfields = "<ViewFields><FieldRef  Name='Project_x0020_ID' /><FieldRef Name='Title' /></ViewFields>"
$result = $list.GetItems($query)
#Write-Host $result.count
foreach ($resultItem in $result)
{
  $siteURL = $resultItem["SiteURL"]
        $oRootsite = new-object Microsoft.SharePoint.SPSite($siteURL)
        $RootWeb = $oRootsite.OpenWeb()        
  $lookForList ="Events"
  $EventsList= $RootWeb.Lists[$lookForList]
   
   if ($EventsList -ne $null)
     {

    $queryEvents = new-object Microsoft.SharePoint.SPQuery
   $queryEvents.Query ="<Where><Eq><FieldRef Name='WorkspaceLink' /><Value Type='CrossProjectLink'>1</Value></Eq></Where>"
   #[xml]$viewfields = "<ViewFields><FieldRef  Name='Project_x0020_ID' /><FieldRef Name='Title' /></ViewFields>"
   $Queryresult = $EventsList.GetItems($queryEvents)
   Write-Host $siteURL  + ":" + $Queryresult.Count
   foreach ($eventListItem in $Queryresult)
   {
    
    if($eventListItem["WorkspaceLink"]  -eq $true)
    {  
    $eventListItem["Workspace"]= $null
    $eventListItem.SystemUpdate($false)
    Write-Host $eventListItem["Title"]  "---"  $eventListItem["Workspace"]
    }
   }
  }

  # Rollout OPS Project Stages and Update document edit form
  # Enable-SPFeature –Identity "be307273-d9de-4f16-84d2-52b5e1467ff0" –url $siteURL


}

for single site

$siteUrl = "http://servername/sites/P127036/"
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint") > $null
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$RootWeb = $site.RootWeb
$lookForList ="Events"
$EventsList= $RootWeb.Lists[$lookForList]
  if ($EventsList -ne $null)
    {
  $query = new-object Microsoft.SharePoint.SPQuery
 $query.Query ="<OrderBy><FieldRef Name='Title' /></OrderBy>"
 #[xml]$viewfields = "<ViewFields><FieldRef  Name='Project_x0020_ID' /><FieldRef Name='Title' /></ViewFields>"
 $result = $EventsList.GetItems($query)
 Write-Host $result.count
 foreach ($resultItem in $result)
 {
  if($resultItem["WorkspaceLink"]  -ne $false)
  {
  Write-Host $resultItem["Title"]  "---"  $resultItem["Workspace"]
  $resultItem["Workspace"]= $null
  $resultItem.SystemUpdate()
  Write-Host  "After updated:"  $resultItem["Workspace"]
  }
 }
 
}

No comments:

Post a Comment

Followers

Blog Archive