Monday, January 16, 2012

Powershell script update draft version Sharepoint 2010

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://yoursite.com/
$lookForList = "Shared documents"
#Walk through each site and change draft settings on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {
   
    write-host "Checking site:"$_.Title
   
    #Make sure the library exists
    $docLibrary = $_.Lists[$lookForList]
   
    if ($docLibrary -ne $null)
    {
        $docLibrary.DraftVersionVisibility = 1
        $docLibrary.Update()
 write-host $docLibrary.Title "on site" $_.Title "is updated"
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}
#Dispose of the site object
$site.Dispose()

Powershell script modify webpart on page Sharepoint 2010

$site = new-object Microsoft.SharePoint.SPSite("http://yoursite.com")
$web = $site.OpenWeb("RandomSite/AnotherSite/")
$page = $web.GetFile("Pages/default.aspx")
$page.CheckOut()
$wpm = $web.GetLimitedWebPartManager("Pages/default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wpm.WebParts | ft

$wp = $wpm.WebParts[1]
$wp.EnableRedirect = $false
$wp.Url = "http://www.google.com/"
$wpm.SaveChanges($wp)
$page.CheckIn("Test")
$page.Publish("Test")
$web.Close()
$site.Close()

Powershell script list all powershell commands in a text file

Get-Command –PSSnapin “Get-Command –PSSnapin “Microsoft.SharePoint.PowerShell” format-table name > C:\SP2010_PowerShell_Commands.txt

Powershell script install feature Sharepoint 2010

$MyFeatureId = $(Get -SPFeature -limit all where {$_.displayname -eq "myfeatureName"}).Id
Install -SPFeature $MyFeatureId

Powershell script activate developer dashboard on demand in your Sharepoint 2010 environment

$dd = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$dd.DisplayLevel = ‘On Demand’;
$dd.TraceEnabled = $true;
$dd.Update ();

Powershell script disable feature Sharepoint 2010 site collection

$singleSiteCollection = Get-SPSite -Identity http://yoursite.com/
Disable -SPFeature $MyFeatureId -Url $singleSiteCollection.URL

Powershell script change title of a document library in your Sharepoint 2010 site collection

#Change these variables to your site URL and list name
$site = Get-SPSite http://yoursite.com/
$listName = "Shared Documents"
#Walk through each site in the site collection
$site | Get-SPWeb | ForEach-Object {
#Get the list in this site
$list = $_.Lists[$listName]
#Make the list changes
$list.Title = "Documents"
$list.Description = "Use the Documents library to store shared files"
#Update the list
$list.Update()
}
#Dispose of the site object
$site.Dispose()

Powershell script add webpart to your Sharepoint 2010 sites in your site collection

$site = new-object Microsoft.sharePoint.SPSite("http://yoursite.com/")
$web=$site.OpenWeb()$pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)$defaultpage=$pubweb.GetPublishingPages()[$pubweb.DefaultPage]$defaultpage.CheckOut()$webpartmanager=$web.GetLimitedWebPartManager($defaultpage.Url,
[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)$webpart=new-object  Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
$webpart.ChromeType=[System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly;
$webpart.Title="Name of your Webpart"
$webpartmanager.AddWebPart($webpart, "Left", 0);$defaultpage.CheckIn("Checked in Webpart")
$defaultpage.listItem.File.Publish("Published Webpart")
if ($defaultpage.listItem.ParentList.EnableModeration)
{
   $modInformation = $defaultpage.listItem.ModerationInformation   if($modInformation.Status -ne [Microsoft.SharePoint.SPModerationStatusType]::Approved)
   {
      $defaultpage.ListItem.File.Approve("Approved Page")
   }
}

Powershell script delete content type from document library in your Sharepoint 2010 site collection

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://yoursite.com/
$lookForList = "Document”
$stringCTRemove = "Your Content type"
#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {
   
    write-host "Checking site:"$_.Title
   
    #Check list exists
    $docLibrary = $_.Lists[$lookForList]
   
    #Remove unwanted content types from the list
    if($docLibrary -ne $null)
    {
        $ctToRemove = $docLibrary.ContentTypes[$stringCTRemove]
        write-host "Removing content type" $ctToRemove.Name "from list" $docLibrary.Title
        $docLibrary.ContentTypes.Delete($ctToRemove.Id)
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}
#Dispose of the site object
$site.Dispose()


Powershell script add content type to document library in your Sharepoint 2010 site collection

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://yoursite.com/
$lookForList = "Document"
$ctName = "Your content type name"
#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {
   
    write-host "Checking site:"$_.Title
   
    #Make sure content types are allowed on the list specified
    $docLibrary = $_.Lists[$lookForList]
   
    if ($docLibrary -ne $null)
    {
        $docLibrary.ContentTypesEnabled = $true
        $docLibrary.Update()
   
        #Add site content types to the list
        $ctToAdd = $site.RootWeb.ContentTypes[$ctName]
        $ct = $docLibrary.ContentTypes.Add($ctToAdd)
        write-host "Content type" $ct.Name "added to list" $docLibrary.Title
        $docLibrary.Update()
    }
    else
    {
        write-host "The list" $lookForList "does not exist in site" $_.Title
    }
}
#Dispose of the site object
$site.Dispose()

Sunday, January 15, 2012

Creating sites from site templates

There's a lot of bugs you will notice when creating sites from site templates saved through the GUI in Sharepoint 2010. A couple of examples; Document ID is not unique in the site collection Content types you may have in your document libraries may be corrupt, there's a lot of fixing you will have to do by hand or by scripting, using powershell.

Document ID not unique

I've noticed document ID is not unique when using Sharepoint 2010 site templates saved from the GUI. Microsoft is working on a solution, there will be a hotfix in a couple of weeks.