Thursday 5 February 2015

How to update a SharePoint list column through Powershell

PowerShell Script for updating SharePoint List Column

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
 Add-PSSnapin Microsoft.SharePoint.PowerShell
}
 
#Script settings
 

$web = Get-SPWeb https://sitename:port/

$list = $web.Lists["ListName"]

foreach ($item in $list.Items)
{
$item["Column1"] = "Text";
$item["Column2"] = "Text";
$item.Update();
}

write-host "Success"
 #Dispose web
 
$web.Dispose()

If it is a large List we can update the List column batch wise. Below is the script for updating share point list item batch wise

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)

{
  Add-PSSnapin Microsoft.SharePoint.PowerShell


}
 
#Script settings
 
 

$web = Get-SPWeb https://sitename:port/

$list = $web.Lists["Tasks"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery

$spQuery.ViewAttributes = "Scope='Recursive'";

$spQuery.RowLimit = 100

$caml = '<OrderBy Override="TRUE"><FieldRef Name="ID"/></OrderBy>'

$spQuery.Query = $caml

do

{
$listItems = $list.GetItems($spQuery)

$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition

foreach($item in $listItems)

{
$item["Column1"] = "Text";

$item["Column2"] = "Text";
$item.Update();

}

}
while ($spQuery.ListItemCollectionPosition -ne $null)

write-host "Success"
#Dispose web
$web.Dispose()

 

Hope this helps..

Sunday 1 February 2015

Upload Files to Sharepoint Document Library Using Task Scheduler



Steps to configure Task Scheduler with PowerShell scripts:

1. Create a folder containing the files to upload to SharePoint. In my case it is C:\Documents

2. Create another  folder to keep the Script file (i.e. BulkUpload.ps1). The script for uploading documents to share point library is already mentioned in my previous Blog.


 
3. Open Start -> Task Scheduler, see below Screenshot


     
4. Create a New Task.Following Window will appear, Give a Name to the Task and Select Two options mentioned in Screenshot to run as administrator



5. Click Action Tab -> New. A window will appear to set the actions



Enter powershell.exe as Program\script and the path to power shell script (C:\TaskSchedulerScript\BulkUpload.ps1) as arguments. Click OK
 
 
6. Click on Triggers ->New ->Give scheduler Time to run


7. When you click OK to confirm task creation, you will be prompted to enter the password for the account you selected to run the script.

8. Enter the password and click OK

9. You can see a task is created in the Task Scheduler Library (if not, click refresh in the right-hand panel)