Forum Discussion

pratiksavla's avatar
pratiksavla
Copper Contributor
Oct 09, 2023

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 property
site.WebApplication.Properties.Add("Property1", "Value1")
// get property
site.WebApplication.Properties["Property1]

We don't get any errors in above code.

 

Now, we want to get and update this property via powershell. 

We have already tried following commands:

1. Get-SPWeb

$site=Get-SPWeb -Identity "<SiteUrl>"
$site.Properties

2. Get-SPSite

$site=Get-SPSite "<SiteUrl>"
$site.WebApplication.Properties

3. Get-SPSite (OpenWeb())

$site=Get-SPSite "<SiteUrl>"
$web=$site.OpenWeb()
$web.Properties

 

Unfortunately, we were unable to retrieve or set the property using these PowerShell commands, which corresponds to the functionality used in the .NET code.

We need assistance in determining how to update or retrieve the property set using the mentioned code via PowerShell.

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi pratiksavla

    To get and update the property set using the mentioned code via PowerShell, you can use the following steps:

    1. Get the SharePoint site object.
    2. Get the web application object of the site object.
    3. Get the properties collection of the web application object.
    4. Add or update the property in the properties collection.
    5. 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's avatar
      pratiksavla
      Copper 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..

      • LeonPavesic's avatar
        LeonPavesic
        Silver 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:

        1. Open Command Prompt or PowerShell with administrator privileges on the SharePoint server.

        2. 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)

Resources