CSOM – How to set “default column value settings” for a document library

In this blog I will describe about the option to set the column default value settings. SharePoint 2013 document libraries have the option to set the default column values at the folder level.  Assume that I have a document library with the below folders.

csom-1

 

 

 

 

 

 

 

 

Whenever the document is uploaded to the SharePoint folder the uploaded document needs to be automatically tagged with the SharePoint metadata. The column can be set with the default values for each folder. Without getting the user to tag manually the document can be automatically tagged with the default value. This can be done under Library Settings with Column default value settings option.

csom-2

 

When the default values are assigned, the view will be shown as below:

csom-3

 

Once the values are assigned the document library will save the value in an Xml format under the forms directory. The values are saved in the client_LocationBasedDefaults.html. Below is the xml format of the value saved in the file.

<MetadataDefaults>

    <a href="/ESPC15/Shared%20Documents/Microsoft">

        <DefaultValue FieldName="Brand_x0020_Name">Microsoft</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Others">

        <DefaultValue FieldName="Brand_x0020_Name">Others</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Microsoft/ASPNET">

        <DefaultValue FieldName="Technology">12;#ASPNET|c59a6216-895a-4c04-8377-63b5f9f78ed2</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Microsoft/Azure">

        <DefaultValue FieldName="Technology">13;#Azure|add6b6fe-358e-4e37-a15b-90ab58ac153e</DefaultValue>

    </a>

</MetadataDefaults>

If you notice above the values are stored as an anchor tag with the metadata Taxonomy Term Id and the GUID of the particular Term. So to assign the term to other folders we can just add the xml node to this file and upload it to the forms directory of the document library.

CSOM approach to add default values

Using CSOM PowerShell script we can download the file from the forms folder of the document library to save it to local drive. Then the local file can be modified to add the nodes needed.

Below are the steps followed for the CSOM PS
Step 1: Connect to the document library using the PS CSOM script

Step 2: Download the file client_LocationBasedDefaults.html from the forms directory to save it to local folder to change

Step 3: Change the file to add the details needed, in my case added the <a> tag for the SharePoint folder to set Term

Step 4: Upload back to the document library forms folder

Step 1 & 2

Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"



$username = "name@domain.com"

$password = "<password>"

$destUrl = "<url>" ## https://site

$srcLibrary = "Documents"



$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)



Write-Host "connecting..."

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($destUrl)

$ctx.Credentials = $credentials

$srcWeb = $ctx.Web

$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)

$ctx.Load($srcWeb)

$ctx.Load($srcList)

$ctx.ExecuteQuery()

Write-Host "connected"



#### Step 1: download the html file

        # Get forms folder in library

        $formsFolder = $srcList.RootFolder.Folders.GetByUrl("Forms")

        $ctx.Load($formsFolder)

        $ctx.ExecuteQuery()



        # Get client_LocationBasedDefaults.html file in forms library

        $LocationBasedDefaultsXML = $formsFolder.Files.GetByUrl('client_LocationBasedDefaults.html')

        $ctx.Load($LocationBasedDefaultsXML)

        $ctx.ExecuteQuery()



        # Download file

        $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $LocationBasedDefaultsXML.ServerRelativeUrl)

        $fstream = New-Object System.IO.FileStream("C:\client_LocationBasedDefaults.html", [System.IO.FileMode]::Create);

        $fileInfo.Stream.CopyTo($fstream)

        $fstream.Flush()

        $fstream.Close()

    Write-Host "Downloaded the file successfully"

### download the file complete

 

Step 3

Example if you want to set the default term for SharePoint folder add the below entry to the file downloaded

<a href="/ESPC15/Shared%20Documents/Microsoft/SharePoint">

        <DefaultValue FieldName="Technology">16;#SharePoint|36079b03-5612-4cc2-86b8-178061d83ee0</DefaultValue>

 </a>

If you want to find the term id and the GUID you can use the F12 dev window at the internet explorer on the term store.

 

csom-4

 

 

Step 4:

Once the file client_LocationBasedDefaults.html is modified with the added term the final file will look like this

<MetadataDefaults>

    <a href="/ESPC15/Shared%20Documents/Microsoft">

        <DefaultValue FieldName="Brand_x0020_Name">Microsoft</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Others">

        <DefaultValue FieldName="Brand_x0020_Name">Others</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Microsoft/ASPNET">

        <DefaultValue FieldName="Technology">12;#ASPNET|c59a6216-895a-4c04-8377-63b5f9f78ed2</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Microsoft/Azure">

        <DefaultValue FieldName="Technology">13;#Azure|add6b6fe-358e-4e37-a15b-90ab58ac153e</DefaultValue>

    </a><a href="/ESPC15/Shared%20Documents/Microsoft/SharePoint">

    <DefaultValue FieldName="Technology">16;#SharePoint|36079b03-5612-4cc2-86b8-178061d83ee0</DefaultValue>

</a>

</MetadataDefaults>

 

Now upload back the file to the same location to set the values. The below script will upload back the file to the folder.

 

Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"



$username = "name@domain.com"

$password = "<password>"

$destUrl = "<url>" ## https://site

$srcLibrary = "Documents"



$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)



Write-Host "connecting..."

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($destUrl)

$ctx.Credentials = $credentials

$srcWeb = $ctx.Web

$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)

$ctx.Load($srcWeb)

$ctx.Load($srcList)

$ctx.ExecuteQuery()

Write-Host "connected"



Write-Host "uploading..."

$root = $srcList.RootFolder

$ctx.Load($root);

$ctx.ExecuteQuery()

$folder = $root.Folders

$ctx.Load($folder);

$ctx.ExecuteQuery()

foreach($f in $folder)

{

$ctx.Load($f)

$ctx.ExecuteQuery()

Write-Host $f.Name

if($f.Name -eq "Forms")

{

Write-Host "Inside " $f.Name

$fci = New-Object Microsoft.SharePoint.Client.FileCreationInformation

$fci.Content = [System.IO.File]::ReadAllBytes("C:\client_LocationBasedDefaults.html");

$fci.Url = "client_LocationBasedDefaults.html";

$fci.Overwrite = $true;

$fileToUpload = $f.Files.Add($fci);

$ctx.Load($fileToUpload);

$ctx.ExecuteQuery()

Write-Host "Uploaded .. "

}

}

Write-Host "End Script"











 

Share this on...

Rate this Post:

Share:

Topics:

General

Tags: