Thursday, 14 January 2016

SharePoint Keyword Query Code

Below the C# code to display the Search Result for a Query from Console Application.

Console Code to View the Search Results


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Query;

namespace SearchConsoleApplication1
{
       class Program
       {
              static void Main(string[] args)
              {
                     using (SPSite siteCollection = new SPSite("http://w15-sp"))
                     {
                           KeywordQuery keywordQuery = new KeywordQuery(siteCollection);
                           keywordQuery.QueryText = "SharePoint";
                           keywordQuery.SortList.Add("Author", SortDirection.Ascending);
                           keywordQuery.SortList.Add("Size", SortDirection.Descending);

                           SearchExecutor searchExecutor = new SearchExecutor();
                           ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
                           var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);

                           var resultTable = resultTables.FirstOrDefault();

                           DataTable dataTable = resultTable.Table;
                DataTable workTable = dataTable;
                DataRow[] currentRows = workTable.Select(null, null, DataViewRowState.CurrentRows);

                    if (currentRows.Length < 1 )
                      Console.WriteLine("No Current Rows Found");
                    else
                    {
                        Console.WriteLine("Writing Column Names ");
                      foreach (DataColumn column in workTable.Columns)
                        Console.Write("\t{0}", column.ColumnName);

                      Console.WriteLine("\tRowState");
                      Console.WriteLine("Writing Rows values ");
                     /* foreach (DataRow row in currentRows)
                      {
                        foreach (DataColumn column in workTable.Columns)
                          Console.Write("\t{0}:", row[column]);

                        Console.WriteLine("\t::" + row.RowState);
                      }*/
                    }

Console.ReadLine();
                     }
              }
       }
}



References




Monday, 4 January 2016

SharePoint One Liners in Powershell

The magic of Powershell lies in executing complex queries in a single line which might need many clicks from an User Interface. Below are a couple of one line codes in PowerShell for SharePoint.

1.What is the SharePoint Version
(Get-SPFarm).BuildVersion

Note: 15 means SharePoint 2013,14 for SharePoint 2010,12 for MOSS

3. Get the domain name that hosts apps in the entire farm
Get-SPAppDomain

4. Display all the managed accounts in the farm
Get-SPManagedAccount

5. Returns all servers in the local farm with SharePoint installed excluding Exchange and non Application server.
Get-SPServer | Where{$_.Role -ne "Invalid"} |sort Address

5. Get the total number of site count hosted in each content database in Farm. Returns the display in HTML format stored at c:\getDBSitecount.html"
Get-SPContentDatabase | ConvertTo-Html -Property Name,WebApplication,CurrentSiteCount|Out-File -FilePath C:\getDBSitecount.html

5. List the timer job running monthly and show the output in gridview
Get-SPTimerJob|?{$_.Schedule -like "month*"}|SELECT Name,Decription,Schedule|OUT-GRIDVIEW


Reference:
http://sharepoint.stackexchange.com/questions/26471/get-all-the-machines-in-a-sharepoint-farm



Sunday, 3 January 2016

SharePoint and Powershell 3 step guide

Knowing little Powershell can make you feel very powerful in SharePoint. Below is a quick demo explaining 3 steps needed to run Powershell commands in SharePoint with ease.




Step 1:Find the Command(s) – Get-Command

Step 2:Get details of the Command(s) – Get-help

Step 3: Test the command with a Get operation and filter for a requirement to view data


Step 1:Get Command

žVerb-Noun format for Powershell commands
Possible žVerbs – Get,Set,New,Add,Remove
žNoun – All SharePoint commands Noun start with “SP”
žTo get all SharePoint commands:¡Get-command –Module “Microsoft.SharePoint.PowerShell”
žGet-Command *sp*,-verb,-noun
žExample you forget the name of the command to view log files,žType get-command *sp*log*
To iterate through the commands used TAB KEY

Step 2:get-help

Get-help [commandName]
žUpdate-help –force,žHelp about_*
žHelp [verb]*[searchterm]* –showwindow -detailed –examples –full –online
Note the parameters you need


Step 3:Test Command

žCommand -<ParameterName> <value1>.<value2>
žType any of the examples and test
žTAB through Parameters after – symbol
žTab through Paramater values
žView Members Get-Member
žFilter if needed -Where {$_.Parameter –like “”};Select param1,param2
žVariable assign (Eg)$wa = Get-SPWebApplication
žCurrent Variable $_;Foreach($w in $wa){$_.w.AlternateUrls}
žMeasure to get count of objects.Example, Get-SPWebApplication|Measure
žException Handling-Try{code}{ Write-Exception “Exception $_”}
To output Use Out-GridView;Export-csv;Format-list;Write-Host

The content has been pulled in after listening to creator of PowerShell and SharePoint Powershell gurus from Microsoft Virtual Academy site.

References:
žhttps://technet.microsoft.com/fr-fr/library/jj984298.aspx
žhttps://mva.microsoft.com/




Friday, 1 January 2016

SharePoint Search Keyword Query Language Demo

SharePoint Search KQL Magic

SharePoint Search is very powerful. With a little knowledge of KQL or Keyword Query Syntax, you can get what you want much faster instead of searching from the search results.

Details below in a short video: 






Reference
https://msdn.microsoft.com/en-in/library/ee558911.aspx
http://www.techmikael.com/2014/03/s15e01-kql-basics.html