Como sabéis, el uso de Property Bags es una de las técnicas que tenemos disponibles en SharePoint para poder definir propiedades de configuración en los distintos niveles lógicos de la arquitectura de la plataforma (Granja, Aplicación Web, Colección de Sitios, Sitio, Lista y Carpeta). Las Property Bags son muy útiles para almacenar cadenas de conexión, datos específicos para nuestras soluciones, etc. En este post, os voy a enseñar como crear, actualizar y borrar property bags por medio de PowerShell…como veréis en el script que os pego a continuación (y que os podéis descargar desde el siguiente enlace de la Script Gallery de TechNet How to create, update and delete a property bag for a site collection), el truco pasa por saber trabajar con la propiedad AllProperties definida en este caso a nivel de sitio de SharePoint…el resto, no es más que jugar con añadir, actualizar y borrar las propiedades por medio de la clave definida.
1: ############################################################################################################################################
2: # This script allows to work with SharePoint property bags at the Site Collection Level
3: # Required Parameters:
4: # ->$sSiteCollection: Site Collection where we want to do add a property bag.
5: # ->$sOperationType: Operation type to be done with the property bag - Create - Update - Delete.
6: # ->$sPropertyBagKey: Key for the property bag to be added.
7: # ->$sPropertyBagValue: Value for the property bag addded.
8: ############################################################################################################################################
9:
10: If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
11: { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
12:
13: $host.Runspace.ThreadOptions = "ReuseThread"
14:
15: #Definition of the function that allows to create, update and remove a property bag
16: function WorkWithSiteCollectionPropertyBags
17: {
18: param ($sSiteCollection,$sOperationType,$sPropertyBagKey,$sPropertyBagValue)
19: try
20: {
21: $spSite=Get-SPSite -Identity $sSiteCollection
22: $spwWeb=$spSite.OpenWeb()
23: switch ($sOperationType)
24: {
25: "Create" {
26: Write-Host "Adding property bag $sPropertyBagKey to $sSiteCollection !!" -ForegroundColor Green
27: $spwWeb.AllProperties.Add($sPropertyBagKey,$sPropertyBagValue)
28: $spwWeb.Update()
29: $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
30: Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green
31: }
32: "Read" {
33: Write-Host "Reading property bag $sPropertyBagKey" -ForegroundColor Green
34: $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
35: Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green
36: }
37: "Update" {
38: $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
39: Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green
40: Write-Host "Updating property bag $sPropertyBagKey for $sSiteCollection" -ForegroundColor Green
41: $spwWeb.AllProperties[$sPropertyBagKey]="SPSiteColPBagUpdatedValue_2"
42: $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
43: Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green
44: }
45: "Delete" {
46: Write-Host "Deleting property bag $sPropertyBagKey" -ForegroundColor Green
47: $spwWeb.AllProperties.Remove($sPropertyBagKey)
48: $spwWeb.Update()
49: $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]
50: Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green
51: }
52: default {
53: Write-Host "Requested Operation not valid!!" -ForegroundColor DarkBlue
54: }
55: }
56: $spwWeb.Dispose()
57: $spSite.Dispose()
58: }
59: catch [System.Exception]
60: {
61: write-host -f red $_.Exception.ToString()
62: }
63: }
64:
65: Start-SPAssignment –Global
66: #Calling the function
67: $sSiteCollection="http://<YourSiteCollection>"
68: $sPropertyBagKey="SPSiteColPBagKey_2"
69: $sPropertyBagValue="SPSiteColPBagValue_2"
70: #WorkWithSiteCollectionPropertyBags -sSiteCollection $sSiteCollection -sOperationType "Delete" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue
71: WorkWithSiteCollectionPropertyBags -sSiteCollection $sSiteCollection -sOperationType "Create" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue
72: WorkWithSiteCollectionPropertyBags -sSiteCollection $sSiteCollection -sOperationType "Read" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue
73: WorkWithSiteCollectionPropertyBags -sSiteCollection $sSiteCollection -sOperationType "Update" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue
74: WorkWithSiteCollectionPropertyBags -sSiteCollection $sSiteCollection -sOperationType "Delete" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue
75:
76:
77: Stop-SPAssignment –Global
78:
79: Remove-PSSnapin Microsoft.SharePoint.PowerShell
Nota: Si alguno tiene la tentación de usar la colección Properties en lugar de AllProperties, le aconsejo que no lo haga sin revisar antes el siguiente enlace y evitarse así unos cuantos dolores de cabeza: http://www.stuartroberts.net/index.php/2011/12/19/remove-property-sppropertybag/