Monday, May 23, 2011

In RSS feeds at the site level with PowerShell, including disabling list RSS feeds by default so that you can choose which lists you want to enable when required.

In this article, I will go through how to enable RSS feeds on a list and configure the various options associated with them, including selecting the list columns to be published in the feed.

Enabling RSS Feeds on a list

The first part of the script connects to the site and list you want to configure:

#Get site collection and list objects
$web = Get-SPWeb http://portal
$list = $web.Lists["Documents"]

We then enable RSS feeds on the list, if currently disabled. This is the same thing as clicking on the following option in the browser UI:

image

if ($list.EnableSyndication -eq $false)
{
$list.EnableSyndication = $true
$list.Update()
write-host "RSS Feeds enabled on list:" $list.Title
}
else
{
write-host "RSS Feeds already enabled on list:" $list.Title
}

List RSS properties

For configuring the properties of the RSS feed, we need to use the EnsureRssSettings method to check the RSS settings of the list and connect to the root folder:

#Checks the RSS settings of the list, and, if necessary, updates them to ensure that the most current settings are used
$list.EnsureRssSettings()
#Get root folder which contains RSS configuration properties
$rootFolder = $list.RootFolder

The commands for setting the properties themselves depends on the options you wish to set with the script. The table below shows the code for setting each of the properties along with the section of the UI they correspond to:

RSS Channel Information image
RSS Channel settings can be set with the following code. Where there is a Yes/No option, as with the truncate multi-line text setting above, use 0 to specify No and 1 to specify Yes:

$rootFolder.Properties["vti_rss_LimitDescriptionLength"] = 0
$rootFolder.Properties["vti_rss_ChannelTitle"] = "Intranet: Documents"
$rootFolder.Properties["vti_rss_ChannelDescription"] = "RSS feed for the Documents list."
$rootFolder.Properties["vti_rss_ChannelImageUrl"] = "/_layouts/images/siteIcon.png"

Document Options (document libraries only) image
$rootFolder.Properties["vti_rss_DocumentAsLink"] = 1
$rootFolder.Properties["vti_rss_DocumentAsEnclosure"] = 1
Item Limit image
$rootFolder.Properties["vti_rss_ItemLimit"] = 25
$rootFolder.Properties["vti_rss_DayLimit"] = 7
Other Options (not configurable from the UI) These options are hidden from the UI and only configurable through code/PowerShell. I haven’t tested either of them, but assume the first specifies if you want to display the RSS icon with the feed (default Yes) and the second configures the RSS feed for display on the Quick Launch menu (default No):

$rootFolder.Properties["vti_rss_DisplayRssIcon"] = 1
$rootFolder.Properties["vti_rss_DisplayOnQuicklaunch"] = 0

Of course, as the option to configure these settings are both hidden from the UI, they may not do anything at all!

Once you have configured the properties of the list RSS feed, you have to apply them with the following update commands:

#Update root folder and list with the changes
$rootFolder.Update()
$list.Update()

Selecting list columns for use in RSS Feeds

The PowerShell script code in this section will include the following list columns for display in the RSS feed:

image

Columns marked with an asterisk will map to standard RSS tags – e.g., "Created by" is mapped to the RSS "Author" tag. All other columns will display in the RSS description tag.

First, we need to get a hidden view called “RssView” and delete all currently selected columns from the view:

#Get RSS View and delete all existing columns
$rssView = $list.Views["RssView"]
$rssViewFields = $rssView.ViewFields
$rssViewFields.DeleteAll()

Next, we add the columns to the view as required.

#Add new columns using their display name
$rssViewFields.Add("Title")
$rssViewFields.Add("Version")
$rssViewFields.Add("Modified By")

The method I have used above allows you to specify the display name of the columns to be added. You could choose to use internal column names instead with this code for each column: $rssViewFields.Add($list.Fields.GetFieldByInternalName("fieldname"))

Finally, we have to update the RSS view and dispose of the web object:

#Update the RSS View
$rssView.Update()

#Dispose of web object
$web.Dispose()

No comments: