Forum Discussion
pratiksavla
Oct 09, 2023Copper Contributor
SharePoint (2019) PowerShell Command for SPSite.WebApplication.Properties
We are trying to set and get properties in WebApplication using following code SPSite site = new SPSite(<SiteUrl>)
// or
SPSite site = SPFeatureReceiverProperties.Feature.Parent as SPSite
// set pr...
LeonPavesic
Oct 09, 2023Silver Contributor
Hi pratiksavla
To get and update the property set using the mentioned code via PowerShell, you can use the following steps:
- Get the SharePoint site object.
- Get the web application object of the site object.
- Get the properties collection of the web application object.
- Add or update the property in the properties collection.
- Set the properties collection of the web application object.
# Get the SharePoint site object.
$site = Get-SPSite "<SiteUrl>"
# Get the web application object of the site object.
$webApplication = $site.WebApplication
# Get the properties collection of the web application object.
$properties = $webApplication.Properties
# Add or update the property in the properties collection.
$properties.Add("Property1", "Value1")
# Set the properties collection of the web application object.
$webApplication.Properties = $properties<div class=""><div> <p>To get the property value, you can use the following PowerShell command:</p><div class=""><div class=""><li-code lang="powershell"># Get the property value.
$propertyValue = $webApplication.Properties["Property1"]<p>Here is an example of a PowerShell script that gets and updates the Property1 property of the SharePoint site:</p><div class=""><div class=""> <div class=""><li-code lang="powershell"># Get the SharePoint site object.
$site = Get-SPSite "<SiteUrl>"
# Get the web application object of the site object.
$webApplication = $site.WebApplication
# Get the properties collection of the web application object.
$properties = $webApplication.Properties
# Add or update the property in the properties collection.
$properties.Add("Property1", "Value1")
# Set the properties collection of the web application object.
$webApplication.Properties = $properties
# Get the property value.
$propertyValue = $webApplication.Properties["Property1"]
# Write the property value to the console.
Write-Host "Property1 value: $propertyValue"<p>To use this script, replace <<SiteUrl>> with the URL of the SharePoint site. You can also modify the Property1 property name and value as needed</p>
pratiksavla
Oct 10, 2023Copper Contributor
Hi LeonPavesic
We have already used the SPSite.WebApplication.Properties as mentioned in the original post with Add/Remove functions. But we are unable to get the values set by powershell commands in C# code..
- LeonPavesicOct 10, 2023Silver Contributor
Hi pratiksavla,
To ensure that the properties are available in your C# code, you may need to perform an IISRESET (Internet Information Services Reset) on the SharePoint server after setting the properties using PowerShell. This will refresh the server and clear any caches, making the properties available to your C# code.
Here's how you can perform an IISRESET:
Open Command Prompt or PowerShell with administrator privileges on the SharePoint server.
Run the following command to perform an IISRESET:
iisreset /noforce
This will restart IIS and refresh the SharePoint server.
After performing the IISRESET, try accessing the properties in your C# code using SPSite.WebApplication.Properties again. The values set via PowerShell should now be available.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)- pratiksavlaOct 10, 2023Copper Contributor
Hi LeonPavesic
We tried performing IISReset but it did not solve the issue.
Although using following code we were able to set the property via powershell and get the property in C# code:
$site = GetSPSite '<siteurl>' $site.WebApplication.Properties.Add('Property1','Value1') $site.WebApplication.Update() $site.dispose()
But now when the property is set or updated using C# code we are not able to see the changes in powershell.