Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

December 29, 2013

SHarePoint get List of Specific (Task, GenericList, DocumentLibrary, Announcements) Object Model C#



 SharePoint saves everything in format of list below are few type of Lists those we use frequently.
  • GenericList
  • DocumentLibrary
  • Survey
  • Announcements
  • Contacts
  • Tasks
Complete List Type template can be found here

Now some time it happens your clients come up with some requirement and you have to get all list of specific Type of SPList. its not big deal ;)

Suppose I have 20 list few are Task type few Announcement Type and few are generic list, Now I wants only those list whose Template Type is Announcement.


C# Code:
using (SPWeb oWeb = SPContext.Current.Site.OpenWeb("\RelaviteUrl",false))
            {
                foreach(SPList oList in oWeb.Lists)
                {
                    if (SPListTemplateType.Announcements == oList.BaseTemplate)
                    {
                        //(oList.Title);
                    }                
                }                
            }



Please share any issue or findings on this. 


--
Regards,
Praveen Pandit

September 21, 2013

Update Modified by and Created by in SharePoint using Powershell


Many times you have to update list with large number of items or some time we have to change Modified or created by names of all items, yeah we are aware about System.Update() method but if data has populated and no code action will take place and requirement says to update because we don't wants to confuse client and his customers so in very rare and after approval we execute it to update modified and created by names.

We can use below script to update thousands of list items in single shot. but take care once changes are done by PowerShell can not be roll back.

so to update sharepoint list items simply can save below Powershell script in a .PS1 file and run it with Elevated privileges in SharePoint server where you has permission to modify items.

PowerShell Code:
<# Created by : Praveen Pandit
   Purpose  : To update Modified and created by Names.
   Created Date : June, 2011
#>
     $targetUser=$oWeb.EnsureUser("domain\userid");  
     # in List Created by -Author and Modifed by -Editor
     $oItem["Author"] = $targetUser            
     $oItem["Editor"] = $targetUser  
     # You can add other fields here any field value data time etc....
     $oItem.Update()  
 



Please share any issue or findings on this.


--
Regards,
Praveen Pandit

July 22, 2013

PowerShell how to add New Filed / Column type number / integer choice / DropDown in SharePoint

"PowerShell  how to add New Filed / Column type number / integer choice / DropDown in SharePoint"

Here is another example for How PowerShell is Powerful I mean useful.

Export List of site/ subsites

 $site = Get-SPSite "http://ServerName/SiteCollection"  
 Set-content ".\urls.csv" "URL"  
  foreach ($web in $site.AllWebs) {  
     write-host "Web URL -->" $web.URL  
     AC ".\urls.csv" $web.URL  
   }   

#above code will a CSV with heading URL and list of sites (like below) with name 'urls.csv'
URL
http://servername/B
http://servername/A

///////////////////////////// Add Number or Integer Column ////////////////////////////

  $csv = Import-CSV ".\urls.csv"  
  $spFieldType = [Microsoft.SharePoint.SPFieldType]::Number  
  $spFieldName = "Worked Hours"     
 foreach ($siteURL in $csv) {  
   # Web URL  
   $web = Get-SPWeb -Identity $siteURL.url;  
   # SPList name  
    $list = $web.Lists["Tasks"]  
   if($list -ne $null)  
   {  
     write-host "`nList is" $list "on Site " $web.Title " " $web.url;  
     $YN= Read-host "Press Enter to Confirm or Press ctrl+c"  
     if($YN -eq $null)  
     {  
      write-host "Checking Existing Columns."  
     $TF=$true;  
     ForEach ($id in $fld)  
     {  
       if($id -match $spFieldName)  
       {  
         Write-host "Column already exist`n" -foregroundcolor yellow;  
         $TF=$false;  
       }        
     }  
       if($TF)  
       {  
      Write-host "......Creating Column.....";      
      #The new field is not required Filed, so the value of the third parameter is 0  
      $list.Fields.Add($spFieldName, $spFieldType, 0)  
      $list.Update()  
      Write-host "Adding Decimal two places formate"  
      $spField = $list.Fields.GetField($spFieldName)  
      $spField.DisplayFormat = "2"    
      $spField.Update()  
      Write-host " **Item Added** `n" -foregroundcolor green  
      }  
      }  
   }  
   else{Write-Host "No List Found"}  
 }  

/////////////////////////////////// Add DropDown or Choice Column ////////////////////////////////////

  $spFieldName = "My Column"  
 $Choices = New-Object System.Collections.Specialized.StringCollection  
 $Choices.Add("Fixed")  
 $Choices.Add("Not Fixed")  
 $csv = Import-CSV ".\urls.csv"  
 foreach ($siteURL in $csv) {  
   # Web URL  
   $web = Get-SPWeb -Identity $siteURL.url;  
   # SPList name  
    $list = $web.Lists["Projects"]  
   if($list -ne $null)  
   {  
     write-host "`nList is" $list "on Site " $web.Title " " $web.url;  
     $YN= Read-host "To Confirm Press Enter......."  
     if($YN -eq '')  
     {  
     #column of type 'Choice Column'  
     $fld  = $list.Fields  
     write-host "Checking Existing Columns."  
     $TF=$true;  
     ForEach ($id in $fld)  
     {  
       if($id -match $spFieldName)  
       {  
         Write-host "Column already exist`n" -foregroundcolor yellow;  
         $TF=$false;  
       }        
     }  
       if($TF)  
       {  
         Write-host "......Creating Column.....";  
         #Add Choice Field to list  
         $list.Fields.Add($spFieldName,[Microsoft.SharePoint.SPFieldType]::Choice,$FALSE,$FALSE,$Choices)  
          $list.Update()  
          Write-host " **Item Added** `n" -foregroundcolor green  
          $views = $list.Views["All Items"];  
          $views.ViewFields.Add($spFieldName);  
          $views.Update()  
        }  
     }  
   }  
   else{Write-Host "No List Found"}  
 }  

--
Regards,
Praveen Pandit