Consider a scenario wherein you need to replace a part of value in single line of text.
1) Fetch List Items based on a particular condition using CAML Query
Eg: Get all list items where status = "In Progress" and Content Type ="My Content"
2) From the list items returned by CAML query, modify and replace a particular string from a value.
3) Column type is single line of text
Add-PSSnapin Microsoft.Sharepoint.Powershell
cls
#Log Variables
$LogFolderPath = "C:\Logs\" # LogFolderPath
# Text File Path
$LogFilePath = $LogFolderPath+ "txtLog_WF_UPDATE_"+(Get-Date).ToString("MM_dd_yyyy_hh_mm_ss")+".txt"
# CSV Log File Path with current time stamp
$CSVLogFilePath=$LogFolderPath+ "CsvLog_WF_UPDATE_"+(Get-Date).ToString("MM_dd_yyyy_hh_mm_ss")+".csv"
#SP variables
$web=$null ;
$list=$null;
$SiteURL = "http://mysharepointsite:30042/sites/en-us"# Library Site Url
$ListName = "Pages";
$RowLimitValue="500"
#Logging the execution start time
Try
{
Write-Host "Started Script Execution at " (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") -ForegroundColor Green
"Started Script Execution at " + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") | Out-File $LogFilePath -Append
}
Catch
{
Write-Host "Issue in accessing the LogFile path $LogFilePath, Please check it is a valid path and the user has permissions on it. Failed with error : " + $_.Exception.Message + " at " + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss")
Exit
}
#Pre-requisite checks
Try
{
#Web Check
$web = Get-SPWeb $SiteURL
if($web -eq $null)
{
Write-Host "Cannot find an SPSite object that contains the following Id or Url: $SiteURL" $_.Exception.Message
"Cannot find an SPSite object that contains the following Id or Url: $SiteURL"+ $_.Exception.Message + " at " + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") | Out-File $LogFilePath -Append
Exit
}
#List Check
$list = $web.Lists[$ListName]
if($list -eq $null)
{
Write-Host "Cannot find a List with the Name: $ListName in the web " $_.Exception.Message
"Cannot find a List with the Name: $ListName in the web "+ $_.Exception.Message + " at " + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") | Out-File $LogFilePath -Append
Exit
}
}
Catch
{
Write-Host "Unable to connect to the Site, Please check the Url . Failed with error : " + $_.Exception.Message
"Unable to connect to the Site, Please check the Url . Failed with error : " + $_.Exception.Message + " at " + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") | Out-File $LogFilePath -Append
Exit
}
if($list -ne $null)
{
Write-Host "Quering the list to get all Items with specific condition ...." -ForegroundColor Yellow
"Quering the list to get all Items with specific condition ...." | Out-File $LogFilePath -Append
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewFields = "<FieldRef Name='Article_x0020_ID' />
<FieldRef Name='ID' />";
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = $RowLimitValue
$spQuery.Query = "<Where>
<And>
<Eq>
<FieldRef Name='ContentType' /><Value Type='Computed'>My Content</Value>
</Eq>
<Eq>
<FieldRef Name='Status' /><Value Type='Choice'>IN PROGRESS</Value>
</Eq>
</And>
</Where>"
$col=$list.GetItems($spQuery)
if($col.Count -gt 0)
{
Write-Host "Started Updating the value for IN-Progress ITEMS ..."
"Started Updating the IN-Progress Items at ... " + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") | Out-File $LogFilePath -Append
do
{
$listItems = $list.GetItems($spQuery)
$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $listItems)
{
$ID = $item["ID"]
$TopsURL = "https://www.mysite.[ARTICLEID].html"
$newTopsURL = $TopsURL.Replace('[ARTICLEID]', "new value")
$item["Article_x0020_ID"] = $newTopsURL
$item.SystemUpdate()
write-host "Update Test Done!!!"
New-Object -TypeName PSCustomObject -Property @{
RESULT="LOGS"
ID = $item["ID"]
CHECKEDOUTTO= $item["CheckoutUser"]
UPDATEDVAL= $item["Article_x0020_ID"]
STATUS = $item["Status"]
} | Export-Csv -Path $CSVLogFilePath -NoTypeInformation -Append
}
}while ($spQuery.ListItemCollectionPosition -ne $null)
}
else
{
Write-Host "No records found"
"No records found" + (Get-Date).ToString("MM-dd-yyyy HH:mm:ss") | Out-File $LogFilePath -Append
}
}
else
{
Write-Host "No List with Name "+$ListName
"No List with Name "+ $ListName | Out-File $LogFilePath -Append
}
Comments
Post a Comment