Search This Blog

Tuesday, 29 May 2012

SharePoint2010 Lists vs PowerShell Scripts

Hi All,

Here i have posted the various operations performed on SharePoint 2010 lists through powershell.

1. Create new custom list
$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://chesgh201tw1v:1000 -AssignmentCollection $spAssignment
# $SPTemplate = $SPWeb.ListTemplates["Custom List"]
# $SPWeb.Lists.Add("Custom List","Description",$SPTemplate)
$SPList = $SPWeb.Lists["Custom List"]
if ($SPList -ne ''){
 #  $SPList.OnQuickLaunch = $True
 #  $SPList.Update()
}
else
{
}
Stop-SPAssignment $SPAssignment

2. Delete a field from the list

            $web = Get-SPWeb -identity http://chesgh201tw1v:1605
$list = $web.Lists["TestList"]
$column = $list.Fields["RelatedColumn"]
$column.Hidden = $false
$column.ReadOnlyField = $false
$column.Update()
$list.Fields.Delete($column)
3. Remove the relationshipbehavior of the lookup field and remove the column from Indexed column

            $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1000");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Open Your List
$List = $OpenWeb.Lists["Child"];
$column = $list.Fields["sdfsdfsf"]
$column.RelationshipDeleteBehavior = "None"
$column.Indexed = $false
$column.Update()
$column.AllowMultipleValues  = $true
$column.Update()
$OpenWeb.Dispose();
$SPSite.Dispose()

4. Various Listitem manipulation

            try
{
    #Get Site
    $SPsite = Get-SPSite("http://chesgh201tw1v:1001") # $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1001");
    #Open you web
    $OpenWeb = $SpSite.OpenWeb();
    #Create Custom List
    $OpenWeb.Lists.Add("MyCustomListPS","Custom List craetion from Powershell",$OpenWeb.ListTemplates["Custom List"])
    #Open the List
    $List = $OpenWeb.Lists.TryGetList("MyCustomListPS")
    if($List -ne $Null)
    {
        $spListItem = $List.Items.Add()
        $spListItem["Title"] = "First Item"
        $spListItem["Yes_X002f_No"] = $false
        $spListItem["Choice"] = "0"
        $spListItem["Currency"] = 23
        $spListItem["DateTime"] = get-date
        $spListItem["Integer"] = 1
        $spListItem["Lookup"] = 1
        $spListItem["MultiChoice"] = 0,1
        $spListItem["Note"] = "Description"
        $spListItem["Number"] = 23
        $spListItem["Text"] = "Single line of text"
        $spListItem["Url"] = "http://google.com"
        $spListItem["user"] = $false
        $spListItem.Update()
    }
    $OpenWeb.Dispose();
    $SPSite.Dispose()
    write-host “Opeartion Completed Successfully: “ -foregroundcolor Green
}
catch [System.SystemException]
{
    write-host “Execution stopped due to: “+$_.Message -foregroundcolor Red
}
5. Lookup Field Creation

            $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1000");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Open Your List
$List = $OpenWeb.Lists["Child"];
#Get List Item from list Lookup (Parent)
$LookupList = $OpenWeb.Lists["Parent"]
$LookupListID = $LookupList.ID
#Add Field Lookup to list
$List.Fields.AddLookup("LookupField",$LookupListID,$FALSE)
$column = $list.Fields["LookupField"]
$column.LookupField = $LookupList.Fields["ParentColumn"].InternalName
$column.AllowMultipleValues  = $true
$column.Update()
#Update view
$Views = $List.Views["All Items"]
$Views.ViewFields.Add(“LookupField”)
$Views.Update()
$OpenWeb.Dispose();
$SPSite.Dispose()


6. MultiLineText Field updation

            $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1001");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Open Your List
$List = $OpenWeb.Lists["MYCustomList"];
$column = $list.Fields["Multi"]
$column.NumberOfLines = 120;
$column.Update()
$OpenWeb.Dispose();
$SPSite.Dispose()

7. Various operation on SharePoint 2010

            try
{
    #Get Site
    $SPsite = Get-SPSite("http://chesgh201tw1v:1001") # $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1001");
    #Open you web
    $OpenWeb = $SpSite.OpenWeb();
    #Create Custom List
    $OpenWeb.Lists.Add("MyCustomListPS","Custom List craetion from Powershell",$OpenWeb.ListTemplates["Custom List"])
    #Open the List
    $List = $OpenWeb.Lists.TryGetList("MyCustomListPS")
    if($List -ne $Null)
    {
        #Shows on Quicklaunch
        $List.OnQuicklaunch = $True
        $List.Update()
       
        write-host “Defining values for choice: “ -foregroundcolor Green
        #Define Choice Field
        $Choices = New-Object System.Collections.Specialized.StringCollection
        $Choices.Add("First Choice")
        $Choices.Add("Second Choice")
        $Choices.Add("Third Choice")
       
        write-host “start Adding Fields to list: “ -foregroundcolor Green
       
        #Add Yes/No Field to list
        $List.Fields.Add("Yes/No", "Boolean", $FALSE)
      
        #Add Choice Field to list
        # $List.Fields.Add("Choice",[Microsoft.SharePoint.SPFieldType]::Choice,$FALSE,$FALSE,$Choices)
        $List.Fields.Add("Choice","Choice",$FALSE,$FALSE,$Choices)
      
        #Add Currency Field to list
        $List.Fields.Add("Currency", "Currency", $FALSE)
      
        #Add DateTime Field to list
        $List.Fields.Add("DateTime","DateTime",$false)
        $List.Fields[“DateTime”].Description = “My DateTime Field”
      
        #Add Integer Field to list
        $List.Fields.Add("Integer", "Integer", $FALSE)
      
        # Get List Item from list Lookup (MyParentList)
        $LookupList = $OpenWeb.Lists["MyParentList"]
        $LookupListID = $LookupList.ID
        # Add Field Lookup to list
        $List.Fields.AddLookup("Lookup",$LookupListID,$FALSE)
      
        #Add Multi Choice Field to list
        $List.Fields.Add("MultiChoice","MultiChoice",$FALSE,$FALSE,$Choices)
      
        #Add Note Field to list
        $List.Fields.Add("Note", "Note", $FALSE)
       
        #Add Number Field to list
        $List.Fields.Add("Number", "Number", $FALSE)
      
        #Add TextField to list
        $List.Fields.Add("Text", "Text", $FALSE)
      
        #Add UrlHyperField Field to list
        $List.Fields.Add("Url","URL",$false)
        $List.Fields[“Url”].Description = “My UrlHyperField Field”
      
        # Add User Field to list
        $List.Fields.Add("User", "User", $FALSE)
      
        #Update view
        write-host “Updating View: “ -foregroundcolor Green
        $Views = $List.Views["All Items"]
        $Views.ViewFields.Add("Yes/No")
        $Views.ViewFields.Add("Choice")
        $Views.ViewFields.Add("Currency")
        $Views.ViewFields.Add("DateTime")
        $Views.ViewFields.Add("Integer")
        $Views.ViewFields.Add("Lookup")
        $Views.ViewFields.Add("MultiChoice")
        $Views.ViewFields.Add("Note")
        $Views.ViewFields.Add("Number")
        $Views.ViewFields.Add("Text")
        $Views.ViewFields.Add("Url")
        $Views.ViewFields.Add("User")
        $Views.Update()
       
        #Create view and add field
        $spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
        $spViewFields = New-Object System.Collections.Specialized.StringCollection    #Create string collection object
        $spViewFields.Add("Title")    #Add columns
        $spViewFields.Add("Yes/No") #Add columns
        $spViewFields.Add("Choice") #Add columns
        $spViewFields.Add("Currency") #Add columns
        $spViewFields.Add("DateTime") #Add columns
        $spViewFields.Add("Integer") #Add columns
        $spViewFields.Add("Lookup") #Add columns
        $spViewFields.Add("MultiChoice") #Add columns
        $spViewFields.Add("Note") #Add columns
        $spViewFields.Add("Number") #Add columns
        $spViewFields.Add("Text") #Add columns
        $spViewFields.Add("Url") #Add columns
        $spViewFields.Add("User") #Add columns
        $spViewName = "My View"        #Set view name
        $spListView = $List.Views.Add($spViewName, $spViewFields, $spViewQuery, 50, $True, $False, “HTML”, $False)    #Add view to list
        $spListView.DefaultView = $True       #Set view as default
        $spListView.Update()    #Update view to reflect changes in site
       
    }
    $OpenWeb.Dispose();
    $SPSite.Dispose()
    write-host “Opeartion Completed Successfully: “ -foregroundcolor Green
}
catch [System.SystemException]
{
    write-host “Execution stopped due to: “+$_.Message -foregroundcolor Red

}
8. Change Field type from Single Line of Text to Multi Line Text
                $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1001");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Open Your List
$List = $OpenWeb.Lists["MYCustomList"];
$oField = $List.Fields["TextField"];
$oField.Type = "Note";
$oField.Update();
$contentField = $List.Fields["TextField"];
$contentField.RichTextMode = "Compatible";
$contentField.Update();
$OpenWeb.Dispose();
$SPSite.Dispose()

No comments:

Post a Comment