Thursday, September 25, 2014

PowerShell Script to download SharePoint List attachments


Here is a PowerShell script for downloading SharePoint List Item attachments. When the code is run all attachments are downloaded. Subfolders are created based on the Assigned To.


$webUrl = "Your Site Name"
$listname = "Your List Name"
$tempLocation = "Folder Location"
$site = new-object Microsoft.SharePoint.SPSite($webUrl)
$web = $site.OpenWeb()
$list = $web.Lists[$listname]
foreach ($listItem in $list.Items)
{
$listItem["Assigned To"].ToString();
$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$listItem["Assigned To"].ToString());
$userfield.User.DisplayName;
Write-Host " Content: " $userfield.User.DisplayName;
$destinationfolder = $tempLocation + "\" + $userfield.User.DisplayName;
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory
}
foreach ($attachment in $listItem.Attachments)
{
$file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)
$bytes = $file.OpenBinary()
$path = $destinationfolder + " \" + $attachment
Write " Saving $path"
$fs = new-object System.IO.FileStream($path, " OpenOrCreate")
$fs.Write($bytes, 0 , $bytes.Length)
$fs.Close()
}
}

Wednesday, May 28, 2014

SharePoint 2010 list Column/Field Validation


My customer wanted to be able to make a Column/Field Validation for a SharePoint 2010 List column. Date column(Date1) needs to be validated so that it is greater than 3 days from today but this column is not a required column so it makes little tricky.

 Initially I was using the below simple formula for my column validation,
 =[Date1]>NOW()+3

It works great if there is a value in the Date1 column/field but I ran into an issue when there is no value in this column/field because the validation is triggering. The formula must evaluate to TRUE for validation to pass.

 So I came up with the below formula which uses IF condition,
=IF(Date1]<>"",IF([Date1]>NOW()+3,TRUE,FALSE),TRUE)

If Date1 is not Empty  then I want to make sure that Date1 has a value which is greater than 3 days from today so I go into the second IF statement which will return a TRUE if Date1 is greater than 3 days from today or FALSE if it doesn't match.   If Date1 is empty  or has no value then I don’t care so I will always return TRUE.


If we need to validate 2 fields then we should be using List Validation feature

 

Tuesday, May 27, 2014

PowerShell script to get List name and GUID


Here is a PowerShell script to get the List name along with GUID


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#Configure the location for the ListGUID output file
$OutputFile="C:\ListGUID.csv";
#Replace Yoursitename as needed
$site = Get-SPSite http://Yoursitename
$web = $site.RootWeb;
$web.lists | Select-Object title, id | Export-Csv -Path $OutputFile;
$web.Dispose();
$site.Dispose();