Thursday, 29 October 2015

Migrate Custom View along with its Fields/Columns from one Sharepoint List to Other.

Add-PSSnapin Microsoft.SharePoint.PowerShell

$SPSourceWeb = Get-SPWeb "http://server:port/SourceSite" # Source Site
$SPSourceList = $SPSourceWeb.Lists["Source List Name"]
$SPSourceListViewSchema = $SPSourceList.Views
$SPSourceListViewSchema = $SPSourceList.Views["Custom View"].SchemaXml
Write-Output $SPSourceListViewSchema > D:\View\CustomView.xml

$ViewTemplatePath = "D:\AutoDeployment\CustomView.xml"
$ViewTemplateXML = [xml](get-content $ViewTemplatePath)

$SPDestSites = @("<Destination Site URL 1>","<Destination Site URL 2>","<Destination Site URL N>")
$Count = 0
Write-Output "WorkFlow Task View" > D:\View\CustomViewErrorLog.txt
foreach($SPDestSite in $SPDestSites)
{
    try
    {
        $SPDestWeb = Get-SPWeb -Identity $SPDestSite
    
        $SPDestList = $SPDestWeb.Lists["Destination List Name"]
        $Viewfields = $WFTaskViewTemplateXML.View.ViewFields.FieldRef.Name
        $viewRowLimit = "100"
        $viewPaged = $true
        $viewDefaultView = $false

        # Setting the Query for the View
         $viewQuery = ""
         $viewName = "Workflow Task View"

        # Finally – Provisioning the View
         $myListView = $SPDestList.Views.Add($viewName, $viewFields, $viewQuery, $viewRowLimit, 
         $viewPaged,    $viewDefaultView)

        # Update the View for changes made to the view
         $myListView.Update()
         $SPDestList.Update()
         $SPDestWeb.Dispose()
    }
    catch
    {
   Write-Output "$($SPDestSite) ==> Exception: $($_.Exception.Message)" > D:\View\CustomViewErrorLog.txt
    }

Update Sharepoint Site Logo through Powershell

Add-PSSnapin Microsoft.SharePoint.PowerShell

# get your site
$myApp = Get-SPWeb "http://server:port"

# add an logo image
$myApp.SuiteBarBrandingElementHtml = '<SharePoint:SPLinkButton CssClass="ms-siteicon-a"  runat="server" NavigateUrl="http://server:port" id="onetidProjectPropertyTitleGraphic"><SharePoint:SiteLogoImage CssClass="ms-siteicon-img" name="onetidHeadbnnr0"  id="onetidHeadbnnr2" LogoImageUrl="http://server:port/sites/Subsite/small- logo.png.png?rev=23" runat="server" href=""/></SharePoint:SPLinkButton>'

# update
$myApp.Update() 

Add/Update/Delete of Items in a List/Library

Adding an Item

$webUrl = “http://server:port/”
   $listName = “List Name”
             $SPWeb =  Get-SPWeb -Identity $webUrl 
            $SPList= $SPWeb.Lists["$listName"]
$SPItem = $SPList.Items.Add();
$SPItem["Title"] = "Demo"
$SPItem["SiteURL"] = "http://powershell/sites/shell.com"
$SPItem.Update() 


Update an Item  by its ID in List

$webUrl = “http://server:port/”
   $listName = “List Name”
             $SPWeb =  Get-SPWeb -Identity $webUrl 
            $SPList= $SPWeb.Lists["$listName"]
$SPItem = $SPList.Items | where {$_['ID'] -eq '15'
$SPItem["Title"] = "Demo"
$SPItem["SiteURL"] = "http://powershell/sites/shell.com"
$SPItem.Update() 


Delete an Item  by its ID in List

$webUrl = “http://server:port/”
   $listName = “List Name”
             $SPWeb =  Get-SPWeb -Identity $webUrl 
            $SPList= $SPWeb.Lists["$listName"]
$SPItem = $SPList.Items | where {$_['ID'] -eq '15'
$SPItem.Delete() 

Back up and Restoration of Farm,Site Collections, Subsites, Content DB.

Ø Farm backup 
Backup-SPFarm –Directory \\App01\SharePointBackups -BackupMethod Full 
Restore-SPFarm –Directory \\App01\SharePointBackups -RestoreMethod New


Ø Back up and restore a service application:
Backup-SPFarm –Directory \\App01\SharePointBackups -BackupMethod Full –Item "Excel Services" 
Restore-SPFarm –Directory \\App01\SharePointBackups -RestoreMethod New –Item "Excel Services"


Ø Back up and restore your SharePoint content databases:
Backup-SPFarm –Directory \\App01\SharePointBackups -BackupMethod Full –Item ContosoPortal 
Restore-SPFarm –Directory \\App01\SharePointBackups -RestoreMethod New –Item ContosoPortal


Ø Back up and restore a site collection:
Backup-SPSite –Identity http://App01/Sites/ContosoPortal -Path \\App01\SharePointBackups\PortalSiteCollection.bak -Force 


Restore-SPSite –Identity http://App01/Sites/ContosoPortal -Path \\App01\SharePointBackups\PortalSiteCollection.bak –Force


Ø Export and import a subsite, list, or library:
Export-SPWeb –Identity http://App01/Sites/ContosoPortal/ -Path \\App01\SharePointBackups\SharedDocuments.bak -Itemurl "Shared Documents" -Force 


Import-SPWeb –Identity http://App01/Sites/ContosoPortal/ -Path \\App01\SharePointBackups\SharedDocuments.bak –Force -IncludeUserSecurity