Archive

Posts Tagged ‘SharePoint 2010’

Adding Announcements to SharePoint 2010 through PowerShell

January 27th, 2010 Niklas Goude No comments

SharePoint 2010 is on it’s way and it’s way cool. Lots of great CmdLets that simplify the SharePoint administrators life. In this post we are going to check out how to add Announcements to an Announcements list in SharePoint through PowerShell. First we’ll do it step-by-step in the “SharePoint 2010 Management Shell” (PowerShell shell that registers SharePoint CmdLets) and then we’ll look at a script that automates the task.

First, start the SharePoint 2010 Management Shell.

Now we can get fancy with SharePoint administration. Adding a new announcement is done basically in the same way as in MOSS, though it’s alot simpler since Microsoft added tons of new great SharePoint CmdLets. Below is an example on adding a new announcement to a SharePoint list.


PS > $siteScope = Start-SPAssignment
PS > $Announcement = ($siteScope | Get-SPWeb -identity http://sharepoint).Lists["Announcements"]
PS > $NewItem = $Announcement.Items.Add()
PS > $NewItem["Title"] = "My First Announcement"
PS > $NewItem["Body"] = "<h2>PowerShell Magic</h2>"
PS > $NewItem["Expires"] = "1/28/2010"
PS > $NewItem.Update()
PS > Stop-SPAssignment $siteScope

The Start-SPAssignment and Stop-SPAssignment should be used with certain objects such as SPSite, SPWeb and SPSiteAdministration, since they need to be disposed. You can read more about the CmdLets at the I Love SharePoint blog.

The rest of the code is pretty straightforward, Get the Web through Get-SPWeb, create an object holding the List, use the Add() method, add the information and finally update the new item using the Update() method.

Here’s what it looks like in the Shell.

Announcement

And here’s our new Item in SharePoint.

Announcement2

When put in a script, we can simply add a new announcement on a single line.


PS > Add-SharePointAnnouncement.ps1 -identity http://sharepoint -List Announcements -Title "Added From Script" `
>> -Body "<h2>PowerShell Scripts Rock</h2>" -Expires 1/28/2010

Click here to download the script.

Rating 3.00 out of 5
[?]
Categories: SharePoint 2010 Tags: