Friday, January 30, 2015

Delete Site Column and correnponding all references using PowerShell Script


Delete Site Column and correnponding all references using PowerShell Script:

Below Script is used to delete the Site Column and the corresponding References that uses the same Column:

# Add SharePoint PowerShell Snapin

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
    Add-PSSnapin Microsoft.SharePoint.Powershell
}


    $siteUrl = 'http://siteURL/'

    $fieldName = 'ArticleSorting'

    Write-Host “Start removing field:” $fieldName -ForegroundColor DarkGreen
    $site = Get-SPSite $siteUrl
    $web = $site.RootWeb

    #Delete field from all content types
    foreach($ct in $web.ContentTypes) {
        $fieldInUse = $ct.FieldLinks | Where {$_.Name -eq $fieldName }
        if($fieldInUse) {
            Write-Host “Remove field from CType:” $ct.Name -ForegroundColor DarkGreen
            $ct.FieldLinks.Delete($fieldName)
            $ct.Update()
        }
    }
    
    #Delete column from all lists in all sites of a site collection
    $site | Get-SPWeb -Limit all | ForEach-Object {
       #Specify list which contains the column
        $numberOfLists = $_.Lists.Count
        for($i=0; $i -lt $_.Lists.Count ; $i++) {
            $list = $_.Lists[$i]
            #Specify column to be deleted
            if($list.Fields.ContainsFieldWithStaticName($fieldName)) {
                $fieldInList = $list.Fields.GetFieldByInternalName($fieldName)

                if($fieldInList) {
                    Write-Host “Delete column from ” $list.Title ” list on:” $_.URL -ForegroundColor DarkGreen

                 #Allow column to be deleted
                 $fieldInList.AllowDeletion = $true
                 #Delete the column
                 $fieldInList.Delete()
                 #Update the list
                 $list.Update()
                }
            }
        }
    }

    # Remove the field itself
    if($web.Fields.ContainsFieldWithStaticName($fieldName)) {
        Write-Host “Remove field:” $fieldName -ForegroundColor DarkGreen
        $web.Fields.Delete($fieldName)
    }

    $web.Dispose()
    $site.Dispose()

#Delete the field below
DeleteField http://siteURL/ $fieldName

Getting site content types and site columns using PowerShell


Getting site content types and site columns using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell

## Variables

$WEB_APPLICATION_URL = "http://siteURL"
$CT_FIELDS_FILE_PATH = "c:\2013_CTs_And_Fields.csv"

## Initialization
echo "Initializing variables"

'"Site URL","Content Type","Field Name"' | Out-File $CT_FIELDS_FILE_PATH;

echo "Entering web application $WEB_APPLICATION_URL"
$webApplication = Get-SPWebApplication -identity $WEB_APPLICATION_URL
ForEach ($siteCollection in $webApplication.Sites)
{
    ForEach($subSite in $siteCollection.AllWebs)
    {
        $subsiteUrl = $subsite.Url;
        echo "Extracting info from $subSiteUrl";
        $contentTypes = $subSite.ContentTypes;
        ForEach ($contentType in $contentTypes)
        {
            ForEach ($field in $contentType.Fields)
            {

                '"'+ $subSiteUrl +  '","'  + $contentType.Name + '","' + $field.Title + '","' | Out-File $CT_FIELDS_FILE_PATH -Append;
            }
        }
    }
}
echo "Finished.....";


The script output is a set of three CSV files:
  1. Mapping between SiteURL, content types and fields

Monday, January 26, 2015

PowerShell to delete unused Crawl Properties

PowerShell to delete unused Crawl Properties:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"


$searchApp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"

$schema = New-Object Microsoft.Office.Server.Search.Administration.Schema –ArgumentList $searchApp

$category = $schema.AllCategories["SharePoint"];

$category.DeleteUnmappedProperties();  

$category.Update();  

Checking Site Column Names for Usage within a Site Collection

Checking Site Column Names for Usage within a Site Collection



if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) { 
  Add-PSSnapin Microsoft.SharePoint.PowerShell 

$site = Get-SPSite("http://siteURL/")  
$str = "ContentNavigation" 

foreach($web in $site.AllWebs)  { 
  foreach($list in $web.Lists)  {   
    foreach ($field in $list.Fields) { 
      if($field.InternalName.Contains($str) -or $field.Title.Contains($str))  {  
        Write-Host "'$web' web - Found a match in the '$field' field in the '$list' list"  
      } 
    } 
  } 

$site.dispose();

Friday, January 23, 2015

PowerShell to find SharePoint Content Types Usuage

PowerShell to find SharePoint Content Types Usuage:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$webs = get-spsite http://yourserver/sites/yoursite | get-spweb 


foreach ($web in $webs) 
{
  foreach ($lst in $web.lists) 
  { 
    foreach ($ctype in $lst.ContentTypes) 
    { 
      if ($ctype.Name -eq "Documents") 
      { $lst.DefaultViewUrl }
    }
  } 
  $web.Dispose() 
}


C#
Here’s a C# version to find all lists that support a particular content type:

using System;
using Microsoft.SharePoint;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite(http://yourserver/sites/yoursite);
            
            SPWebCollection webs = site.AllWebs;
            foreach (SPWeb web in webs) { 
                foreach (SPList lst in web.Lists) { 
                    foreach (SPContentType ctype in lst.ContentTypes) { 
                        if (ctype.Name == "Document") {
                            Console.WriteLine(lst.DefaultViewUrl);
                        } 
                    }
                }
                web.Dispose();
            }
            site.Dispose();
        }
    }

}

Thursday, January 22, 2015

Displaying List of Services running on each server on SharePoint Farm using PowerShell

Displaying List of Services running on each server on SharePoint Farm using PowerShell

foreach($server in $servers)
{
      $serverName = $server.DisplayName
      Write-Host $serverName -ForegroundColor "Black" -BackgroundColor "Yellow" 

      # Get SharePoint services running
      $servicesRunning = $server.ServiceInstances | Where{$_.Status –eq "Online" –and $_.Hidden –eq $False}
      Write-Host "SharePoint Services Running:" -ForegroundColor "Blue" -BackgroundColor "White"
      $servicesRunning | Select TypeName
      Write-Host "`n"
}


The option for the SharePoint 2013 Workflow platform is not available

Error Message:
"The option for the SharePoint 2013 Workflow platform is not available because the workflow service is not configured on the server. Please contact your server administrator"

Solution:
Here are the steps to fix this issue:
1. Make sure Workflow Manager 1.0 for SharePoint 2013 server is installed and configured properly.

2. Verify that all the following services are running: (To check service status, Press Windows key+R and type services.msc)

  • Workflow Manager Backend
  • Windows Fabric Host Service
  • Service Bus Gateway


3. Open “Sharepoint 2013 Management Shell” and run following command:

Enable-SPFeature -Identity WorkflowServiceStore –Url $siteUrl
replace $siteUrl with your site collection URL.

In Some cases, I was getting message the feature was already activated at that site.

4. Open IIS, Verify “Workflow Management Site” is working fine.
For testing, You can open following in your browser.

http://servername:12345

5. In “Sharepoint 2013 Management Shell”, run following

Register-SPWorkflowService –SPSite "http://myserver/mysitecollection" –WorkflowHostUri "http://workflow.example.com:12291" –AllowOAuthHttp

Friday, January 9, 2015

Web Part Deployment using PowerShell

Web Part Deployment PowerShell Script:

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
        Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
    } 

Add-SPSolution -LiteralPath C:\WebPartName.wsp

Install-SPSolution –Identity WebPartName.wsp –GACDeployment -CompatibilityLevel 15

Enable-SPFeature -identity "FeatureName" -URL http://test.com

Tuesday, January 6, 2015

Deleting Unused Webparts from Central Administrator


Deleting Unused Webparts from Central Administrator

http://yoursite/_catalogs/wp/Forms/AllItems.aspx there identify the webparts and delete those are not useful.
for customtemplate [it was created before Visual WebPart introduced] You can find it on your site's web config file.You comes to know the folder under _control template used to store the custome webpart.Delete those .ascx file and its. dll.You can find it on "BIN" folder of your site I mean [C:\Inetpub\Virtual Directories\PortNumber\BIN] and remove those entries from web.config and restart your IIS.