Wednesday, July 10, 2013

Copy 'Name' field as is to the 'Title' Field SharePoint 2010 Document library

When you run below script it will update the 'name' field as is to the 'Title' for all existing files in the document library.
dont forget to change your list name

Auto Fill Title of Document in Document Library in SharePoint 2010



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


#logging (it will create a .csv file where u r running this script)
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "CopyName2Title"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

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

$count=1
$fieldname="Title"
     
      foreach($list in $web.Lists)
      {
      if($list.BaseType -eq "DocumentLibrary")
        {
            $listname = $list.title
            if($list.title -eq "your list name") # update with your list name. if u remove this condition then it will apply to all libraires. 
            {
                  Write-Host $list.title
                  $msg = ("{0},{1}" -f "List:", "=========================== $listname ======================")
               $msg | out-file -FilePath $logfile  -append
           foreach($item in $list.items)
             {
                  $item[$fieldname] = $item["Name"]
                  $item.SystemUpdate();
                 
                  Write-Host $count". FileName:"   $item.url
                  $msg = ("{0},{1}" -f $count, "URL:" + $item.url)
                  $msg | out-file -FilePath $logfile  -append
                  $count++
              }
                  # $list.update()
         }
      }
   }
$web.Dispose()
$site.Dispose()

Tuesday, July 9, 2013

Check Column exist in SharePoint List using Powershell

Sometimes when we are working with SharePoint list items will get an error like

Column has been deleted by another user or not exist.

for this we can use below code to check column exist or not

 foreach($list in $web.Lists)
      {
         if($list.BaseType -eq "DocumentLibrary")
         {
              if( $list.Fields.ContainsField("ddlTest") -eq $true)
               {                                       
               Write-Host "found" $list.title
               }
               else
               {
                #  Write-Host "not found"  
               }
         }
      }

Monday, July 8, 2013

ArryaList in Powershell

$fieldsArray = $null
$fieldsArray = New-Object 'System.Collections.Generic.List[string]';
$fieldsArray.Add("DSMB-SMC Documents1")
$fieldsArray.Add("DSMB-SMC Documents2")
$fieldsArray.Add("DSMB-SMC Documents3")
     
 Write-Host "count" $fieldsArray.count
 foreach($fieldLink in $fieldsArray){
       Write-Host    $fieldLink
      }
       

              

Friday, July 5, 2013

CAML Query using PowerShell


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

$list=$web.Lists["TestDocs"]
$listname = $list.title
           
           
Write-Host $list.title
$spQuery = New-Object Microsoft.SharePoint.SPQuery



#$camlQuery ="<Where><Or><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>CV</Value></Eq><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>Form FDA 1572</Value></Eq></Or></Where>"

$camlQuery = '<OrderBy><FieldRef Name="Title" Ascending="True" /></OrderBy>'
Write-Host $camlQuery
$spQuery.Query = $camlQuery
$spQuery.RowLimit = 100
Write-Host "Query string" $spQuery.Query


$spListItems = $list.GetItems($spQuery)
Write-Host "spListItems :" $spListItems
  foreach ($item in $spListItems)
    {
      write-host "Name:" $item.Name
      write-host "Title:" $item["Title"]
    }

      

Wednesday, July 3, 2013

Add Field or Column to ContentType and set the column order



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

  http://sharepointempower.com/2013/03/column-ordering-in-sharepoint-2010/

It will create a log file in your location where you are running your script

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()




Followers