Saturday, February 19, 2011

How to register Javascripts within SharePoint page programmatically

When you’re integrating script within a SharePoint page, you can use the ClientScriptManager object to add and manage scripts within a Web application. For example, the following code snippet shows a simple method that ensures only one instance of each script is added to a page:

public static void RegisterScript
(ref ClientScriptManager csm,
string key, string url)
{
if (!csm.IsClientScriptBlockRegistered(key))
csm.RegisterClientScriptInclude(key, url);
}

For more information on the ClientScriptManager, see http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx.

Thursday, February 3, 2011

Visual Studio 2010 and FxCop Integration

Visual Studio 2010 Premium and Ultimate Editions already have FxCop installed by default under the Code Analysis section in the Build menu option, see snapshot below:


You can configure your own Rules by right clicking on your Project -> Properties -> Code Analysis -> Set the Rule Set option as shown in the snapshot below:


To run the Code Analysis section, right click on your Project -> Run Code Analysis as shown in the snapshot below:


You will see the Code Analysis Results in the Error List window as shown in the snapshot below:
(Check for both Errors and Warnings and fix them)


I hope this helps.

SPDispose Check utility - Integrate with Visual Studio 2010

SPDisposeCheck utility checks whether you have disposed the unmanaged SPSite and SPWeb objects correctly or not. Its a tool that every SharePoint developer needs to have integrated in their Visual Studio boxes.

I have prepared a list of Best Practices for Disposing SharePoint objects: http://www.sharepointfix.com/2008/12/best-practices-disposing-sharepoint.html

Guidelines to integrate SPDisposeCheck with Visual Studio Solution:

1. Download the SPDisposeCheck.exe utility from: http://download.microsoft.com/download/B/4/D/B4D279A0-E159-40BF-A5E8-F49ABDBE95C7/SPDisposeCheck.msi

2. Open your Visual Studio environment and follow the steps as specified:
a. Go to Tools -> External Tools -> Add
b. Enter Title as SPDisposeCheck
c. Command : C:\Program Files (x86)\Microsoft\SharePoint Dispose Check\SPDisposeCheck.exe
d. Arguments : $(TargetName)$(TargetExt)
e. Initial Directory : $(TargetDir)
f. Check mark the Output window checkbox and
g. Click on the Ok button

See snapshot below:


3. Open your Visual Studio solution project and Build it.

4. Select Tools -> SPDisposeCheck:


5. Open the Output window and you should be able to see a set of messages like this:


6. It also shows up the errors in the Visual Studio Error Messages section as follows:

7. You can even configure additional SPDispose Check settings within Visual Studio 2010 by selecting "SharePointDispose Check" link under Tools, see snapshot below:


Let me know if it helps.

Saturday, January 22, 2011

Enumerate site collections, subsites and activate feature using Power Shell

This script enumerates through all site collections for a given web application, then enumerates through all subsites within each site collection and activates the given web level feature, copy the script as mentioned below, change the highlighted sections and save it as IterateSiteSubsitesActivateWebLevelFeature.ps1 file:

 Powershell Script updated on Feb 10th, 2012.

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$webApplicationURL = "http://dev-sp-2010:1000"
$featureFolderName = "PublishingWeb"

$webApp = Get-SPWebApplication $webApplicationURL

if($webApp -ne $null)
{
foreach($siteColl in $webApp.Sites)
{
   if($siteColl -ne $null)
   {
foreach($subWeb in $siteColl.AllWebs)
{
if($subWeb -ne $null)
{
# Print each Subsite
#Write-Host $subWeb.Url
                 
#Get Feature ID based on the Feature Name
$FeatureID = Get-SPFeature -Web $subWeb.Url | Where {$_.DisplayName -eq $featureFolderName}

if($FeatureID -ne $null)
{
#Check whether Feature to be activated is already activated for this subsite
if (Get-SPFeature -Web $subWeb.Url | Where {$_.ID -eq $FeatureID.Id})
{
Write-Host $featureFolderName "is already activated at :" $subWeb.Url
   }
else
{
#Enable-SPFeature -Identity $featureFolderName -Confirm:$false -Url $subWeb.url
Write-Host $featureFolderName "has been activated at :" $subWeb.url
}
}
$subWeb.Dispose()
}
else
{
Echo $subWeb "does not exist"
}
}
$siteColl.Dispose()
}
else
{
Echo $siteColl "does not exist"
}
}
}
else
{
Echo $webApplicationURL "does not exist, check the WebApplication name"
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

Echo Finish

 
To automatically run this script as a batch utility, use the following:
Copy code below, save it as a .bat file to run the powershell script

cd /d %~dp0
powershell -noexit -file ".\IterateSiteSubsitesActivateWebLevelFeature.ps1" "%CD%"
pause

I hope this helps. Also refer to the Microsoft TechNet post for an alternative webservice approach: http://blogs.msdn.com/b/vijay/archive/2009/10/01/how-to-list-all-the-sub-sites-and-the-site-collections-within-a-sharepoint-web-application-using-windows-powershell.aspx

Thursday, January 20, 2011

Creating Managed Metadata Termsets using PowerShell

Iterate through the XML file to dynamically create Managed Metadata Groups, Termsets and Terms using Powershell script.

Copy the XML mentioned below and save it as ManagedMetaDataTermSets.xml

<?xml version="1.0"?>
<termstore name="Managed Metadata Service">
  <group name="SharePoint Fix">
    <termset name="Region">
      <term name="North America">
        <term name="USA"></term>
        <term name="Canada"></term>
        <term name="Greenland"></term>
      </term>
      <term name="Technology">
        <term name="Technical Build and Delivery"></term>
        <term name="Technical Consultancy"></term>
        <term name="Technical Design"></term>
      </term>
      <term name="User Experience">
        <term name="Creative Design"></term>
        <term name="Information Architecture"></term>
      </term>
    </termset>
  </group>
</termstore>

The code mentioned below is RTM/Production Ready, you can find the original source code at Benn Robs site: http://sharepointtales.wordpress.com/2010/05/06/manipulating-the-terms-store-through-powershell/

Copy the code below and save it in a CreateTermSets.ps1 file: (replace the highlighted script block with your values)

Add-PsSnapin Microsoft.SharePoint.PowerShell

function SetTermsRecursive ([Microsoft.SharePoint.Taxonomy.TermSetItem] $termsetitem, $parentnode)
{
$parentnode.term
ForEach-Object {
## create the term
if($_ -ne $null)
{
$newterm = $termsetitem.CreateTerm($_.name, 1033)
Write-Host -ForegroundColor Cyan "Added term $_.name"
SetTermsRecursive $newterm $_
}
}
}
#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}

#Solutions to Deploy
$XMLName = "ManagedMetaDataTermSets.xml"
$XMLPath = Join-Path (Get-ScriptDirectory) $XMLName

echo "Extracting information from the $XMLPath"

#Site Collection URL - Give your site collection url in quotation marks
$TaxonomySiteUrl = "http://localhost"

#Access the TermStore data
[xml]$TermStoreData = Get-Content ($XMLPath)

$site = Get-SPSite $TaxonomySiteUrl
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termstore = $session.TermStores[$TermStoreData.termstore.name]
$TermStoreData.termstore.group

ForEach-Object {
## create the group
if ($termstore.Groups[$_.name] -eq $null)
{
$group = $termstore.CreateGroup($_.name);
Write-Host -ForegroundColor Cyan "Added group $_.name"
$_.termset

ForEach-Object {
## create the termset
$termset = $group.CreateTermSet($_.name)
Write-Host -ForegroundColor Cyan "Added termset $_.name"
SetTermsRecursive -termsetitem $termset -parentnode $_
}
}
}

$termstore.CommitAll()
Copy code below, save it as a .bat file to automatically run the powershell script
cd /d %~dp0
powershell -noexit -file ".\CreateTermSets.ps1" "%CD%"
pause

I have created another PowerShell script that reads through the XML file and delete the respective termsets and group:

Add-PsSnapin Microsoft.SharePoint.PowerShell

#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

#Solutions to Deploy
$XMLName = "ManagedMetaDataTermSets.xml"
$XMLPath = Join-Path (Get-ScriptDirectory) $XMLName
echo "Extracting information from the $XMLPath"

#Site Collection URL - Give your site collection url in quotation marks
$TaxonomySiteUrl = "http://localhost"

#Access the TermStore data
[xml]$TermStoreData = Get-Content ($XMLPath)
$site = Get-SPSite $TaxonomySiteUrl
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termstore = $session.TermStores[$TermStoreData.termstore.name]

$TermStoreData.termstore.group |
 ForEach-Object {
  ## create the group
  if ($termstore.Groups[$_.name] -ne $null)
  {
    # $_.termset | ForEach-Object {
    #$termstore.Groups[$_.name].TermSet
    Write-Host -ForegroundColor Cyan "Deleted $termsetCollection"

 #Get the Term Store Group object
 $groupName = $termstore.Groups[$_.name]

 #Get Term Sets from the Group
 $groupName.TermSets | ForEach-Object {
   $_.Delete();
 }

# #Iterate through each
# foreach($termSet in $termstore.Groups[$_.name].TermSets)
# {
#  $termSet.Delete();
# }

 #Finally delete the Group
 $termstore.Groups[$_.name].Delete()
   }
  }
 $termstore.CommitAll()

I hope this helps.