once in a while you'll need to create a list that contains an attachment; why not use a document library or a documents set, well i'll tell you why. When I came up with this i was a nOOb and had no idea what I was doing. So this example is by no means a best practice, but hey maybe one day I'll come across a scenario where it'll come in handy, so hey toss it into the old toolbox for later.
$RootURL = "http://ssrdevinter.corp.windsor/"
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
function AddAttachment($item, $filePath)
{
$bytes = [System.IO.File]::ReadAllBytes($filePath)
$item.Attachments.Add([System.IO.Path]::GetFileName($filePath), $bytes)
$item.Update()
}
function CreateJobPosting($newJobPost, $Title, $PostingID, $StartDate, $EndTime, $DepartmentIndex, $DescriptionPath)
{
$newJobPost["Title"] = $Title
$newJobPost["PostingID"] = $PostingID
$newJobPost["StartDate"] = $StartDate
$newJobPost["EndDate"] = $EndTime
$newJobPost["Department"] = 1
AddAttachment $newJobPost $DescriptionPath
$newJobPost.update()
Write-Host -ForegroundColor Green Job Posting: $Title Completed
}
try
{
LoadSharePointPowerShellEnviroment
$RootSite = new-object Microsoft.SharePoint.SPSite($RootURL)
$jobWeb = $RootSite.rootweb.webs["cityhall"].webs["work-for-windsor"]
$JobPostingList = $jobWeb.lists["JobPostingsListInstance"]
CreateJobPosting $JobPostingList.items.Add() "It Weather Widget Updater" "IT100" ([DateTime]::Now) ([DateTime]::Now.addDays(15)) 1 "c:\test.txt"
CreateJobPosting $JobPostingList.items.Add() "Barista" "IT001" ([DateTime]::Now) ([DateTime]::Now.addDays(5)) 1 "c:\test.txt"
CreateJobPosting $JobPostingList.items.Add() "Bard" "IT002" ([DateTime]::Now) ([DateTime]::Now.addDays(5)) 1 "c:\test.txt"
CreateJobPosting $JobPostingList.items.Add() "Easter Bunny" "IT003" ([DateTime]::Now) ([DateTime]::Now.addDays(5)) 1 "c:\test.txt"
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error in Create job Postings$_"
}
finally
{
$jobWeb.Dispose()
}
Tuesday, 13 November 2012
Thursday, 8 November 2012
List non published pages
This is a PowerShell snippet that lists all non published pages.
param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://SSRAP1'):"))
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
function NonPublishedPagesReport($nodeWeb2)
{
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($nodeWeb2)
$pubPages = $PubWeb.GetPublishingPages()
foreach($pp in $pubPages)
{
if ($pp.ListItem.File.level -ne "Published")
{
$data = @{"url" = $pubWeb.url.ToString() + "/pages/"
"name" = $pp.name.ToString()
"Status" = $pp.ListItem.File.level.ToString()
"user" = $pp.ListItem.File.CheckedOutByUser.LoginName
}
New-Object PSObject -Property $data
}
}
}
function RecurseNav([Microsoft.SharePoint.SPWeb]$nodeWeb)
{
$subWebs = $nodeWeb.GetSubwebsForCurrentUser()
NonPublishedPagesReport($nodeWeb)
foreach ($nextweb in $subWebs)
{
RecurseNav($nextWeb)
}
}
try
{
if($web_app_url -eq $null -or $web_app_url -eq '')
{
throw "You must specify your server name. Value provided was null/empty."
}
$web = Get-SPWeb $web_app_url
RecurseNav($web) | Out-GridView
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error: $_"
}
param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://SSRAP1'):"))
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
function NonPublishedPagesReport($nodeWeb2)
{
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($nodeWeb2)
$pubPages = $PubWeb.GetPublishingPages()
foreach($pp in $pubPages)
{
if ($pp.ListItem.File.level -ne "Published")
{
$data = @{"url" = $pubWeb.url.ToString() + "/pages/"
"name" = $pp.name.ToString()
"Status" = $pp.ListItem.File.level.ToString()
"user" = $pp.ListItem.File.CheckedOutByUser.LoginName
}
New-Object PSObject -Property $data
}
}
}
function RecurseNav([Microsoft.SharePoint.SPWeb]$nodeWeb)
{
$subWebs = $nodeWeb.GetSubwebsForCurrentUser()
NonPublishedPagesReport($nodeWeb)
foreach ($nextweb in $subWebs)
{
RecurseNav($nextWeb)
}
}
try
{
if($web_app_url -eq $null -or $web_app_url -eq '')
{
throw "You must specify your server name. Value provided was null/empty."
}
$web = Get-SPWeb $web_app_url
RecurseNav($web) | Out-GridView
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error: $_"
}
Labels:
Powershell,
SharePoint 2010
Tuesday, 6 November 2012
Elevated Privileges
In SharePoint once in a while you need to run something in an anonymous environment that requires user credentials not very promising is it? well luckily you can run your request using elevated privileges, you just need to remember to grab your site and your web again inside your delegate.
string welcomePageURL = null;
if (PublishingWeb.IsPublishingWeb(SPContext.Current.Web))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(SPContext.Current.Web.ID))
{
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(elevatedWeb);
welcomePageURL= pWeb.DefaultPage.ServerRelativeUrl;
}
}
});
}
string welcomePageURL = null;
if (PublishingWeb.IsPublishingWeb(SPContext.Current.Web))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(SPContext.Current.Web.ID))
{
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(elevatedWeb);
welcomePageURL= pWeb.DefaultPage.ServerRelativeUrl;
}
}
});
}
Labels:
SharePoint 2010
Friday, 2 November 2012
Remove user From all Groups
PowerShell script to remove a user from all groups in a web app
param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://Wingtip'):"))
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
try
{
if($web_app_url -eq $null -or $web_app_url -eq '')
{
throw "You must specify your server name. Value provided was null/empty."
}
$UserToRemove = $(read-host "Please Provide the User to remove; include the domain eg: domain/smithj")
if($UserToRemove -eq $null -or $UserToRemove -eq '')
{
throw "You must specify your user to remove. Value provided was null/empty."
}
LoadSharePointPowerShellEnviroment
$site = new-object Microsoft.SharePoint.SPSite($web_app_url)
$web = $site.OpenWeb()
$web.SiteUsers.Remove($UserToRemove)
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error: $_"
}
finally
{
$web.Dispose()
$site.Dispose()
}
param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://Wingtip'):"))
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
try
{
if($web_app_url -eq $null -or $web_app_url -eq '')
{
throw "You must specify your server name. Value provided was null/empty."
}
$UserToRemove = $(read-host "Please Provide the User to remove; include the domain eg: domain/smithj")
if($UserToRemove -eq $null -or $UserToRemove -eq '')
{
throw "You must specify your user to remove. Value provided was null/empty."
}
LoadSharePointPowerShellEnviroment
$site = new-object Microsoft.SharePoint.SPSite($web_app_url)
$web = $site.OpenWeb()
$web.SiteUsers.Remove($UserToRemove)
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error: $_"
}
finally
{
$web.Dispose()
$site.Dispose()
}
Labels:
Powershell,
SharePoint 2010
Delete All Groups
This script Deletes all SharePoint groups
param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://Wingtip'):"))
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
try
{
if($web_app_url -eq $null -or $web_app_url -eq '')
{
throw "You must specify your server name. Value provided was null/empty."
}
LoadSharePointPowerShellEnviroment
$site = new-object Microsoft.SharePoint.SPSite($web_app_url)
$web = $site.OpenWeb()
$groups = $web.sitegroups
$GroupsToDelete = @()
foreach ($groupToDelete in $groups)
{
write-host "Group to Delete : ",$groupToDelete -foregroundcolor Magenta
$GroupsToDelete += $groupToDelete.Name
}
foreach ($delgroup in $GroupsToDelete)
{
$web.SiteGroups.Remove($delgroup)
write-host "Group Deleted : ",$delgroup -foregroundcolor Green
}
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error: $_"
}
finally
{
$web.Dispose()
$site.Dispose()
}
param($web_app_url=$(read-host "Please provide web app url of the list to be delete (EG:'http://Wingtip'):"))
function LoadSharePointPowerShellEnviroment
{
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
try
{
if($web_app_url -eq $null -or $web_app_url -eq '')
{
throw "You must specify your server name. Value provided was null/empty."
}
LoadSharePointPowerShellEnviroment
$site = new-object Microsoft.SharePoint.SPSite($web_app_url)
$web = $site.OpenWeb()
$groups = $web.sitegroups
$GroupsToDelete = @()
foreach ($groupToDelete in $groups)
{
write-host "Group to Delete : ",$groupToDelete -foregroundcolor Magenta
$GroupsToDelete += $groupToDelete.Name
}
foreach ($delgroup in $GroupsToDelete)
{
$web.SiteGroups.Remove($delgroup)
write-host "Group Deleted : ",$delgroup -foregroundcolor Green
}
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error: $_"
}
finally
{
$web.Dispose()
$site.Dispose()
}
Labels:
Powershell,
SharePoint 2010
Tuesday, 16 October 2012
Delete a List
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
<#
.Synopsis
Delete Sharepoint list(s) from entire site
.DESCRIPTION
Delete SharePoint list instance(s) from an entire site, all subwebs
.EXAMPLE
delete-cSharePointList -ListTitle ListName -webUrl
http://wingtipserver
.EXAMPLE
delete-cSharePointList -ListTitle ListName -webUrl http://wingtipserver -subwebs
.EXAMPLE
delete-SharePointList -ListTitle_1, listTitle_2,
listTitle_n -webUrl http://wingtipserver
.EXAMPLE
delete-SharePointList -ListTitle_1, listTitle_2, listTitle_n -webUrl http://wingtipserver -subwebs
#>
function delete-SharePointList
{
[CmdletBinding(SupportsShouldProcess=$true, confirmImpact='High')]
Param
(
# an array of list titles to delete
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[System.String[]] $ListTitle,
# The url of the
web the lists reside in
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[ValidateScript({Get-SPSite -Identity $_ | Select-Object -Property Exists -ErrorAction SilentlyContinue})]
[System.String] $siteUrl,
# switch to
specify whether to delete list from all webs
[Parameter(ValueFromPipelineByPropertyName=$true, Position=2)]
[switch] $subwebs
)
Begin
{
$SPSite = Get-SPSite $siteUrl
}
Process
{
if($subwebs)
{
$SPSite.AllWebs | ForEach-object{
deleteList $_ $ListTitle
}
}
else
{
deleteList $spsite.RootWeb $ListTitle
}
}
End
{
$SPSite.Dispose()
}
}
function deleteList($currentWeb, $listTitle)
{
$listTitle | ForEach-Object{
$listInstance = $currentWeb.Lists.TryGetList($_)
if($listInstance -ne $null)
{
Write-Output ("found a
list with the title {0} in web {1}" -f $_, $currentWeb.Url)
If ($psCmdlet.shouldProcess("$listInstance
in web $webUrl", "Delete List"))
{
$listInstance.Delete()
Write-Output ("Deleted
list {0} in {1}" -f $_, $currentWeb.Url)
}
else
{
Write-Output ("Did not
delete list {0} in {1}" -f $_, $currentWeb.Url)
}
}
else
{
Write-Output ("Failed to
find list with title {0} in web {1}" -f $_, $currentWeb.Url)
}
}
}
Labels:
Powershell,
SharePoint 2010
Find Deployed Feature
If you're getting an error that refers to the oh so helpful GUID, here's a handy little script that lists Features and filters them
By Guid:
$results=Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\" feature.xml -rec
foreach ($File in $results)
{
[xml]$feat=gc $file.pspath
if($feat.feature.id -Like '3752ccf3*')
{
$feat.feature | Select ID, Title, Scope, Name
}
}
By Title:
$results=Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\" feature.xml -rec
foreach ($File in $results)
{
[xml]$feat=gc $file.pspath
if($feat.feature.title-Like 'TEC*')
{
$feat.feature | Select ID, Title, Scope, Name
}
}
By Guid:
$results=Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\" feature.xml -rec
foreach ($File in $results)
{
[xml]$feat=gc $file.pspath
if($feat.feature.id -Like '3752ccf3*')
{
$feat.feature | Select ID, Title, Scope, Name
}
}
By Title:
$results=Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\" feature.xml -rec
foreach ($File in $results)
{
[xml]$feat=gc $file.pspath
if($feat.feature.title-Like 'TEC*')
{
$feat.feature | Select ID, Title, Scope, Name
}
}
Labels:
Powershell
Subscribe to:
Posts (Atom)