Search This Blog

Monday, July 27, 2015

SharePoint CRUD Operations on ListItem PropertyBag

# Initialize the SharePoint PowerShell environment
Add-PSSnapIn "Microsoft.SharePoint.Powershell" -ErrorAction:SilentlyContinue



# Function to perform add , delete , view and update operation on list item properties
# Parameters :
# -- ItemBagKey : Name of property of list item
# -- Operation : Type of operation  "ADD" , "UPDATE" , "DELETE" , "VIEW"
# -- NewValue : Value for the item property 
# -- Item : List item to operate upon 


Function UpdateKey{
      param([string]$ItemBagKey,[string]$Operation,[string]$NewValue,[Microsoft.SharePoint.SPListItem]$item)
      if($Operation -eq "ADD"# If we are adding property to item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Key already found in item bag." -ForegroundColor Yellow;
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key update with new value : "  $NewValue - ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Yellow;
                  $item.Properties.Add($ItemBagKey,$NewValue);
                  $item.SystemUpdate($false);
                  Write-Host "Key added with value : "  $NewValue -ForegroundColor Green;
            }
      }
      elseif($Operation -eq "DELETE"# If we are deleting property from item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties.Remove($ItemBagKey);
                  $item.SystemUpdate($false);
                  Write-Host "Key removed from item bag : "  $ItemBagKey -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag to remove." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "UPDATE"# If we are updating property of item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key updated with new value : "  $NewValue -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "VIEW"# If we want to view value item property.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Property value : "  $item.Properties[$ItemBagKey] -ForegroundColor Yellow;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
}

try
{
     
      #Put your site URL in placeholder <siteurl> 
      $web = Get-SPWeb <siteurl>     

      #Put list name in placeholder <listname> 
      $list = $web.Lists["<listname>"


      #If the list is found
      if($list)
      {
  
  #Put list item id in placeholder <itemid> 
  $item = $list.GetItemById(<itemid> );


  #Put Item property key that you want to operate upon in placeholder               <propkey> 
  $_ItemBagKey = "<propkey>"


  #Replace placeholder <OP> with type of operation you want to perfrom it            could be one among four values ( ADD, UPDATE , DELETE , VIEW )
  $_Operation  = "<OP>"  # "ADD" # "UPDATE" # "DELETE" # "VIEW"


       #Replace placeholder <propvalue> with new value that you want to update          if you want to delete or view just leave the value blank.
       $_NewValue   = "<propvalue>"
  

 #call function to perform operation
 UpdateKey $_ItemBagKey $_Operation $_NewValue $item


 #Dispose web object to release resources.
 $web.Dispose()

}
else
{
   Write-Host "List not found." -ForegroundColor Red;
}
}
catch
{
   # Add your exception handling routine here to trap and handle the exception details.
}






No comments:

Post a Comment