Wednesday, 11 March 2015

Powershell for adding Custom list form of a list

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$Url = "http://sp2013:5559" #Please remove trailing slash if any

$listname ="TestList"
$web = get-spweb $Url
$list = $web.lists[$listname]
$files = $list.rootfolder.files






$editformurl = $list.RootFolder.ServerRelativeUrl + "/CustEditForm.aspx"
$dispformurl = $list.RootFolder.ServerRelativeUrl + "/CustDispForm.aspx"
$newformurl = $list.RootFolder.ServerRelativeUrl + "/CustNewForm.aspx"


$dispform = $files.add($dispformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)
$editform = $files.add($editformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)
$newform = $files.add($newformurl, [Microsoft.SharePoint.SPTemplateFileType]::FormPage)

$wpm = $editform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wpm2 = $dispform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wpm3 = $newform.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$lfw1 = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart])
$lfw2 = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart])
$lfw3 = new-object ([Microsoft.SharePoint.WebPartPages.ListFormwebpart])



$ilist1 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw1)
$ilist2 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw2)
$ilist3 = [Microsoft.SharePoint.WebPartPages.IListWebPart]($lfw3)


$ilist1.ListId = $list.id
$ilist1.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_EDITFORM;

$ilist2.ListId = $list.id
$ilist2.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_DISPLAYFORM;

$ilist3.ListId = $list.id
$ilist3.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_NEWFORM;


$lfw1.Hidden ="True"
$lfw2.Hidden ="True"
$lfw3.Hidden ="True"
$lfw1.ChromeType ="None"
$lfw2.ChromeType ="None"
$lfw3.ChromeType ="None"

$wpm.AddWebPart($lfw1, "Main", 1)
$wpm2.AddWebPart($lfw2, "Main", 1)
$wpm3.AddWebPart($lfw3, "Main", 1)





$list.DefaultDisplayFormUrl = $dispformurl
$list.DefaultEditFormUrl = $editformurl
$list.DefaultNewFormUrl = $newformurl



$list.update()



$SiteURL =$Url
#---------------Test Page----------------------------
$PageName="Rooms/CustNewForm.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
 



$PageName="TestList/CustDispForm.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
 


$PageName="TestList/CustEditForm.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName}
 

Powershell for Editing Web.config to enable sessionState(inproc)

# Load SharePoint PowerShell PSSnapIn and the main SharePoint .net library
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#set a few variables for the script
$owner = "WebConfigModifications"
$Url = "http://sp2013:5559/"

#Get the web application we want to work with
$webapp = get-spwebapplication $Url
#$webApp.WebConfigModifications | Format-Table Name,Owner
#$webApp.WebConfigModifications.Clear()


#get the Foundation Web Application Service (the one that puts the content web apps on servers)
$farmservices = $webapp.Farm.Services | where { $_.TypeName -eq "Microsoft SharePoint Foundation Web Application" }

 write-host $farmservices


 $newModification = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$newModification.Path = "configuration/system.web/pages"
$newModification.Name = "enableSessionState"
$newModification.Sequence = 0
$newModification.Owner = $owner
$newModification.Type = 1   #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$newModification.Value = "true"
$webapp.WebConfigModifications.Add($newModification)


$newModification = new-object Microsoft.SharePoint.Administration.SPWebConfigModification
$newModification.Path = "configuration/system.webServer/modules"
$newModification.Name ="add[@name='Session']"
$newModification.Sequence = 0
$newModification.Owner = $owner
$newModification.Type =0  #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$newModification.Value = "<add name='Session' type='System.Web.SessionState.SessionStateModule' />"

#add our web config modification to the stack of mods that are applied
$webapp.WebConfigModifications.Add($newModification)
$webapp.Update()

#trigger the process of rebuildig the web config files on content web applications
$farmServices.ApplyWebConfigModifications()

Powershell for adding webpart pages

#Check and add SharePoint PowerShell snap-in
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$web = Get-SPWeb http://hd-spdev02:5559 #Please remove trailing slash if any
$list = $web.GetList("http://hd-spdev02:5559/SitePages/Forms/")
$layoutTemplate = 4  # Template code




function Add-WebPartPage($pageTitle)
{
$xml = "<?xml version=""1.0"" encoding=""UTF-8""?><Method ID=""0,NewWebPage""><SetList Scope=""Request"">" + $list.ID + "</SetList><SetVar Name=""Cmd"">NewWebPage</SetVar><SetVar Name=""ID"">New</SetVar><SetVar Name=""Type"">WebPartPage</SetVar><SetVar Name=""WebPartPageTemplate"">" + $layoutTemplate + "</SetVar><SetVar Name=""Overwrite"">true</SetVar><SetVar Name=""Title"">" + $pageTitle + "</SetVar></Method>"
#$xml = "<?xml version=""1.0"" encoding=""UTF-8""?><Method ID=""0,NewWebPage""><SetList Scope=""Request"">" + $list.ID + "</SetList><SetVar Name=""Cmd"">NewWebPage</SetVar><SetVar Name=""ID"">New</SetVar><SetVar Name=""Type"">WebPartPage</SetVar><SetVar Name=""WebPartPageTemplate"">" + $layoutTemplate + "</SetVar><SetVar Name=""Overwrite"">true</SetVar><SetVar Name=""Title"">" + $pageTitle1 + "</SetVar></Method>"
$result = $web.ProcessBatchData($xml)
}


#Test Form
$pageTitle = "Test"
Add-WebPartPage($pageTitle)



Powershell for uploading documents in Document library

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Script settings

$Url = "http://sp2013:5559/"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared Documents"

$floorLayoutImagesPath = "C:\Images"

#Open web and library

$web = Get-SPWeb $Url

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $floorLayoutImagesPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    try
    {
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"

    #Close file stream
    $fileStream.Close();
    }
    catch
    {
 
        continue;
     
    }
}

#Dispose web

$web.Dispose()

Powershell for Installing WSP and activating feature

#Check and add SharePoint PowerShell snap-in
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

# Function to check if the timer job is complete
function WaitForJobToFinish()
{
    $JobName = "*solution-deployment*$solutionName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null)
    {
        Write-Output 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Output "Waiting to finish job $JobFullName"
       
        while ((Get-SPTimerJob $JobFullName) -ne $null)
        {
            Write-Output .
            Start-Sleep -Seconds 2
        }
        Write-Output  "Finished waiting for job.."
    }
}


$Url="http://sp2013:5959/"

$solutionPath="C:\Users\spadmin\Desktop\"

$solution="Test.wsp"
$path= $solutionPath + $solution
Add-SPSolution -LiteralPath $path
install-spsolution -Identity $solution  -GACDeployment -Force
WaitForJobToFinish

Enable-spfeature -identity "Test_Job" -confirm:$false -url $Url

Powershell for adding Menu item in Global Navigation in sharepoint 2013

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$Url = "http://sp2013:5559/"
$web = Get-SPWeb $Url;
 $CreateNavigationNodeMethod = [Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::CreateSPNavigationNode      

#Add Test4 link
         $headingNode = $CreateNavigationNodeMethod.Invoke("Test4","/SitePages/Test4.aspx", [Microsoft.SharePoint.Publishing.NodeTypes]::AuthoredLinkPlain, $web.Navigation.TopNavigationBar)
        $headingNode.Properties["Audience"]=";;;;" +"Test1"
     
        $headingNode.Update()

#Add Test3 link

        $headingNode = $CreateNavigationNodeMethod.Invoke("Test3","/SitePages/Test3.aspx", [Microsoft.SharePoint.Publishing.NodeTypes]::AuthoredLinkPlain, $web.Navigation.TopNavigationBar)
        $headingNode.Properties["Audience"]=";;;;" +"Test,Test1"      
        $headingNode.Update()



#Add Dropdown
        $Heading="Test Name Dropdown"
        $CreateNavigationNodeMethod = [Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::CreateSPNavigationNode      
           
        $headingNode = $CreateNavigationNodeMethod.Invoke($Heading, [System.String]::Empty, [Microsoft.SharePoint.Publishing.NodeTypes]::Heading, $web.Navigation.TopNavigationBar)
       
        $headingCollection = $headingNode.Children  
       
     

            $linkNode = $CreateNavigationNodeMethod.Invoke("Test Dropdown", "/_layouts/ApplicationPages/Test.aspx", [Microsoft.SharePoint.Publishing.NodeTypes]::AuthoredLinkPlain,$headingCollection)
            $linkNode.Properties["Audience"]=";;;;" +"Test2,Test3"
            $linkNode.Update()

         
     

       

       
         
           

Powershell for Adding Content Editor Webpart to a List Form in Sharpeoint 2010 & 2013

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$Url = "http://sp2013:5959" #Please remove trailing slash if any
$web = Get-SPWeb $Url
$web.AllowUnsafeUpdates=$true

#setting modal Dialog for all List Forms
foreach ($list in $web.Lists)
{
    $list.NavigateForFormsPages = $false;              
    $list.Update();
}


function Add-WebPartToPage($pageUrl, $webpartzone,$index,$fileName)
{
 
Write-Host("----------Adding Content Editor Webpart--------" +$filename)-ForegroundColor Yellow
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$errorMsg = "";
$lvwpGuid1 = [System.Guid]::NewGuid().ToString()
$lvwpKey = "g_" + $lvwpGuid1.Replace("-","_")
 
# Instantiate wp
$lvwp =New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart  
$lvwp.ID = $lvwpKey
$code = "$"
$lvwp.ContentLink = "/Style%20Library/TestFolder/Dialog.css"
$lvwp.Title = "Content Editor"  
$lvwp.Hidden ="True"  
$lvwp.ChromeType = "None"
$lvwp.HorizontalAlign = "Center"  
 
# Add the web part
$webpartmanager.AddWebPart($lvwp, "Main", 0);
 
$web.Update();


     
}


$SiteURL =$Url
#---------------Test Lists----------------------------
$PageName="Test/NewForm.aspx"
Add-WebPartToPage "$SiteURL/Lists/$PageName" "Header" 0 "Content Editor"

$PageName="Test/DispForm.aspx"
Add-WebPartToPage "$SiteURL/Lists/$PageName" "Header" 0 "Content Editor"

$PageName="Test/EditForm.aspx"
Add-WebPartToPage "$SiteURL/Lists/$PageName" "Header" 0 "Content Editor"