Wednesday, November 24, 2010

SharePoint 2010 Publishing feature GUID - Activation Dependency

This Publishing feature Activation Dependency within the elements.xml ensures that your custom feature gets activated only when Publishing feature is enabled on your respective site collection. Copy and paste the code below in your custom feature project elements.xml file -

<ActivationDependencies>
    <ActivationDependency FeatureTitle="Publishing Infrastructure" FeatureDescription="Publishing Infrastructure need to be activated prior to deploying the Custom PageLayouts, Site Content Types and Site Columns feature." FeatureId="f6924d36-2fa8-4f0b-b16d-06b7250180fa" />
  </ActivationDependencies>

If the scope of your feature is Web, then you can even ensure whether Publishing site feature has been activated on your subsite as well:

<ActivationDependency FeatureTitle="SharePoint Server Publishing" FeatureDescription="Option to create Custom Page Layouts will not be visible, unless you activate SharePoint Server Publishing feature at the web level." FeatureId="94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" />

Monday, November 22, 2010

Power Shell .wsp deployment scripts for SharePoint 2010

Copy the PowerShell scriptlet below and paste it in a notepad, modify your Site Collection URL in the code marked in yellow below and save it with a .ps1 extension.

Also have a look at Powershell script to deploy multiple solutions (.wsp) to your site collection. See the full post here: http://www.sharepointfix.com/2011/07/powershell-script-to-deploy-multiple.html
=======================================================================
Add-PsSnapin Microsoft.SharePoint.PowerShell

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

#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
$SolutionName="PrintListItem.wsp"
$SolutionPath = Join-Path (Get-ScriptDirectory) $SolutionName
echo "Extracting information from $SolutionPath"

$SolutionPath= $SolutionPath
$FeatureName="PrintListItem_Feature1"

#Feature name
$FeatureID= $(Get-SPFeature -limit all | ? {($_.displayname -eq $FeatureName)}).Id
$SingleSiteCollection = Get-SPSite $SiteUrl

#Admin service
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName
   Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'
}

Write-Host 'DEACTIVATING FEATURE ...'

Disable-SPFeature -Identity $FeatureName -Url $SiteUrl -Confirm:$false
Write-Host 'FEATURE HAS BEEN DEACTIVATED SUCCESSFULLY.'

#Uninstall
Write-Host 'UNINSTALLING SOLUTION ...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}

if ($Solution -ne $null)
{
    if($Solution.ContainsWebApplicationResource)
    {
        Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false
    }
    else
    {
        Uninstall-SPSolution $SolutionName -Confirm:$false
    }
}

while ($Solution.JobExists)
{
    Start-Sleep 2
}

Write-Host 'SOLUTION HAS BEEN UNINSTALLED SUCCESSFULLY.'

Write-Host 'REMOVING SOLUTION ...'

if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
{
    Remove-SPSolution $SolutionName -Confirm:$false

Write-Host 'SOLUTION HAS BEEN REMOVED SUCCESSFULLY.'
}

Write-Host 'ADDING SOLUTION ...'

Add-SPSolution $SolutionPath  | Out-Null

Write-Host 'SOLUTION HAS BEEN ADDED SUCCESSFULLY.'

Write-Host 'DEPLOYING SOLUTION ...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}

#use '-force' paramater to install all commands in this if statement

if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
{
Install-SPSolution $SolutionName –AllwebApplications -GACDeployment -Force -Confirm:$false
}
else
{
Install-SPSolution $SolutionName -GACDeployment -Force -Confirm:$false
}

while ($Solution.Deployed -eq $false)
{
    Start-Sleep 2
}

Write-Host 'SOLUTION HAS BEEN DEPLOYED SUCCESSFULLY.'

Write-Host 'ACTIVATING FEATURE ...'

if ($FeatureName -ne $null)
{
    Enable-SPFeature -Identity $FeatureName -Url $SiteUrl -Confirm:$false
}
#message
Write-Host 'FEATURE HAS BEEN ACTIVATED SUCCESSFULLY.'


if (-not $IsAdminServiceWasRunning)
{
    Stop-Service $AdminServiceName
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

Echo Finish

=====================================================================

Now create a new notepad file and copy the code mentioned below, and then save the below mentioned code in a .bat file to automate the PowerShell deployment.

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

Friday, November 19, 2010

Custom Ribbon Actions in SharePoint 2010 - Elements.xml

Custom action to add a Ribbon button in the Display Form of a SharePoint List:

<CustomAction Description="Prints a single List Item" Title="Print List Item" Id="{055c63b4-58d8-4b4a-a366-70f69257b491}" Location="CommandUI.Ribbon.DisplayForm" RegistrationId="100" RegistrationType="List" Sequence="0" Rights="ViewListItems" xmlns="http://schemas.microsoft.com/sharepoint/">
    <UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
    <CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.Controls._children">
          <Button Id="{C75857A4-C0BC-439A-813A-A1DCC885A14A}" Command="{06000B27-86BB-4203-AF44-29F61FDA678D}" Image32by32="~site/_layouts/Icon/PrintIcon.jpg" Image16by16="~site/_layouts/Icon/PrintIcon.jpg" Sequence="0" LabelText="Print List Item" Description="Prints a Single List Item" TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="{06000B27-86BB-4203-AF44-29F61FDA678D}" CommandAction="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

Custom action to add a Ribbon button in the Display Form of a SharePoint Task List:

<CustomAction Description="Prints a single List Item" Title="Print List Item" Id="{055c63b4-58d8-4b4a-a366-70f69257b491}" Location="CommandUI.Ribbon.DisplayForm" RegistrationId="107" RegistrationType="List" Sequence="1000" Rights="ViewListItems" xmlns="http://schemas.microsoft.com/sharepoint/">
    <UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
    <CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.Controls._children">
          <Button Id="{C75857A4-C0BC-439A-813A-A1DCC885A14A}" Command="{06000B27-86BB-4203-AF44-29F61FDA678D}" Image32by32="~site/_layouts/Icon/PrintIcon.jpg" Image16by16="~site/_layouts/Icon/PrintIcon.jpg" Sequence="1000" LabelText="Print List Item" Description="Prints a Single List Item" TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="{06000B27-86BB-4203-AF44-29F61FDA678D}" CommandAction="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

Custom action to add a Ribbon button in the Display Form of a SharePoint Document Library:

<CustomAction Description="Prints a single List Item" Title="Print List Item" Id="{055c63b4-58d8-4b4a-a366-70f69257b491}" Location="CommandUI.Ribbon.DisplayForm" RegistrationId="101" RegistrationType="List" Sequence="1000" Rights="ViewListItems" xmlns="http://schemas.microsoft.com/sharepoint/">
    <UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
    <CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.Controls._children">
          <Button Id="{C75857A4-C0BC-439A-813A-A1DCC885A14A}" Command="{06000B27-86BB-4203-AF44-29F61FDA678D}" Image32by32="~site/_layouts/Icon/PrintIcon.jpg" Image16by16="~site/_layouts/Icon/PrintIcon.jpg" Sequence="1000" LabelText="Print List Item" Description="Prints a Single List Item" TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="{06000B27-86BB-4203-AF44-29F61FDA678D}" CommandAction="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

Custom action to add an item in the Edit Control Block of a SharePoint List:

<CustomAction Id="PrintListItem.ItemToolbar"
  GroupId="PrintListItem"
  RegistrationType="List"
  RegistrationId="100"
  Location="EditControlBlock"
  Sequence="100"
  Title="Print List Item"
  ImageUrl ="/_layouts/Icon/PrintIcon.jpg">
  <UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}"/>
</CustomAction>

Custom action to add an item in the Edit Control Block of a SharePoint Task List:

<CustomAction Id="PrintListItem.ItemToolbar" GroupId="PrintListItem" RegistrationType="List" RegistrationId="107" Location="EditControlBlock" Sequence="300" Rights="ViewListItems"
Title="Print List Item" ImageUrl ="/_layouts/Icon/PrintIcon.jpg"><UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}"/></CustomAction>

Custom action to add an item in the Edit Control Block of a SharePoint Document Library:

<CustomAction Id="PrintListItem.ItemToolbar"
  GroupId="PrintListItem"
  RegistrationType="List"
  RegistrationId="101"
  Location="EditControlBlock"
  Sequence="300"
  Rights="ViewListItems"
  Title="Print List Item"
  ImageUrl ="/_layouts/Icon/PrintIcon.jpg">
    <UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}"/>
    <!--<UrlAction Url="javascript:window.open('{SiteUrl}/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}','PrintListItem','height=600,width=800,resizable=yes,scrollbars=1');"/>-->
  </CustomAction>

Custom action to add a Ribbon item in the SharePoint 2010 Standard Actions Menu of a SharePoint List:

<CustomAction   GroupId="ActionsMenu" Location="Microsoft.SharePoint.StandardMenu" Sequence="1000" Title="Print List Item" ImageUrl="/_layouts/Icon/PrintIcon.jpg" Description="Print list item" RegistrationType="List">
<UrlAction Url="~site/_layouts/PrintListItem/PrintListItem.aspx?List={ListId}&amp;ID={ItemId}" />
</CustomAction>

Will keep updating this post with more Custom Action elements in the future.

Thursday, November 11, 2010

Cross Site collection dropdown Look Up using JQuery

Often times you might need to do a sub-site or a cross site collection look up and populate it as a dropdown control in your SharePoint application. This is possible using JQuery. Here is the code for the same:

<script type="text/javascript" src="/sites/SPFix/JQueryDemo/JQueryDocumentLibrary/jquery-1.4.2.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
var soapEnv =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
<soapenv:Body>
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
<listName>Tasks</listName>
<viewFields>
<ViewFields>
<FieldRef Name='Title' />
</ViewFields>
</viewFields>
</GetListItems>
</soapenv:Body>
</soapenv:Envelope>";

$.ajax({
url: "/sites/SharePointFix/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset="utf-8""
});
});

function processResult(xData, status) {
$(xData.responseXML).find("z\:row").each(function() {
$('#crossSiteCollectionLookUp').
append($("<option></option>").
attr("value",$(this).attr("ows_Title")).
text($(this).attr("ows_Title")));
});
};
</script>
<body>
<select id="crossSiteCollectionLookUp">
</select>
</body>

1. Download and install the jQuery library from http://www.jquery.com/

2. Upload the jQuery library in any of your SharePoint document library and refer the path in the script source URL highlighted above. For best practices on integrating jQuery with SharePoint, refer: http://weblogs.asp.net/jan/archive/2008/11/20/sharepoint-2007-and-jquery-1.aspx

3. Drag and drop the Content editor webpart where you want to show your dynamic dropdown lookup column to be visible.

4. Copy and paste the  above code in the Content Editor webpart. Change the highlighted sections like List name, Column name, URL and Display column names as required in your scenario.

In the code snippet above, I am dynamically populating the Title column from my Tasks List in a Dropdown control.

Should work like a charm :)