Pages

Thursday, March 16, 2017

Add Web Application property bag using Power Shell and use them in SharePoint timer job

Introduction

SharePoint Web Application level property bag is used to store configuration setting data and information. I make use of the property bag when I write a timer job.

I store value in property bag as key, value using power shell and using C# server object model I retrieve and make use of it.

Set Web Application Level Property Bag

You can create a function in Power shell

 function SetWebApplicationPropertyBag ($webAppUrl, $key, $value) {

	$Web = Get-SPWebApplication -Identity $webAppUrl
	$Web.Properties.Add($key,$value) #add property bag
	$Web.Update()

	Write-host "Created new Property:" $Web.Properties[$key]
 
 }

And you can call the function like this.

$webAppUrl ="Web Application url goes here"
$PropertyName1 ="Key"
$PropertyValue1 ="Value"

SetWebApplicationPropertyBag $webAppUrl $PropertyName1 $PropertyValue1

You have to change the values of $webAppUrl, $PropertyName1 and #PropertyValue1.

Copy paste code in SharePoint Management shell and your web application property will be created.

Retrieve property bag value in SharePoint Timer Job

Since SharePoint Timer Jobs are created with the feature, in Web Application level scope, we can use the below code to retrieve the data.

var value = WebApplication.Properties["Key"].ToString();

You can make use of value and do operations.

Remove Property Bag

You can use below code to remove a value from property bag.

$Web = Get-SPWebApplication -Identity "Web Application Url"

$Web.Properties.Remove("Key")

$Web.Update()

No comments:

Post a Comment