Automatic Creation of Managed and Crawl Properties


SharePoint 2013/2016 automatically generates managed and crawl properties for site columns. This makes the life of site collection administrator lot easier. In this post I will put a real example of how to test it.

Step 1

Create Site columns and content type and add it to a list. I used the PowerShell below to do that

Add-PSSnapIn “Microsoft.SharePoint.Powershell”

#Get the site collection and web object

$siteColl = Get-SPSite -Identity “https://portal16.contoso.com/”

function Create-SiteColumnText ($siteColl, $CustomColumn)

{

$rootWeb = $siteColl.RootWeb

#Assign fieldXMLString variable with field XML for site column

$fieldXMLString = “<Field Type=’Text’ Name=’$CustomColumn’

Description=’Indicate date of last and next meeting and names of current members.’

DisplayName=’$CustomColumn’

StaticName=’$CustomColumn’

Group=’Custom Columns’

Hidden=’FALSE’

Required=’FALSE’

Sealed=’FALSE’

ShowInDisplayForm=’TRUE’

ShowInEditForm=’TRUE’

ShowInListSettings=’TRUE’

ShowInNewForm=’TRUE’></Field>”

#See field XML on console

#write-host $fieldXMLString

#Create site column from XML string

$rootWeb.Fields.AddFieldAsXml($fieldXMLString)

Write-Host “Column Added”

}

function Create-SiteColumnChoice ($siteColl, $CustomColumn)

{

$rootWeb = $siteColl.RootWeb

#Assign fieldXMLString variable with field XML for site column

$fieldXMLString = “<Field Type=’Choice’

Name=’$CustomColumn’

Description=’Choice.’

Format=’Dropdown’

DisplayName=’$CustomColumn’

StaticName=’$CustomColumn’

Group=’Custom Columns’

Hidden=’FALSE’

Required=’FALSE’

Sealed=’FALSE’

ShowInDisplayForm=’TRUE’

ShowInEditForm=’TRUE’

ShowInListSettings=’TRUE’

ShowInNewForm=’TRUE’>

<Default>Specification</Default>

<CHOICES>

<CHOICE>Specification</CHOICE>

<CHOICE>Development</CHOICE>

<CHOICE>Test</CHOICE>

<CHOICE>Documentation</CHOICE>

</CHOICES>

</Field>”

#See field XML on console

#write-host $fieldXMLString

#Create site column from XML string

$rootWeb.Fields.AddFieldAsXml($fieldXMLString)

Write-Host “Column Added”

}

Create-SiteColumnText -siteColl $siteColl -CustomColumn “MyCustomColumn1”

Create-SiteColumnText -siteColl $siteColl -CustomColumn “MyCustomColumn2”

Create-SiteColumnText -siteColl $siteColl -CustomColumn “MyCustomColumn3”

Create-SiteColumnChoice -siteColl $siteColl -CustomColumn “MyCustomColumn4”

$web = $siteColl.RootWeb

$ctypeName = “MyCustomContentType”

$ctypeParent = $web.availablecontenttypes[“Item”]

$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)

$web.contenttypes.add($ctype)

$field = $web.fields.getfield(“MyCustomColumn1”)

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$field = $web.fields.getfield(“MyCustomColumn2”)

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$field = $web.fields.getfield(“MyCustomColumn3”)

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$field = $web.fields.getfield(“MyCustomColumn4”)

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)

$ctype.fieldlinks.add($fieldLink)

$ctype.Update()

$ListTitle = “CustomList1”

$Web.Lists.Add($ListTitle, $ListTitle, 100)

$Library =$Web.Lists[$ListTitle]

$Library.ContentTypesEnabled = $true

$Library.ContentTypes.Add($ctype);

$Library.ContentTypes.Delete($Library.ContentTypes[0].Id)

$Library.ContentTypes.Count

$Library.Update()

$Array = @(“Specification”, “Development”, “Test”, “Documentation”)

for ($i = 1; $i -lt 5; $i++)

{

$Item = $Library.AddItem()

$Item[“Title”] = “Item $i”

$Item[“MyCustomColumn1”] = “Item $i Column $i”

$Item[“MyCustomColumn2”] = “Item $i Column $i”

$Item[“MyCustomColumn3”] = “Item $i Column $i”

$j = $i

$Item[“MyCustomColumn4”] = $Array[$j-1]

$Item.Update()

Write-Host “Done…” -ForegroundColor Green

}

clip_image002

Next Step would be to run a full crawl.

Open Central Administration – Application management – Manage service application – Search Service Application – Content Sources – Choose Full crawl from All Sites Content Source. You can run increment crawl in case your farm is large but my farm only has few items.

clip_image004

Click on Crawl Log – URL View – Type the URL of the custom list and click Search

clip_image006

You see that your items are indexed. Now wait for crawl status to idle.

clip_image008

Now go back to portal site – Open Site Settings – Under Site Collection Administrator click Search Schema

clip_image010

Type MyCustom in filter textbox and Click green arrow.

clip_image012

You would see that these columns are greyed out with clear message when you edit them.

clip_image014

“Automatically created managed property.MyCustomColumnX2OWSTEXT is an automatically created managed property. You can’t see any of its existing settings or which crawled properties are mapped to it, but you can map more crawled properties to it. If you save the managed property without making any changes, all settings will be deleted.”

Once you make a change in the property, the link become’s active.

clip_image016

These properties can now be used in Content Search Web part, reminders and display templates

You can make Search Rest to read the values as well

https://portal16.contoso.com/_api/search/query?querytext=’Item 1 Column 1’&selectproperties=’Path,Url,Title,Author,MyCustomColumn1OWSTEXT’

image

CSOM, REST and SharePoint 2013 Search


SharePoint 2013 introduce couple of new ways to access Search Feature in custom applications.  SharePoint 2010 provided the Served Side Search Object Model and Search.asmx web service which allowed us to use Search in Custom apps but there was no access from client.  Now you can call Search from either CSOM or REST.  Even at this moment there is not enough information available out there to completely understand and demo the feature.  But I was able to make it work.

CSOM is more traditional approach if you are developing application for .NET, Silverlight.  Simply Add Reference to

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

These assemblies are necessary as the base implementation of CSOM is part of these assemblies.

Additionally you have to add reference to SharePoint 2013 Search Client OM DLL. which is

Microsoft.SharePoint.Client.Search.dll

Now you can write the C# code to Query and Parse the Result.

using (ClientContext context = new ClientContext(new Uri("http://intranet.contoso.com/sites/Search/")))

{

        context.Credentials = System.Net.CredentialCache.DefaultCredentials;

        KeywordQuery keywordQuery = new KeywordQuery(context);

        if (textBox1.Text.Length == 0)

        {

            MessageBox.Show("Please enter Title in Text Box to Save.");

            return;

        }

        keywordQuery.QueryText = textBox1.Text;

 

        SearchExecutor searchExec = new SearchExecutor(context);

        ClientResult<ResultTableCollection> Results = searchExec.ExecuteQuery(keywordQuery);

        context.ExecuteQuery();

 

        if (Results != null)

        {

            if (Results.Value[0].RowCount > 0)

            {

                DataTable dt = new DataTable();

 

                dt.Columns.Add("Title");

                dt.Columns.Add("Path");

                dt.Columns.Add("Write");

                dt.Columns.Add("Author");

                dt.Columns.Add("Rank");

 

                foreach (var Row in Results.Value[0].ResultRows)

                {

                    DataRow dr = dt.Rows.Add();

                    if (Row["Title"] != null)

                    {

                        dr[0] = Row["Title"].ToString();

                    }

                    else

                        dr[0] = Row["AccountName"].ToString();

 

                    dr[1] = Row["Path"].ToString();

                    dr[2] = Row["Write"].ToString();

                    dr[3] = Row["Author"].ToString();

                    dr[4] = Row["Rank"].ToString();

                }

 

 

                dataGridView1.DataSource = dt.DefaultView;

            }

            else

            {

                MessageBox.Show("No Results Found for your Query " + textBox1.Text);

            }

        }

        else

        {

            MessageBox.Show("No Results Found for your Query " + textBox1.Text);

        }

        

 

 

}

As almost every CSOM method has its REST Endpoint now SharePoint 2013 now also provide an effective way to using Search using REST and oData.  First you have to make a httpRequest object using oData and make a GET request which will be passed to AJAX. 

For this sample I created a SharePoint Hosted App.  I added a Text Box and Button to the page and a Result Div that will show the output to the user  (Very Normal way in HTML apps)

“Another thing to know that this SharePoint app will not work until you give it permission to use Search.”  You have to double click on application manifest file and inside permission choose Search.

While Executing the app the app will ask for user permission to allow it to run Search. 

First you have to understand the URL convention of the Search 2013 REST.  To make Search using REST you have to using URL in the format of

SiteURL/host/_api/Search/Query?QueryText=’Text Keyword’

You can also perform a Query to get Query Suggestions using Sugges Instead of Query

SiteURL/_api/search/suggest?querytext=’keyword needed suggestion’

You can read all about Search and performing Search from here

The Result will return in JSON format which you have to parse.  The Results are included in d:RelevantResults section of the output XML.  This section include all the properties you need to display results to the end users.  the results include almost everything that you need from keywords, Tags, Image Thumbnails etc.

I will post about making Query using Fiddler and then from SharePoint App tomorrow.

SharePoint 2013–The start address sps3://intranet.contoso.com cannot be crawled


 

I guess you might have seen this issue before so please try to remember the fix as well.  While configuring Search I find out that SharePoint 2013 Search gave me the following error on User Profile Crawling.

“Access is denied.  Verify that either the Default Content Access Account has permission to access this repository, or add a crawl rule to crawl this repository…”

To Fix it we have to add our Content Access Account to be able to Crawl the content.

To do so we will go to Manage Service Application.

image

Now Select the User Profile Service Application row and click Administrator.

image

Now Enter your content Access Account in the first text box and click Add.

image

Click OK.

Now run the Full Crawl again.

image

Now compare the Searchable Items

image

Now Just take a look at Amazing Search Results Page.

image

Here is Conversation Search

image

People Results

image

Video test the video results later.

Search Settings at Site Collection Level

image

For More information and idea about the Vision of Microsoft SharePoint Search please visit the Microsoft SharePoint Team Blog for Series of Articles on SharePoint Search.

http://sharepoint.microsoft.com/blog/Pages/BlogPost.aspx?pID=1030