SharePoint Tech Blog

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

Advertisements

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.

11/07/2009

SharePoint Publishing Process

Filed under: SharePoint — Tags: , , — kctnpblog @ 12:52 pm

A common request in MOSS is for the ability for users to create publishing pages such as news articles. If those publishing pages happen to involve images and other documents then the publishing process requires the author to first upload the image or document to another library (if it doesn’t already exist) and then reference the image or document in their page. If the article requires approval then this means that the approver needs to not only approve the news article but also the images and documents that have been referenced in the news article. To address this issue, I’m hoping that:

  1. the picture library and document library are both configured such that the items posted onto those libraries need approval.
  2. the page layouts on the page library can be customised such that the author is given a link to upload images and documents directly from the page being authored
  3. a custom workflow runs after the out of box approval workflow has run on the publishing page to then automatically approve the images and the documents referenced by the page.

I’ll be experimenting with this concept and will post once I have some more news on how this could be addressed.

08/07/2009

Create SharePoint Web Application and Root Site Collection using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 10:58 am

PowerShell is a really powerful scripting tool and I’m quite amazed at how simple it is to automate several tasks in SharePoint using PowerShell. To start this blog off, here’s a simple script to create a web application using PowerShell:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
# Return the Default TimeZone for Sydney
function get-defaulttimezoneid
{
[Microsoft.SharePoint.SPregionalSettings]::Globaltimezones | where-object { $_.Description -like "*sydney*" }
}
$port=$args[0]
$webAppBuilder=new-object Microsoft.SharePoint.Administration.SPWebApplicationBuilder( [Microsoft.SharePoint.Administration.SPFarm]::Local);
$webAppBuilder.Port=$port;
$webAppBuilder.RootDirectory="C:\Inetpub\wwwroot\wss\VirtualDirectories\$port";
$webAppBuilder.ServerComment="SharePoint - $port";
$webAppBuilder.ApplicationPoolID="SharePoint - $port";
$webAppBuilder.CreateNewDatabase=$true;
$webAppBuilder.DatabaseName="WSS_Content_$port";
$webAppBuilder.UseNTLMExclusively=$true;
$webAppBuilder.AllowAnonymousAccess=$false;
$webAppBuilder.UseSecureSocketsLayer=$false;
$webApp=$webAppBuilder.Create();
$webApp.ProvisionGlobally();
$timezone=get-defaulttimezoneid
$webApp.DefaultTimeZone=$timezone.ID;
$webApp.Update()
$webApp.ProvisionGlobally()
$webApp.ApplicationPool.CurrentIdentityType = [Microsoft.SharePoint.Administration.IdentityType]::SpecificUser;
$webApp.ApplicationPool.Username = "mossdemo\mossadmin";
$webApp.ApplicationPool.Password = "password";
$webApp.ApplicationPool.Update();
$webApp.ApplicationPool.Provision();
$title="Test Intranet" $description="Test Intranet"
$locale=1033
$sptemplate="SPSPORTAL#0"
$ownerlogin="mossdemo\mossadmin"
$owneremail="mossadmin@mossdemo.local"
$ownername="MOSS Administrator"
$webApp.Sites.Add("/", $title,$description,$locale,$sptemplate,$ownerlogin,$ownername,$owneremail)

All that’s needed is the port number to be passed to the code from the command line. If you’ve installed PowerShell then command prompt to the folder where this scipt file is and type in:
powershell .\<script file name>.ps1 8080

to create a web application on port 8080

« Newer Posts

Create a free website or blog at WordPress.com.