Removing an item from the property bag using CSOM

Some time ago I was working with the SharePoint property bag using CSOM (and JSOM) and I also needed to be able to remove the property from the property bag. There are some things to know before working with the property bag using CSOM, there are other people who have described this so I will link to an article I found on this Add and Retrieve property bag by CSOM

So now you know that adding an item to the FieldValues collection won't add the property to the property bag. The same is also true for removing an item from the FieldValues collection on the property bag, this won't remove the item from the property bag. But it is really easy to remove the property from the property bag, you just have to set the value to a null value, and it will be removed from the property bag. That is all there is to it. (I have been very naughty, and haven't compiled the code below, so please check it for your self.)


Web web = clientContext.Site.RootWeb;
clientContext.Load(web, w => web.AllProperties); // You need to retrieve the properties first
clientContext.ExecuteQuery();

// only remove it when the property is set
if (web.AllProperties.FieldValues.ContainsKey("YourImpressiveProperty")) 
{
    web.AllProperties["YourImpressiveProperty"] = null;
    web.Update();
    clientContext.ExecuteQuery();
}

I will demonstrate this in the following sample. For my convenience, and to make it more interesting I will use PowerShell and CSOM for this sample. For more information on PowerShell and CSOM, take a look at CSOM SharePoint PowerShell Reference and Example Codes.


# the path below depends on the location of the Client assembly on your system 
# the 15 root is for on premise, the 16 root for Office 365, I use on premise here
$path = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI"
Add-Type -Path "$path\Microsoft.SharePoint.Client.dll"


# your dev/demo environment site collection url
$url = "http://demo.dev.local/" 

#-------------------------
# Functions
#-------------------------

function OutputProperties($url) 
{
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    try 
    {
        $rootWeb = $context.Site.RootWeb
        $context.Load($rootWeb)
        $context.Load($rootWeb.AllProperties)
        $context.ExecuteQuery()

        # show properties
        $rootWeb.AllProperties.FieldValues
        Write-Host "-----------------------------------------------------------------------------------------"
    }
    finally 
    {
        if ($context -ne $null) {
            $context.Dispose()
        }
    }
}

function SetPropertyOnSite($url, $propertyName, $propertyValue)
{
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    try 
    {

        $rootWeb = $context.Site.RootWeb
        $context.Load($rootWeb)
        $context.Load($rootWeb.AllProperties)
        $context.ExecuteQuery()

        # set property
        $rootWeb.AllProperties[$propertyName] = $propertyValue
        $rootWeb.Update()
        $context.ExecuteQuery()

    }
    finally 
    {
        if ($context -ne $null) {
            $context.Dispose()
        }
    }
}

#-------------------------
# Code
#-------------------------


# set YourImpressiveProperty value
SetPropertyOnSite $url "YourImpressiveProperty" "Your cool value..."


OutputProperties $url


# remove YourImpressiveProperty from property bag, by setting the value to null
SetPropertyOnSite $url "YourImpressiveProperty" $null 


OutputProperties $url

If you put this in a .ps1 file and execute this, you will get something like the following output:

All Properties Sample Output

You can now see that after adding the property, it is in the property bag, see the selected line in the PowerShell window. After setting the value to null, the property was removed. I hope this was useful for you.