SharePoint Tech Blog

15/08/2009

Set expiry policy on SharePoint list using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 1:51 pm

MOSS out of box functionaliy allows creating information management policies on lists to expire content on those lists. If this needs to be done using code then the following code could be used to set the expiry policy programmatically using PowerShell.
This code requires the SharePoint Powershell Profile to be loaded beforehand


#Include the additional dll references required
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System")
[Void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Workflow")

$web=Get-SPWeb -webUrl "http://mossdev";

$list=$web.Lists["MyList"];

$listctype=$list.ContentTypes["Item"];

$policy=[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($listctype);

if ($policy -eq $null) {
$policy=[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]:: CreatePolicy($listctype,$null);
}

$policy.Description="Expire items the day after the expiry date";
$policy.Statement="Expire items the day after the expiry date";
$policy.Update();

$expirypolicyexists=$false;
if ($policy.Items.Count -ne 0)
{
foreach ($policyitem in $policy.Items)
{
if ($policyitem.Name -eq "Expiration")
{
$expirypolicyexists=$true;
}
}
}

if ($expirypolicyexists -eq $false)
{
$policyFeatureID=[Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration];
#set the internal field name and workflow id according to your needs
#in this case it has been set to expire the item based on the
#expiry date custom field and execute the disposition approval workflow
#when the item expires
formula
$policy.Items.Add($policyFeatureID,$customData);
$policy.Update();
"Expiry policy added"
}
else
{
"An expiry policy already exists - not overwriting";
}

$web.Dispose();

14/08/2009

Associate a workflow to a SharePoint list using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 1:56 am

This script will associate one of the out of the box Disposition Approval workflows to a list.
This code requires the SharePoint Powershell Profile to be loaded beforehand


#Include the additional dll references required
[Void] [System.Reflection.Assembly]:: LoadWithPartialName("System")
[Void] [System.Reflection.Assembly]:: LoadWithPartialName("Microsoft.SharePoint.Workflow")

#open the web
$web=Get-SPWeb -webUrl "http://moss";

#list to which the workflow is to be associated
$list=$web.Lists["MyList"];

#create workflow association object
$culture=new-object System.Globalization.CultureInfo("en-AU");
$basetemplate = $web.WorkflowTemplates.GetTemplateByName("Disposition Approval",$culture);
$wftasklist=$web.Lists["Workflow Tasks"];
$wfhistorylist=$web.Lists["Workflow History"];
$wfAssoc=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]:: CreateListAssociation($basetemplate,"Disposition Approval",$wftasklist,$wfhistorylist);

#associate workflow to list
$list.AddWorkflowAssociation($wfAssoc);
$list.Update();

#cleanup and end
$web.Dispose();

11/08/2009

Read SharePoint list using PowerShell

Filed under: SharePoint — Tags: , , — kctnpblog @ 10:51 pm

Although this code is specifically to read the site directory, it could be modified to read any other list in SharePoint.
This code requires the SharePoint Powershell Profile to be loaded beforehand

$web=Get-SPWeb -url "http://moss/sitedirectory";
$list=$web.Lists["Sites"];
foreach ($item in $list.Items){
$item["Title"];
$item["Description"];
$urlfield=$item["URL"].split(",");
$urladdress=$urlfield[0];
$urldescription=$urlfield[1].trim();
$urladdress;
$urldescription;
}
$web.Dispose()

Add item to SharePoint list and approve using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 7:31 am

This specific example is about adding an item to the site directory list and approving it automatically. However the same code can be used to add an item to any list in SharePoint and approve it.

This code requires the SharePoint Powershell Profile to be loaded beforehand

$web=Get-SPWeb -url "http://moss/sitedirectory";
$list=$web.Lists["Sites"];
$newitem=$list.items.Add();
$newitem["Title"]="Project One";
$newitem["Description"]="Test Site for Project One";
$newitem["URL"]="http://mossprojects/projectone, Project One URL";
$newitem.ModerationInformation.Status= [Microsoft.SharePoint.SPModerationStatusType]::Approved;
$newitem.ModerationInformation.Comment="Auto approved";
$newitem.Update();
$web.Dispose();

07/08/2009

Execute SharePoint timer job using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 1:20 am

If you are in a situation where you need to test a content expiry policy, the schedule for the out of box timer jobs will need to be changed.
To change the “Hold Processing and Reporting” timer job, you can do this using

stsadm -o setholdschedule -schedule "every 1 minutes between 0 and 59"

To change the “Information management policy” timer job, you can do this using

stsadm -o setpolicyschedule -schedule "every 1 minutes between 0 and 59"

To change the “Records Center Processing” timer job, you can do this using

stsadm -o setrecordsrepositoryschedule -schedule "every 1 minutes between 0 and 59"

The “Expiration policy” timer job however, cannot be set using stsadm. Instead, use this Powershell script to run it manually:
This code requires the SharePoint Powershell Profile to be loaded beforehand…

$farm=[Microsoft.SharePoint.Administration.SPFarm]::Local
foreach ($service in $farm.Services) {
if ($service.TypeName -eq "Microsoft.Office.RecordsManagement.Internal. PolicyConfigService"){
foreach ($jobdefinition in $service.JobDefinitions){
if ($jobdefinition.TypeName -eq "Microsoft.Office.RecordsManagement.Internal. ExpirationJobDefinition"){
$jobdefinition.Execute([Guid]::Empty);
Write-Host "Execution completed";
}
}
}
}

Schedule the above code to run automatically based on a windows timer job once very minute or as often as required on the development/test environment.
It is recommended to run the “Information management policy” timer job before running the “Expiration policy” timer job because the “Information management policy” timer job updates each item that is affected by the policy if the policy has changed.

05/08/2009

List the information management policy on a specific content type using PowerShell

Filed under: SharePoint — Tags: , , — kctnpblog @ 6:38 am


$web=Get-SPWeb -url "http://moss";
$contenttype=$web.AvailableContentTypes["My Content Type"];
$policy=[Microsoft.Office.RecordsManagement.InformationPolicy.Policy]::GetPolicy($contenttype);
$policyitems=$policy.Items;
foreach ($policyitem in $policyitems)
{
$policyitem
}
$web.Dispose();

List/Enumerate information management policies on a SharePoint site collection

Filed under: SharePoint — Tags: , , — kctnpblog @ 5:41 am

This code requires the SharePoint Profile for PowerShell

$site=Get-SPSite -url "http://moss";


$policycatalog= [Microsoft.Office.RecordsManagement.InformationPolicy .PolicyCatalog] ($site);
$policies=$policycatalog.PolicyList;
foreach ($policy in $policies)
{
$policy;
$policyitems=$policy.Items;
foreach ($policyitem in $policyitems)
{
$policyitem
}
}
$site.Dispose();

Create SharePoint site column using PowerShell

Filed under: SharePoint — Tags: , , — kctnpblog @ 3:40 am

This code requires the SharePoint Powershell Profile to be loaded beforehand…

$web=Get-SPWeb("http://moss");
$web.Fields.Add("My Column Name","Text",0);
$web.Dispose();

SharePoint Profile for Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 3:36 am


## SharePoint DLL

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Policy")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Portal")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration")

############################################

# Get-SPSite -url

############################################

function global:Get-SPSite($url){

return new-Object Microsoft.SharePoint.SPSite($url)

}

############################################

# Get-SPWeb [-url | -site ]

############################################

function global:Get-SPWeb($url,$site)

{

if($site -ne $null -and $url -ne $null){"Url OR Site can be given"; return}

#if SPSite is not given, we have to get it...

if($site -eq $null){

$site = Get-SPSite($url);

}

#Output 1 or more sites...

if($url -eq $null){

for($i=0; $i -lt $s.AllWebs.Count;$i++){

Write-Output $s.AllWebs[$i]; ##Send through Pipeline

$s.Dispose(); ##ENFORCED DISPOSAL!!!

}

}else{

Write-Output $site.OpenWeb()

}

}

############################################

# Dispose-SPWeb -web -parent

############################################

function global:Dispose-SPWeb($web,$parent=$false){

$site = $web.Site;

$web.Dispose();

if($parent){ $site.Dispose(); }

}

############################################

# Get-SPFarm

############################################

function global:Get-SPFarm(){

return [Microsoft.SharePoint.Administration.SPFarm]::Local

}

############################################

# Get-SPWebApp -url

############################################

function global:Get-SPWebApp($url){
return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)

}

04/08/2009

Extracting the SharePoint dll’s from the GAC using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 7:31 am

To extract the SharePoint dll’s from the GAC using Powershell:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Portal")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Policy")

[appdomain]::currentdomain.getassemblies() |
where {($_.fullname -match "Microsoft.Office") -OR ($_.fullname –match "Microsoft.SharePoint")} |
copy-item -path {$_.location} -destination c:\temp\ -verbose

Ensure that the c:\temp folder exists and run this code to extract the SharePoint dll’s into that folder.

Older Posts »

Create a free website or blog at WordPress.com.