SharePoint Tech Blog

29/07/2010

Enumerate SharePoint features using Powershell

Filed under: PowerShell, SharePoint — Tags: , — kctnpblog @ 8:42 pm

This code requires the SharePoint Powershell Profile to be loaded.

To execute the code, provide the site url as a command line parameter.
e.g. powershell .\listfeatures.ps1 http://site

The output lists the features on the web application, site collection and web.


function writefeature($featureobj) {
$featuredef=$featureobj.DefinitionId;
$feature=$farm.FeatureDefinitions[$featuredef];
"{0,-40} {1,-50} {2,-40}" -f $feature.Id , $feature.DisplayName , $feature.Scope;
}

function setwindowsize() {
$pshost = get-host
$pswindow = $pshost.ui.rawui

$newsize = $pswindow.buffersize
$newsize.height = 3000
$newsize.width = 150
$pswindow.buffersize = $newsize

}

#Sets the size of the window to display well formatted output
setwindowsize

#Get command line parameter for url
$url=$args[0];

$farm = Get-SPFarm;

"Web application features"
$webapp = Get-SPWebApp $url;
$webapp.Features | ForEach-Object {
writefeature $_;
}

""
"Site collection features"
$spsite=Get-SPSite -url $url;
$spsite.Features | ForEach-Object {
writefeature $_;
}
$spsite.Dispose();

""
"Web features"
$spweb=Get-SPWeb -url $url;
$spweb.Features | ForEach-Object {
writefeature $_;
}
$spweb.Dispose();

Advertisements

03/05/2010

Create SharePoint Audiences using Powershell

Filed under: SharePoint — Tags: , — kctnpblog @ 12:49 am

This is a modified version of the codeplex script for creating SharePoint audiences. I find this a bit easier to use since it uses an xml file as its input.
This code requires the SharePoint Powershell Profile to be loaded.

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Audience");
#
$url="http://localhost";
#
$spsite=Get-SPSite -url $url;
$spcontext=[Microsoft.Office.Server.ServerContext]::GetContext($spsite);
$searchcontext=[Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($spsite)
$spsite.Dispose();
#
$audmanager=New-Object Microsoft.Office.Server.Audience.AudienceManager($spcontext);
$audcollection=$audmanager.Audiences;
#
[xml]$adata=Get-Content .\audiences.xml
#
$adata.Audiences | ForEach-Object {
$audname=$_.Audience.Name;
$auddesc=$_.Audience.Description;
$newaud=$audcollection.Create($audname,$auddesc);
$newaud.AudienceRules=New-Object System.Collections.ArrayList;
$_.Audience.Rule | ForEach-Object {
$property=$_.Property;
$operator=$_.Operator;
$value=$_.Value;
$newrule=New-Object Microsoft.Office.Server.Audience.AudienceRuleComponent($property,$operator,$value);
[Void]$newaud.AudienceRules.Add($newrule);
}
$newaud.Commit();
#compile audience
$args=$searchcontext.Name, "1", "1", $audname;
$runjob=[Microsoft.Office.Server.Audience.AudienceJob]::RunAudienceJob($args);
}

The audiences.xml file:

15/04/2010

Failed to add BusinessObjects XI SharePoint 2007 PIK to SharePoint

Filed under: SharePoint — Tags: — kctnpblog @ 11:45 pm

Whilst installing the BusinessObjects XI Portal Integration Kit for SharePoint; if the install stops with the following error:

“Failed to add BusinessObjects XI 3.1 SharePoint 2007 PIK to SharePoint. Please verify that you have all the necessary access rights and provide the correct server names, and reinstall this software.”

The following entry may also be found in the Application Event Log corresponding to this error:

Event ID: 3351
Description:
SQL database login failed. Additional error information from SQL Server is included below.
Login failed for user 'domain\userid'.

To fix this ensure that you have logged-on to the server as the SharePoint Farm Administrator before attempting the install.

09/11/2009

Enable incoming content deployment jobs on a SharePoint farm using PowerShell

Filed under: SharePoint — Tags: , — kctnpblog @ 2:24 am


cls
$spcdconfig=[Microsoft.SharePoint.Publishing.Administration.ContentDeploymentConfiguration]::GetInstance()
$spcdconfig.AcceptIncomingJobs = $true
$spcdconfig.RequiresSecureConnection = $false
$spcdconfig.Update()

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();

Older Posts »

Create a free website or blog at WordPress.com.