Tuesday, December 24, 2013

Error occurred in deployment step 'Install app for SharePoint': Failed to install app for SharePoint. Please see the output window for details.

Hi,

I was facing the below error when I was trying to deploy the SharePoint hosted app.

Searched lot of articles, regarding this.
But I finally was able to deploy the solution by doing the changes in the feature as below


Thursday, November 21, 2013

Scroll Tiles using jquery

Hi,

My requirement is to get the items from the custom list ,display those items on the page through Tiles in the scrolling format from right to left.

From MSDN I got guidance as below

1. Find the Web Part ID through Internet Explorer F12 developer tools.
2. Add a content edit web part on the page.
3. Edit the web part and insert  the code below into it.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#promotedlinksbody_WPQ2").replaceWith("<marquee>" + $("#promotedlinksbody_WPQ2").html() + "</marquee>");
    });
</script>

Finally I am getting the output as below

Varying Search Results post migration to SharePoint 2013

Hi,

In SharePoint 2013 central admin, through query builder we are passing one query and we are getting 4 results.
But we migrated a source code from SharePoint 2010 to SharePoint 2013 where we are getting only one result.
Earlier code in SharePoint 2010 is as below
 public static DataTable KeywordSearch(List<string> selectProperties,string scope,
            string searchServiceAppName,string query)
        {
            SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
            SearchServiceApplicationProxy searchProxy =
                settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>(searchServiceAppName);

            KeywordQuery keywordQuery = new KeywordQuery(searchProxy);
            keywordQuery.QueryText = query;
            keywordQuery.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
            foreach (string property in selectProperties)
            {
                keywordQuery.SelectProperties.Add(property);
            }
            keywordQuery.ResultTypes = ResultType.RelevantResults;
            keywordQuery.HiddenConstraints = "scope:" + "\"" + scope + "\"";                       

            ResultTableCollection resultsTableCollection = keywordQuery.Execute();           
            ResultTable searchResultsTable = resultsTableCollection[ResultType.RelevantResults];
            DataTable resultsDataTable = new DataTable();
            resultsDataTable.TableName = "Results";
            resultsDataTable.Load(searchResultsTable, LoadOption.OverwriteChanges);
            var searchResultsTables = resultsTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
            DataTable resultsDataTable = new DataTable();
            if (searchResultsTables != null && searchResultsTables.Count() > 0)
                resultsDataTable = searchResultsTables.FirstOrDefault().Table;
            return resultsDataTable;
        }
Modified to SharePoint 2013 as below
   public static DataTable KeywordSearch(List<string> selectProperties,string scope,
            string searchServiceAppName,string query)
        {
            SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
            SearchServiceApplicationProxy searchProxy =
                settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>(searchServiceAppName);

            KeywordQuery keywordQuery = new KeywordQuery(searchProxy);
            keywordQuery.QueryText = query;
            keywordQuery.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
            foreach (string property in selectProperties)
            {
                keywordQuery.SelectProperties.Add(property);
            }         

            SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.Ssa);
            SourceRecord source = searchProxy.GetResultSourceByName(scope, owner);
            keywordQuery.SourceId = source.Id;

            SearchExecutor executor = new SearchExecutor();
            ResultTableCollection resultsTableCollection= executor.ExecuteQuery(keywordQuery);

            DataTable resultsDataTable = new DataTable();
            if (searchResultsTables != null && searchResultsTables.Count() > 0)
                resultsDataTable = searchResultsTables.FirstOrDefault().Table;
            return resultsDataTable;
        }
Finally our team has found solution by the including the below line
keywordQuery.SourceId = source.Id;
keywordQuery.TrimDuplicates = false;    
Now able to get the results exactly as per query builder in central admin

Wednesday, November 20, 2013

Content Source Creation using PowerShell

Hi,

In the migration process, we have a requirement i.e.  

1)To create content source through Power Shell
2)Updating the newly created content source with site collection through Power Shell
3)Running the full crawl on the newly created content source

For these we have achieved by writing the script as below

// CreateContentWithMulURLs.ps1
#Set the Microsoft.SharePoint.PowerShell cmdlets
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
    {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }

#Get the current script execution directory
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition
$InputFileName = "Content.csv"
$ContentFile = $executingScriptDirectory +"\"+ $InputFileName
$CreateContentcsv = import-csv $ContentFile
$OutputLogFileName= "Log.txt"

#Get the Log file ready
$LogDirectory=$executingScriptDirectory + "\" + $OutputLogFileName
$file = New-Item $LogDirectory -type file -force


foreach($field in $CreateContentcsv) 
{
                $SearchServiceApplicationName = $field.SearchServiceApplicationName

                $ContentSourceName = $field.ContentSourceName

                $defaultContentSourceName = $field.defaultContentSourceName

                $SiteUrl = $field.SiteUrl
}

#Remove Urls from default Content Source
try
{

    $defaultContentSource = Get-SPEnterpriseSearchCrawlContentSource -Identity $defaultContentSourceName -SearchApplication $SearchServiceApplicationName

    if($defaultContentSource.CrawlState -eq "Idle")
    {

        #If multiple addresses, remove one by one
        if($SiteUrl.Contains(","))
        {     
                        [array]$StartAddresses = $SiteUrl.Split(",")
        }
        else
        {
                        [array]$StartAddresses = $SiteUrl
        }

        if($StartAddresses.Count -ne 0)
        {
            for ($i = 0; $i -lt $StartAddresses.length; $i++)
                        {
                                        if ($defaultContentSource.StartAddresses.Exists($StartAddresses[$i]))
                                        {
                                                    $defaultContentSource.StartAddresses.Remove($StartAddresses[$i])
                                                        $LogString= $StartAddresses[$i] +" is removed from content source "+ $defaultContentSource.Name
                    add-content $file $LogString
                                        }
                                        $defaultContentSource.Update()                   
                        }
        }
    }
    else
    {
        Write-Host "$defaultContentSourceName content source is not in Idle Status. Please re-run the script later..."
        $LogString= $defaultContentSourceName + " content source is not in Idle Status. Please re-run the script later...."
        add-content $file $LogString
                Pause
        break       
    }
}
catch
{
                    #Log the error        
        $LogString= $error[0].Exception.Message;
        add-content $file  $LogString
        $error.clear;
}



#if content Source named StorePortal already exist. Updating the Content source with startaddresses
try
{
    $flag = 0

    $SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue

    if($SearchServiceApplication.Count -ne 0)
    {
        $ContentSources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication

        $ContentSources | ForEach-Object {   
            if ($_.Name.ToString() -eq $ContentSourceName)
            {       
                            $SPContentSource = Get-SPEnterpriseSearchCrawlContentSource -Identity $ContentSourceName -SearchApplication $SearchServiceApplicationName
                            Write-Host "Content Source : $ContentSourceName already exist. Updating the Content source with start addresses..."

                            $LogString= "Content Source : "+$ContentSourceName+" already exist. Updating the Content source with start addresses..."
                            add-content $file $LogString

                            $flag = 1
                        if($StartAddresses.Count -ne 0)
                                        {
                                                        for ($i = 0; $i -lt $StartAddresses.length; $i++)
                                                        {
                                                                        if (!($SPContentSource.StartAddresses.Exists($StartAddresses[$i])))
                                                                        {
                                                                        $SPContentSource.StartAddresses.Add($StartAddresses[$i])
                                                                                        $LogString= $StartAddresses[$i] +" is added to content source "+ $SPContentSource.Name
                                        add-content $file $LogString
                                                                        }
                                                                        $SPContentSource.Update()                                                             
                                                        }
                                        }
            }
                    }
    }
}
catch
{
                #Log the error       
    $LogString= $error[0].Exception.Message;
    add-content $file  $LogString
    $error.clear;
}


#Create the new content source if content Source named testcontentsource not exist
try
{
    if($flag -eq 0)
    {
                    $SPContentSource = New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchServiceApplication -Type SharePoint -name $ContentSourceName -StartAddresses $SiteUrl
                    $LogString= "Content Source : "+$ContentSourceName +" created"
                    add-content $file $LogString
    }
}
catch
{
                #Log the error       
    $LogString= $error[0].Exception.Message;
    add-content $file  $LogString
    $error.clear;
}


try
{
    if($SPContentSource.CrawlState -eq "Idle")
    {
        Write-Host "Starting the FullCrawl for the content source : $ContentSourceName"
        Write-Host "Please wait for while until the crawl gets completed"
        $SPContentSource.StartFullCrawl()
        do {Start-Sleep 2; Write-Host "." -NoNewline}
        While ( $SPContentSource.CrawlState -ne "CrawlCompleting")
        Write-Host "FullCrawl for the content source : $ContentSourceName completed."
        $LogString= $ContentSourceName + " Content Source FullCrawl completed."
        add-content $file $LogString
    }
}
catch
{
                #Log the error       
    $LogString= $error[0].Exception.Message;
    add-content $file  $LogString
    $error.clear;
}
[void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")

Pause

// Content.csv
SearchServiceApplicationName,ContentSourceName,defaultContentSourceName,SiteUrl
Search Service Application,testcontentsource,Local SharePoint sites,"http://abc:9889/"

Enable Session State Script

Hi,

Earlier we were updating below details in web config manually
Now as part of migration from Sharepoint 2010 to Sharepoint 2013 we had automate the above through power shell as below

// EnableSessionState_Batch.bat
Powershell.exe -Command Set-ExecutionPolicy "Bypass"
Powershell.exe -Command "& {E:\Naga\EnableSessionState\EnableSessionState.ps1}"
Pause

// EnableSessionState.xml
<?xml version="1.0" encoding="utf-8" ?>
<SessionInputs>
<WebAppConfigPath>C:\inetpub\wwwroot\wss\VirtualDirectories\32113\web.config</WebAppConfigPath>
</SessionInputs>

// EnableSessionState.ps1
#-----Input parameters to the script
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snap-in"
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

# Get complete file path (eg:  E:\SP2010\xxxx.ps1)
$filepath = $MyInvocation.MyCommand.Definition                                       
# Get current Directory  file path  (eg:  E:\SP2010)
$directorypath = [System.IO.Path]::GetDirectoryName($filepath)        
# Get current Drive   (eg:  E:\)
$directory = Get-Item $directorypath | Split-Path -Parent             
$InputFile = $directorypath+"\EnableSessionState.xml"

$xmlinput = [xml] (get-content $InputFile)
$item = $xmlinput.SessionInputs

## Start logging
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\EnableSessionState-$LogTime.rtf"
Start-Transcript -Path $LogFile -Force

#---------------------Variables-----------------------------------#
#$csvfpath - to get csv file path from xml
$wappconfigpath = $item.WebAppConfigPath
#-----------------------------------------------------------------#

#-----------Function to add Session State----------------------------------------------------------#
try
{
Function AddSessionState {
    Write-Host "Enabling Sessions for SharePoint Web Application..." -foregroundcolor "Cyan"

    # Get the path to the config file from $WebApp
    # Prepare variables required to extract Web.Config from SPWebApplication   

    ##$WebAppConfigPath = "C:\inetpub\wwwroot\wss\VirtualDirectories\42275\Web.Config"
   
    Write-Host "Loading Web.Config as XML in " $WebAppConfigPath -ForegroundColor Cyan
    [xml]$xml = Get-Content $wappconfigpath
    Write-Host "Done" -ForegroundColor Green  
  
    # Check for system.webserver node
    $moduelsNode = $xml.SelectSingleNode("//configuration/system.webServer/modules")
    if ($moduelsNode -ne $null)
    {
        Write-Host "modules node exists in  section. Updating the node..." -ForegroundColor Cyan    

        # Check for
        $addSessionNode = $xml.SelectSingleNode("//configuration/system.webServer/modules/add[@name='Session']")
        if ($addSessionNode -ne $null)
        {
            Write-Host "'' entry found in Web.Config....So no updates made for this node." -ForegroundColor Cyan
        }
        else
        {
            # Recreate the  node
            $root = $xml.get_DocumentElement();        
            $addSessionNode = $xml.CreateNode('element',"add","")   
            $addSessionNode.SetAttribute("name", "Session")
            $addSessionNode.SetAttribute("type", "System.Web.SessionState.SessionStateModule")
            $appSettingsNode =  $xml.SelectSingleNode("//configuration/system.webServer/modules").AppendChild($addSessionNode)

            #Apply to Web.Config
            $xml.Save($wappconfigpath)

            Write-Host -NoNewline "'' entry created in Web.Config." -ForegroundColor Green
        }
    }

    Write-Host "Sessions enabled successfully." -foregroundcolor "Green"
} AddSessionState
#-----------------------------------------------------------------------------------------------------------------#
#-----------Function to Enable Session State----------------------------------------------------------------------#
function Set-enableSessionState{
    param([string]$path = $(read-host "Please enter a web.config file to read"),
          [string]$value = $(read-host "Set Session State : 'true' or 'false'"))   
    
    $date = (Get-Date).tostring("mm_dd_yyyy-hh_mm_s");
    $backup = $path + "_$date";
    $xml = [xml](Get-Content $path);

    $xml.Save($backup);
    $root = $xml.get_documentElement();
    $sysweb = $root["system.web"];
    $sysweb.pages.SetAttribute("enableSessionState",$value);
   
    $xml.Save($path);   
}

Set-enableSessionState $wappconfigpath "true"
Write-Host "Session State has been enabled..." -ForegroundColor Cyan
#-----------------------------------------------------------------------------------------------------------------#
#-----------Function to add Session Mode---------------------------------------------------------------------------#
Function AddSessionMode {
    Write-Host "Enabling Sessions for SharePoint Web Application..." -foregroundcolor "Cyan"

    # Get the path to the config file from $WebApp
    # Prepare variables required to extract Web.Config from SPWebApplication   

    ##$WebAppConfigPath = "C:\inetpub\wwwroot\wss\VirtualDirectories\42275\Web.Config"
   
    Write-Host "Loading Web.Config as XML in " $wappconfigpath -ForegroundColor Cyan
    [xml]$xml = Get-Content $wappconfigpath
    Write-Host "Done" -ForegroundColor Green  
  
    # Check for system.web node
    $InProcNode = $xml.SelectSingleNode("//configuration/system.web")
    if ($InProcNode -ne $null)
    {
        Write-Host "InProc node exists in  section. Updating the node..." -ForegroundColor Cyan    

        # Check for
        $addSessionNode = $xml.SelectSingleNode("//configuration/system.web/sessionState[@mode='InProc']")
        if ($addSessionNode -ne $null)
        {
            Write-Host "'' entry found in Web.Config....So no updates made for this node." -ForegroundColor Cyan
        }
        else
        {
            # Recreate the  node
            $root = $xml.get_DocumentElement();        
            $addSessionNode = $xml.CreateNode('element',"sessionState","")   
            $addSessionNode.SetAttribute("mode", "InProc")
            $appSettingsNode =  $xml.SelectSingleNode("//configuration/system.web").AppendChild($addSessionNode)

            #Apply to Web.Config
            $xml.Save($wappconfigpath)

            Write-Host -NoNewline "'' entry created in Web.Config." -ForegroundColor Green
        }
    }

    Write-Host "Sessions enabled successfully." -foregroundcolor "Green"
} AddSessionMode
}
catch
{
                Write-Output $_
}
#-----------------------------------------------------------------------------------------------------------------#
$EndDate = Get-Date
Write-Host -ForegroundColor White "-----------------------------------"
Write-Host -ForegroundColor White "| Session State Enabled |"
Write-Host -ForegroundColor White "| Started on: $StartDate |"
Write-Host -ForegroundColor White "| Completed:  $EndDate |"
Write-Host -ForegroundColor White "-----------------------------------"
Stop-Transcript
Invoke-Item $LogFile     

Finally got the result as expected.