Sunday, March 23, 2014

Sharepoint - List all Site Level and Web Level features programmatically


The below sample is just for listing all site feature and current web level features.


static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://site collection url"))
            {
                Console.WriteLine("");
                Console.WriteLine("Site URL=" + site.Url);
                Console.WriteLine("");
                foreach (SPFeature f in site.Features)
                {
                    Console.WriteLine(f.Definition.Id + "======" + f.Definition.DisplayName + "======" + f.Definition.Parent);
                }


                using (SPWeb web = site.OpenWeb())
                {
                    Console.WriteLine("");
                    Console.WriteLine("\n\n Web URL=" + web.Url + "\n\n");
                    Console.WriteLine("");
                    foreach (SPFeature f in web.Features)
                    {
                        Console.WriteLine(f.Definition.Id + "======" + f.Definition.DisplayName + "======" + f.Definition.Parent);
                    }
                }
            }
            Console.WriteLine("");
            Console.WriteLine("Program Completed");

        }



If you want to display all web level features from all the subwebs, you need to write a recursive function to loop through all subsites and display the features.


Enable Developer Dashboard

$dash =[Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$dash.DisplayLevel = 'OnDemand';
$dash.TraceEnabled = $true;
$dash.Update() 


Disable/ Turn Off Developer Dashboard



$content = ([Microsoft.SharePoint.Administration.SPWebService]::ContentService)
$appsetting = $content.DeveloperDashboardSettings
$appsetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::Off
$appsetting.Update()

Sharepoint Powershell - Copy Column (Fields) to list

$web = Get-SPWeb http://siteURL           
$sourcelist = $web.Lists["SourceList"]
$destlist = $web.Lists["DetinationList"]

$columns=$sourcelist.fields



foreach($col in $columns)
{
 if( $destlist.fields.ContainsField($col))
               {                                      
           # Write-Host "found" $col
               }
               else
               {
              Write-Host "Field not found ="     $col, $col.Type
              Write-Host "Adding Field" 
              $destlist.fields.Add($col)
               }


}

Write-Host "Script Completed"