Monday, July 22, 2013

Get all seach scopes for current site collection


In this article we will be seeing about search scopes in SharePoint 2010.|

In this article:
  • Add namespaces
  • Get search context
  • Get all scopes
  • Skip default scopes
  • Add scopes in dropdown
Get all the search scopes:

Add following namespaces.

using Microsoft.Office.Server;
using  Microsoft.Office.Server.Search.Administration;

Use following method to populate all scopes of current site collection in dropdown.

private void GetScopes()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
// Get the search context object.
SearchContext context = SearchContext.GetContext(site);
Scopes scopes = new scopes(context);
foreach(Scope scope in scopes.GetSharedScopes())
{
// Skip following default scopes
// Global Query Exclusion - Everything that should be omitted from all search by default
// Rank Demoted Sites - Sites whose rank will be demoted in click - distance calculation.

if(scope.Name != "Global Query Exclusion" && scope.Name !="Rank demoted Sites")
// Show scopes in dropdown
ddlscopes.Items.Add(scope.Name);
}
}
});
}

Sunday, July 7, 2013

CAML Query to get all documents from a Document Library

CAML query to retrieve all documents from all folders in a document library.

SPList lstDocs = SPContext.Current.Web.Lists.TryGetList("myDocLib");
SPQuery query = new SPQuery();
query.Query = @"<Where><BeginsWith><FieldRef Name = 'ContentTypeId' /><Value Type= 'ContentTypeId'>0x0101</Value></BeginsWith></Where>";
query.ViewAttributes = "Scope = 'RecursiveAll'";
SPListItemCollection itmsDocs = lstDocs.GetItems(query);

Wednesday, March 13, 2013

Deploy Administrator Approved Forms - InfoPath 2010

Administrator-approved form templates

Administrator-approved form templates are available to any site collection on the SharePoint site and can contain code that requires full trust. They are individually verified, uploaded, and activated by a SharePoint site administrator. Administrator-approved form templates are maintained in a special document library that can be accessed only by administrators.

Deploy administrator-approved form templates

To deploy an administrator-approved form template, you must complete three actions after the form template has been designed: verify, upload, and activate. These steps can either be performed through the command-line interface or through the Central Administration site. You can upload a form template by using the Publishing Wizard in the InfoPath program, using the command line on a server running InfoPath Forms Services in the farm to which the form template will be deployed, or by using the Central Administration interface.
To deploy the form using central admin you can refer
http://technet.microsoft.com/en-us/library/cc262921.aspx
Using central admin method we can deploy one form at a time but if we need to deply many forms this process takes lot of time and effort. So I tried to automate this process using powershell. I have wrote a small powershell script to deploy these forms.
# Set the required variables
$siteURL = "
http://MySiteURL"
$publishingDirectory = "Source file location"
$settingsFile = "$publishingDirectory\InfoPathFormsList.txt"
$category = "My Category"
function Show-Progress([string]$activity, [string]$status, [int]$count)
{
if ($count % 4 -eq 1) {write-progress -activity $activity -status $status " _" ;}
if ($count % 4 -eq 2) {write-progress -activity $activity -status $status " \" }
if ($count % 4 -eq 3) {write-progress -activity $activity -status $status " |" }
if ($count % 4 -eq 0) {write-progress -activity $activity -status $status " /" }
}
foreach ($i in Get-Content -Path $settingsFile)
{
$formLocation = "$publishingDirectory\$i.xsn"
# Deactivate the InfoPath form
Write-Output "Deactivate " $i
Disable-SPInfoPathFormTemplate -Identity $i -Site $siteURL -ErrorAction:SilentlyContinue
# uninstall the InfoPath form
Write-Output "Uninstalling " $i
$formTemplate = Get-SPInfoPathFormTemplate -Identity $i -ErrorAction:SilentlyContinue
if ($formTemplate -ne $null)
{
Uninstall-SPInfoPathFormTemplate -Identity $i -ErrorAction:SilentlyContinue
}
$percent = 1
while ($formTemplate -ne $null -and $formTemplate.FormTemplateStatus -eq "Removing")
{
if ($count -eq 100) {$count = 0;}
write-progress -activity "Uninstalling $i" -status "Removing" -percentcomplete (++$count);
$formTemplate = Get-SPInfoPathFormTemplate -Identity $i -ErrorAction:SilentlyContinue;
}
write-progress -activity "Uninstalling $i" -status "Removing" -percentcomplete 100;
# Verify the InfoPath form
#Write-Output "Verifying " $i
#Test-SPInfoPathFormTemplate -Path $formLocation
# Upload the InfoPath form
Write-Output "Uploading " $i
Install-SPInfoPathFormTemplate -Path $formLocation -Confirm:$false
# Activate the InfoPath form
Write-Output "Activating " $i
Enable-SPInfoPathFormTemplate -Identity $i -Site $siteURL
# Modify the category of the form
Set-SPInfoPathFormTemplate -Identity $i -Category $category
}

To use this script

1. Copy this script in a notepad.
2. Change $siteURL =
http://MySiteURL
3. Create a folder sourcesFiles and paste your InfoPath form template (.xsn) files in it.
4. Enter your source folder path in $publishingDirectory = "Source folder path"
5. Save this notepad text as PowerShell script (myScript.ps1).
6. Create a InfoPathFormsList.txt file in the same folder and write all InfoPath form template names (i.e. myTemplate.xsn) in it.
7. Open SharePoint 2010 management shell and run this PowerShell script