Friday 16 May 2014

Add/Delete list items

Quick script to add some items to a SharePoint list and specify the list items content type, and then delete those items

Clear-Host
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$web = get-spweb "https://collaboration-dev5.ioc-lab.local/Approvals/Apps/Marks/"
$lst = $web.GetList("https://collaboration-dev5.ioc-lab.local/Approvals/Apps/Marks/Lists/TopSponsorItems")

$ct = $lst.ContentTypes["Top Sponsor Item"]

for($i=190; $i -le 00; $i++){

$item = $lst.Items.Add();
$item["ContentTypeId"] = $ct.Id;

$item["Title"] = "Item $i"

$item["CustomID"] = $i;
$item["SectionGroup"] = "Brands"
$item["SponsorSection"]= "Brands"
$item["TopSponsor"]= "Coke"

$item.update();
Write-host "`tCreate item Title: $($item.Title)" -foregroundcolor red
}

#Delete items with 
foreach ($item in $lst.items)
{

    if($item["CustomID"] -gt 190){
    Write-host "`tdelete item id: $($item.id)" -foregroundcolor red
   
    $lst.getitembyid($item.id).Delete()
    }
}



$web.dispose();

Customize the SmallSearchInputBox

Sometimes you need to customize the OTB searchbox but without touching control templates or building out your own custom webpart control. This restriction commonly comes from environments in which you are restricted to just SharePoint Designer. To resolve this I would utilize JavaScript and CSS to just customize the look and feel of the SearchBox. the following is a native JS implementation to change the placeholder text and to rewire the the onblur event to use a new function that would reset the placeholder text to what we specified.

var spm= spm|| {};

if (typeof spm.searchBoxExtension == 'undefined' || spm.searchBoxExtension == null)
{
  spm.searchBoxExtension = {

    init: function() {

      var searchBox = document.getElementsByName("InputKeywords")[0];
      searchBox.value = "What are you searching for?";
      searchBox.onblur = this.customBlur;
    },

    customBlur: function(event) {
      if (this.value == '') {
        this.value = 'What are you searching for?';

        if (this.className.indexOf('s4-searchbox-QueryPrompt') == -1)
          this.className += this.className ? ' s4-searchbox-QueryPrompt' : 's4-searchbox-QueryPrompt';

        document.getElementById('ctl00_PlaceHolderSearchArea_ctl01_ctl04').value = '0'
      } else {
        document.getElementById('ctl00_PlaceHolderSearchArea_ctl01_ctl04').value = '1';

      }
    }
  }
}

(function(){
  _spBodyOnLoadFunctionNames.push("spm.searchBoxExtension.init");
})()

Thursday 8 May 2014

List All site Collections

This will list all the web application and some details about their site collections

Clear-Host
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

function GetSiteDetails()
{
    foreach ($site in Get-SPWebApplication | Get-SPSite)
    {
        $data = @{"Url" = $site.url
                  "Root Web" = $site.rootweb
                  "Template" = $site.rootweb.WebTemplate
                   "Web Application" = $site.webApplication
                  }
        New-Object PSObject -Property $data
    }
}


GetSiteDetails | Out-GridView

Friday 2 May 2014

Upload file to Document Library

Sometimes, you want to quickly upload a file to a document library, especially when you're using a sandboxed solution and you can't store your javascript or StyleSheet Files in the Layouts folder, long ago I mentioned that you could create a batch file that would xcopy your files into the layouts folder hastening the develop/debug cycle, well this is the same thing for Sandbox solutions.

function LoadSharePointPowerShellEnviroment
{
    write-host
    write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Magenta
    Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
    Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}

Clear-Host
LoadSharePointPowerShellEnviroment
write-host


Clear-Host
# Set the variables
$WebURL =  "https://stiteCollection/sites/Marks"
$DocLibName = "SiteAssets"
$FilePath = "C:\Users\SPDevFAdmin\Documents\Scripts\AppLoader.js"


$web = Get-SPWeb $WebURL
$List = $web.GetFolder($DocLibName)
$Files = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$File= Get-ChildItem $FilePath


Write-Host "uploading file $File " -ForegroundColor Yellow
$stream = $file.OpenRead()
     

# Upload it to SharePoint
$done = $Files.Add($DocLibName +"/" + $FileName, $stream,$true)

Write-Host "$done has been uploaded" -ForegroundColor Magenta

if($stream){
    Write-Host "dispose stream" -ForegroundColor Yellow
    $stream.Dispose()
}

if($web -ne $null){
    Write-Host "dispose web" -ForegroundColor Yellow
    $web.Dispose();
}