Skip to main content

SharePoint Script to update MMD Column in Library

This post contains the code to update Managed Metadata column value in Library.



Add-PSSnapin Microsoft.Sharepoint.Powershell
cls
#Log Variables
$basePath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $basePath


#SP variables
$web=$null ;
$list=$null;
$SiteURL = "https://mysharepointsite/en-us"# Library Site Url
$ListName = "Pages";# List Name
$FieldName="Document"
$termStoreName = "Managed Metadata Service Application"
$termGroupName = "Document_Content"
$termSetName = "DocumentName"
$MMDValueToUpdate = "Technical Doc"


#Pre-requisite checks
Try
{
    #Web Check
    $web = Get-SPWeb $SiteURL
    if($web -eq $null)
    {
      Write-Host "Cannot find an SPSite object that contains the following Id or Url: $SiteURL"  $_.Exception.Message
       Exit
    }

    #List Check
    $list = $web.Lists[$ListName]
    if($list -eq $null)
    {

       Write-Host "Cannot find a List with the Name: $ListName in the web  "  $_.Exception.Message
       Exit
    }

  }

Catch
{
    Write-Host  "Unable to connect to the Site, Please check the Url . Failed with error : " + $_.Exception.Message
    Exit
}

if($list -ne $null)
{
try
{

 #Create a Taxonomy Session
 $TaxonomySession = Get-SPTaxonomySession -Site $web.Site
 $TermStore = $TaxonomySession.TermStores[$termStoreName]
 $TermGroup = $TermStore.Groups[$termGroupName]
 $TermSet = $TermGroup.TermSets[$termSetName]

 #Below line of code will get the name, GUID and other properties from TermStore
 $Term = $Termset.Terms[$MMDValueToUpdate]

#This is the Library Item OOTB ID of the item which you want to update
 $item = $list.GetItembyID($ItemID)
 if($item.Count -gt 0)
 {
 $MMSField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields[$FieldName]
 #If value exists in TermStore
 if($Term)
 {
 $MMSField.setFieldValue($item,$Term)
 $Item.SystemUpdate()
 Write-Host "Updated value as: " $Term.Name -backgroundcolo Green
 }
 else
{
Write-Host "Could not find termstore properties for : " $MMDValueToUpdate  -backgroundcolo Red
}
}
catch
{
   Write-Host "Error : $_.Exception.Message" -foregroundcolor white -backgroundcolor Red
   return
}
}
else
{
   Write-Host "No List with Name "+$ListName

}

Comments

Popular posts from this blog

How to get lookup value(username) from people picker column using JavaScript(Client Object Model)?

Recently I faced an issue on fetching username from people picker column. Below is the code which can be used in such scenarios- // JavaScript source code function getUsers() { //Get the current client context ctx = new SP.ClientContext(webUrl); // Get Web web = ctx.get_web(); // Get List list = web.get_lists().getByTitle("My List"); camlQry = new SP.CamlQuery(); //Traverse through all files and folders deep and get some people picker fields like "Author" camlQry.set_viewXml('<View Scope=\'RecursiveAll\'>' + '<Query>' + '<ViewFields>' + '<FieldRef Name=\'' + colValuesEnum.Title + '\' />' + '<FieldRef Name=\'' + colValuesEnum.Author + '\' />' + '</ViewFields> ' + '</View>'); listItem = list.getItems(camlQry); ctx.load(listItem); ctx.executeQueryAsync(success, failed); } //Success CallBack Function fun

Angular JS Routing Step by Step using Visual Studio.

In this post, we will see step by step how to create an Angular JS Project using Visual Studio. This is for beginners only J Before we start, make sure ·         You have very basic understanding of AngularJS ·         Basic understanding of Routing ·         Beginner level knowledge on html5 ·         Hands-On using Visual Studio 1.       Open Visual Studio and click “new project”. Select “ASP.NET Web Application”. Enter Project Name, Location and Solution Name . 2.   Create an “ Empty ” ASP.NET Web Application as shown in below figure 3.  Open Solution Explorer, right click on the solution and click “Manage Nuget Packages” Install Angular.js Once installed, you will see all Angular.JS references in “Scripts” folder. 4.    In Solution Explorer, create a folder “views” and add three html pages as shown below: Below is the code for Index.html : <!DOCTYPE html> <html ng-app="app"> <