Tuesday, July 2, 2013

Column order for SharePoint 2010 ContentTypes using Powershell

 When you run below script it will add a field to content type and set the order. For my field I want to set the order next to ‘SAC Doc’ filed. For that I am reading existing index of ‘SAC Doc’ field and setting to my new field.

For column order you can refer this post as well



function SetColumnOrder([Microsoft.SharePoint.SPContentType]$contentType)
 {
            #Write-Host $contentType.name
            [Microsoft.SharePoint.SPFieldLinkCollection]$flinks = $contentType.FieldLinks;
               #array object
               $fieldsArray = New-Object 'System.Collections.Generic.List[string]';

          #Read all the InternalNames of the FieldLinks into a list
          foreach($fieldLink in $flinks){
               $fieldsArray.Add($contentType.Fields[$fieldLink.ID].InternalName);
          }
     
              $fieldchkboxInternalName =$contentType.Fields["SAC DOC"].InternalName
             # Write-Host $fieldchkboxInternalName
              $fieldDDLInternalName =$contentType.Fields["ddlTest"].InternalName
             
          if($fieldsArray.Contains($fieldchkboxInternalName)){

              $indexChkbox = $fieldsArray.IndexOf($fieldchkboxInternalName)
                    #Write-Host "checkbox index:" $indexChkbox
                   
                    $indexDDL = $fieldsArray.IndexOf($fieldDDLInternalName)
                     #Write-Host "ddlindex:"  $indexDDL
                     #Write-Host  "fieldsArraycount" $fieldsArray.Count
                   
                    if($indexChkbox -ne $indexDDL )
                {
                          $fieldsArray.RemoveAt($indexDDL)
                         
                          if($indexChkbox -le $fieldsArray.Count -1){
                           $fieldsArray.Insert($indexChkbox,$fieldDDLInternalName)
                                     Write-Host "SAC ddl index:"  $fieldsArray.IndexOf($fieldDDLInternalName)
                                     $msg = ("{0},{1}" -f "SAC ddl index order :", $fieldsArray.IndexOf($fieldDDLInternalName))
                                    $msg | out-file -FilePath $logfile  -append
                          }
                       #else{
                        #  $fieldsArray.Add($fieldDDLInternalName);
                        #}
                        }
             #Add the array to the reorder
             $flinks.Reorder($fieldsArray.ToArray());
             #Update the ContentYpe
             $contentType.Update($true)

            }
                             
}


$URL = "http://spdev5:8002/sites/library"
$site = New-Object Microsoft.SharePoint.SPSite($URL)
$web = $site.openweb("")


#logging
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "SAC_AddField2ContentTypes"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "S.No,Message"
$header | out-file -FilePath $logfile



$fieldName = "ddlTest"
$field = $web.Fields[$fieldName]
#$ct = $web.ContentTypes[$contentTypeName]
Write-Host "Fieldname:" $field
$index=40
$ContentTypes = $web.ContentTypes

 try{

      foreach ($ContentType in $ContentTypes)
      {
          $contentTypeGroup = $ContentType.Group
          $contentTypeName = $ContentType.Name

          if ( $contentTypeGroup  -like '*DMID*')
          {
              
                   #if($ContentType.Fields["SAC DOC"] -ne $null)
                   #{
                    #  write-host $ContentType.Name #$MyField.Id
                        #$MyField=$ContentType.Fields["SAC DOC"]
                      #$ContentType.FieldLinks[$MyField.Id].Required=$true
                  # }
                 
                  #if($ContentType.Name -eq  "Laboratory Normal Ranges")
                  if($ContentType.Name -eq "CI"  -or $ContentType.Name -eq "Lab" )
                  {    
                        if($ContentType.Fields["ddlTest"] -eq $null)
                        {
                              $link = new-object Microsoft.SharePoint.SPFieldLink $field
                              $ContentType.FieldLinks.Add($link)
                              $ContentType.Update($true)
                              write-host "'SAC Doc' dropdownlist added to: " $ContentType.Name
                              $msg = ("{0},{1}" -f "'SAC Doc' dropdownlist added to:", $ContentType.Name)
                              $msg | out-file -FilePath $logfile  -append
                              SetColumnOrder $ContentType
                              #break;    
                        }
                        else
                        {
                              write-host  "'SAC Doc' dropdownlist already added to: " $ContentType.Name -BackgroundColor Red
                              $msg = ("{0},{1}" -f "'SAC Doc' dropdownlist already added to:", $ContentType.Name)
                              $msg | out-file -FilePath $logfile  -append
                        }
                       
                  }
                 
          }
       }
 }
 catch [System.Management.Automation.ExtendedTypeSystemException]
 {
    Write-Host $_.Exception.ToString()
       $msg = ( "{0},{1}" -f "Error occurred while reading list..:", $_)
       $msg | out-file -FilePath $logfile  -append
 }

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





Friday, June 28, 2013

Create SharePoint 2010 Search Scope and Rules using Powershell

For instance, I have 3 site collections and all site collections under same database as shown below.
http://spdev5:8004 (root site colection 1)
http://spdev5:8004/sites/site2   (site collection 2)
http://spdev5:8004/sites/site3  (site collection 3)
By default when I search in the root site collection it displays results from all site collections. For ex if I search something in the root site collection it is showing data of site col2 and site col 3 as well. However if search anything in site col2 or site col3 results are limited to that site collection only. That’s fine.
Below script will create a scope in the central admin and you can see this one in the site collection as well.

In the scope I am including my root level site and excluding site collection2.


$siteURL="rootsitecolurl"
$searchServiceName="Search Service Application"
$scopeName="MyScope"
$docLibraryURL = “sitecoll2url”


 $ssa = Get-SPEnterpriseSearchServiceApplication –Identity $searchServiceName | New-SPEnterpriseSearchQueryScope -Name $scopeName -Description "Include root site collection and Exclude rest of the site collections"  -DisplayInAdminUI $true
$scope = Get-SPEnterpriseSearchQueryScope -SearchApplication $searchServiceName -Identity $scopeName
Write-Host $scope.NAME
$rule1 = New-SPEnterpriseSearchQueryScopeRule -RuleType "Url" -Url $siteURL -MatchingString $siteURL -FilterBehavior "Include" -UrlScopeRuleType "Folder" -scope $scope
$rule2 = New-SPEnterpriseSearchQueryScopeRule -RuleType "Url" -Url $docLibraryURL -MatchingString $docLibraryURL -FilterBehavior "Exclude" -UrlScopeRuleType "Folder" -scope $scope


Write-Host "Scope Created"

#This scope name (MyScope) you have to update in the root site collection search results page #‘OSSSearchResults.aspx’. just look for ‘Scope’ and update as ‘Scope= MyScope

# for more info 

Uncheck SharePoint 2010 Site Column type checkbox using Powershell

$site = New-Object Microsoft.SharePoint.SPSite("yoursiteurl")
$web = $site.openweb("")

$Library = "working documents"$DocLib = $web.lists[$Library]

Write-Host "title"  $DocLib

foreach ($item in $DocLib.items)
{

if($item["ARCHIVED"]) #if it is checked (Archived is a site col)
{
      write-host "Archived:"  $item["ARCHIVED"] $item.title       $item["ARCHIVED"] = "0" #Uncheck the checkbox
      $item.Update()
      write-host "Archived:" $item["ARCHIVED"]
}

}


$web.dispose()
$site.dispose()


Thursday, June 27, 2013

Enable Search for specific sharepoint 2010 list/library using Powershell

$site = New-Object Microsoft.SharePoint.SPSite("yoursiteurl")
$web = $site.openweb("")

$Library = "workingdocuments"
$DocLib = $web.lists[$Library]

Write-Host "title"  $DocLib
Write-Host  "search" $DocLib.NoCrawl
$DocLib.NoCrawl = $false #if it is false search will be enabled
Write-Host  "search enabled:" $DocLib.NoCrawl

$DocLib.update()


$web.dispose()
$site.dispose()


Wednesday, June 19, 2013

SPSecurityTokenService.Issue() failed: System.NullReferenceException: Object reference not set to an instance of an object

Basically it is looking for alternate access mapping. Below steps for creating alternate access mapping

Login to Central Admin.

1.            Go to Application Management.
2.            Under Web Applications click Configure alternate access mappings.
3.            Click on Add Internal URLs link on the tool bar. If you want to configure intranet or internet url’s click on Edit Public URLs
4.            In the URL protocol, host and port text box, type or verify the full URL of your site that is using the FBA Authentication.
5.            Set Zone to Default and save.


Friday, June 14, 2013

Export/Import SharePoint List/Library Powershell

#Export SharePoint List
Export-SPWeb  "http://spdev5:8005" -path "D:\\Development\surya\ResourceLibrary.cmp" -ItemUrl Resource%20Library -IncludeVersions All

#Import SharePoint List

Import-SPWeb -Identity http://spdev5:8003 -path "D:\Development\surya\ResourceLibrary.cmp"

Followers