Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Created October 9, 2012 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/3860453 to your computer and use it in GitHub Desktop.
Save bradoyler/3860453 to your computer and use it in GitHub Desktop.
Powershell function to extract stuff from Sharepoint
# below are some SP scripts that do some basic tricks
function GetDocInfo
{
#//this checks existence of a file in a specified SPList
Param ( [parameter(Mandatory=$true)][string]$LibName,
[parameter(Mandatory=$true)][string]$DocName
)
$web = Get-SPWeb http://yourSPserver
$docLibrary = $web.Lists[$LibName]
$folder = $docLibrary.RootFolder
$spFilePath = ("/" + $DocName)
$spFullPath = $folder.Url + $spFilePath
write-host $spFullPath "exists: " $web.GetFile($spFullPath).Exists
}
function GetListFields
{
#// This returns all the Fields of a given SPList
#// Also shows how to loop through a collection with 'ForEach'
Param (
[parameter(Mandatory=$true)][string]$ListName
)
$ctx = Get-SPServiceContext http://yourSPserver
$Scope = New-Object Microsoft.SharePoint.SPServiceContextScope $ctx
$web = Get-SPWeb http://yourSPserver
$splist = $web.Lists[$ListName]
$Column = $splist.Fields
foreach($Field in $Column)
{
Write-Host "Field:" $Field
}
}
function QueryList
{
#//This queries the "Title" field of a given value in a given SPList
Param (
[parameter(Mandatory=$true)][string]$Title,
[parameter(Mandatory=$true)][string]$ListName
)
$web = Get-SPWeb http://yourSPserver
$splist = $web.Lists[$ListName]
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$camlQuery = '$Title'
$spQuery.Query = $camlQuery
$spQuery.RowLimit = 10
$spListItems = $spList.GetItems($spQuery)
foreach ($item in $spListItems)
{
write-host "Name:" $item.Name
write-host "Title:" $item["Title"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment