Thursday 28 January 2016



How to Clear dbo.auditdata table from your SharePoint Content database table
Do NOT do this on a production server directly. Try to run in Development environment.
Logging 3-26-2013 2-49-14 PM
Change retention days 3  from 14 by using following PowerShell command:
Get-SPUsageDefinition  | ForEach-Object { Set-SPUsageDefinition $_ –DaysRetained 3}
The next time the Usage Log File timer jobs run it’ll clean out everything more than 3 days old. If we want to manually trigger those jobs we can use this PowerShell:
Get-SPTimerJob | Where-Object { $_.title -like "*usage data*" } | Start-SPTimerJob

It will take some time to clear the Audit data table.

Another way:

Below is the script to delete records for particular site:

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$site = Get-SPSite -Identity "Your Site"

$date = Get-Date

$date = $date.AddDays(-3)

#Delete Audit logs

$site.Audit.DeleteEntries($date)



Sunday 24 January 2016

SharePoint 2013 Powershell script to update document library column (choice field category) when any folder created in Document Library

Scenario : My client wants to update a document library column(selected as choice category)  whenever a folder is added in document library he wants that folder name should be added in category menu also.

Below is the tested SharePoint 2013 script
--------------------------------------------------------------------------------------------------------

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$web=Get-spweb "Your site collection"

$list=$web.Lists.TryGetList("Your document library")

$i=0
$RootFolder=$web.GetFolder("Your document library")

foreach($folder in $RootFolder.SubFolders)
{

$ChoiceField = $list.Fields.GetField("Your column where category filed needs to be added") #Get the field
    if($folder.Name -ne "Forms")
      {
     
        $fname=  $folder.Name
     
       
if($ChoiceField.Choices.Contains($fname))
{
#write-host $ChoiceField.Choices.Item($i)
}
else
        {
       
        $ChoiceField.Choices.Add($fname)
        }

      }
      $i=$i+1
     
$ChoiceField.update()

}
----------------------------------------------------------------------------------------------------------------------
SharePoint 2013 PowerShell script to create sub folder in Document Library.

Note : Tested in SharePoint 2013

Script will get the date from server and create the sub folder in library named as server date.

Below is the script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Get the Web
$web=Get-spweb "Your Site Collection"
#Get the List/Library
$list=$web.Lists.TryGetList("Your Document Library Name")
$RootFolder=$web.GetFolder("Your Root Folder name")

$foldername = Get-Date -Format "yyyy-MM-dd"
 foreach( $folder in $RootFolder.SubFolders )
 {
    if($folder.name -ne "Forms")  
    {
    $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "$foldername");
  #write-host $Subfolder
  if ($Subfolder.Exists -eq $false)
   {
    #Create a Sub-Folder Inside "Sales Documents"
    $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$foldername")
    $Subfolder.Update();
    Write-host "Sub-Folder created successfully"
   }
    else
   {
     write-host "Sub-Folder already exists"
   }
    }


 }