Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Wednesday, December 17, 2014

Item Update vs SystemUpdate

Recently there was a need to update documents/ list items without creating new version and updating 'Modified' and 'Modified by' values.  Lists and Libraries were having event receivers or workflows. There are couple of options to update SPListItem and need to understand the differences between them. 
Update()
·        Updates the item in the database.
·        Updates the 'Modified' and 'Modified by' values.
·        Creates a new version
Systemupdate()
·        Updates the item in the database.
·        No changes in the 'Modified' and 'Modified by' values.
·        No new version.
·        Triggers the item events.
Systemupdate(true)
·        Same as Systemupdate() and it increments item version.
·        Using SystemUpdate(false) is same as SystemUpdate().
UpdateOverwriteVersion()
·        Updates the item but does not create a new version.
·        Updates the  'Modified' and 'Modified by' values.
you could set EventFiringEnabled = false to disable the triggering of events (workflows). Set EventFiringEnabled = true after item update to enable the events again. 

Friday, October 31, 2014

Delete a Timer Job in SharePoint

I was looking for a custom timer job that was having two instances. Second instance got created after another deployment. Feature deactivation did not delete the existing job. I wanted to delete old timer job but there is not option to delete that in central Admin. PowerShell help me out on that. PowerShell’s Get-SPTimerJob command provides listing of all the timer jobs.

When I run this command it was truncating the name and ids. To see all jobs with full name we need to change the buffer Size property of PowerShell window to 250+

Get-SPTimerJob |Format-Table id,name

To narrow down the results I used where clause with name with these properties to distinguish old job.

Get-SPTimerJob | where { $_.name -like "<JobName>" }| Format-Table  -autosize -Property LastRunTime,id,name,DisplayName,Status,ErrorMessage

After finding the id of correct Job, run these commands to delete the job.


$job = Get-SPTimerJob -id <Job's GUID>
$job.Delete()

Job will be deleted. you can confirm running above Get-SPTimerJob PowerShell command or in CA. 

Thursday, September 18, 2014

Extract WSPs from Central Admin Solution Management

You can extract wsps from Central Admin Solution Management using PowerShell if you don't have binary or Setup exe.

$wsps = "Solution1.wsp;Solution2.wsp"
$path = "D:\Wsps\"

$farm = Get-SPFarm
$wspNames = $wsps.split(";",[StringSplitOptions]'RemoveEmptyEntries')
foreach($wspName in $wspNames) {
$file = $farm.Solutions.Item($wspName).SolutionFile
$file.SaveAs($path+$wspName)
Write-Host "Solution File extracted to this location " + $path$wspName
 }


Wednesday, April 16, 2014

ReDeploy Custom Timer Job using PowerShell

Following are the steps you need to redeploy a custom timer job.

Disable-SPFeature -Identity "91b81920-cc93-46da-b2c9-7e29c5077a67" -Url "http://sp2013.com/"
UnInstall-SPFeature -Identity "91b81920-cc93-46da-b2c9-7e29c5077a67"

Uninstall-SPSolution -Identity TimerJob.wsp –allwebapplications
Remove-SPSolution TimerJob.wsp –force

Add-spsolution C:\temp\TimerJob.wsp
Install-spsolution TimerJob.wsp -GACDeployment –force

Enable-SPFeature -Identity "91b81920-cc93-46da-b2c9-7e29c5077a67" -Url "http://sp2013.com/" –PassThru

Stop-Service SPTimerV4
Start-Service SPTimerV4

If you use update solution TimerJob may not pickup updated functionality
//Update-SPSolution -Identity timerjob.wsp -LiteralPath C:\temp\TimerJob.wsp -GACDeployment

GAC Install a DLL using PowerShell

Installed assembly into GAC -

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\temp\Mydll.dll")
iisreset

To Get assembly info-
([system.reflection.assembly]::loadfile("C:\temp\Mydll.dll")).FullName

Saturday, May 11, 2013

Install and uninstall SharePoint Apps using PowerShell


Microsoft introduced apps for Office and apps for SharePoint. You can develop Apps using visual studio office tools and NAPA. You can deploy app from visual studio deploy option or through PowerShell.
Here is how PowerShell can help you deploy Apps to different site collections.
To Install SharePoint App -
Before you can install an app, make sure that App is published to a physical folder using publish option. After publishing you will have a package file with .app extension (like yourApp.app).  
Installation of app is two steps process, first import app to a site collection and then install imported App.
Import-SPAppPackage -Path <Path of .app file> -Site < SiteCollectionURL > -Source < AppSource>
AppSource can be Marketplace, CorporateCatalog, DeveloperSite, ObjectModel, RemoteObjectModel.
Install-SPApp -Web <SPweb ID or URL> -Identity <SPApp>
Example:
$myApp = Import-SPAppPackage -Path "c:\Apps\MySharepointApp.app" -Site http://localhost/apps
 -Source ([microsoft.sharepoint.administration.spappsource]::ObjectModel)

Install-SPApp -Web http://localhost/apps -Identity $myApp
To Uninstall SharePoint App -
To uninstall an app first you need to get the instances of installed Apps then get the app you want to uninstall using app ID or app Title.
To get all installed apps in SharePoint site -
$appInstances = Get-SPAppInstance -Web < SiteCollectionURL >
To get the app details that you want uninstall -
$app = $appInstances | where {$_.Title -eq '< Title or ID of App >'}
To uninstall the app -
Uninstall-SPAppInstance -Identity $app
Example:
$appInstances = Get-SPAppInstance -Web http://localhost/apps
$app = $appInstances | where {$_.Title -eq ' Your SharePoint App'}
Uninstall-SPAppInstance -Identity $app

Thursday, November 17, 2011

How to save a site as template using PowerShell

Here is Powershell script to save a site as template.

$SiteUrl = "http://Sharepoint2010/site"
$Web=Get-SPWeb $SiteUrl
$Web.SaveAsTemplate("Template Name","Template Title","Template Description",1)

The last parameter of SaveAsTemplate() can be 0 or 1. 1 if you want to save the site with data else 0.
Make sure you open the SharePoint Management Shell as Administrator.

How to Hide a List using PowerShell

Last week i had a requirement to hide a list from user view. To achieve that, I used PowerShell to hide a list in SharePoint 2010 by setting hidden property of SPList Object to $true.

Here is the script -

#--- Update these variables -----
$SiteUrl = "http://Sharepoint2010/site"
$ListURL = "http://Sharepoint2010/site/Lists/ListToHide"
#-------------------------------------


$Web = Get-SPWeb $SiteUrl
$List = $spWeb.GetList($ListURL)
$List.Hidden = $true
$List.Update()

Wednesday, August 31, 2011

PowerShell script to list all Webs and Site Templates

Today I wanted to know site templates of all webs with in a site collection. Easiest way to get this is via PowerShell.
Here is the PowerShell script to get all Webs within a SiteCollection and it prints Site Template name along with Title and URL. 
Just update $siteUrl and you are good to go.

$siteUrl = "http://myserver.com/"
$site = Get-SPSite $siteUrl
foreach ($web in $site.AllWebs) { 
            $web | Select-Object -Property Title, Url, WebTemplate 
}
$site.Dispose()