Content Enrichment Web Service in SharePoint 2013

In this article we can explore an Advanced Search feature of SharePoint 2013.

Content Enrichment Web Service

SharePoint 2013 allows developers to add a custom step between content processing. A SharePoint content source crawl will call our custom web service. We can create or change the metadata properties associated with the crawl item.

Under the hood, the web service is actually implementing a pre-defined interface. Each crawl item will be passed to the interface method where the item URL and properties can be accessed.

Scenarios

We can use a Content Enrichment Web Service for the following scenarios:

  1. You have multiple web content sources configured. You need to create new Managed Properties based on each URL. Example: Blog, Website and so on.
  2. You have a content source with limited metadata information. You need to add a new Managed Properties based on the content.
  3. You have a content source with metadata information. You need to correct the Managed Properties.
  4. You have a SharePoint list where an Age column exists. You need to classify the content as Minor and Major using Managed Properties.

Infrastructure

The following is the Infrastructure needed:

  1. Visual Studio 2012
  2. IIS website to host the service
  3. WCF service

Practical

To perform the Practical, we need to set up the following.

  1. SharePoint 2013 Enterprise site
  2. Create Enterprise Search Center site
  3. Central Administration > Search > Create Content Source > Add jeanpaulva
  4. Central Administration > Search > Create Content Source > Add sharepointcto
  5. Central Administration > Create Crawl Rules > Include query strings (?) for both URLs
  6. Server > IE Enhanced Security Configuration > Off
  7. Perform Full Crawl for both content sources

After the preceding steps, try searching for sharepointcto from the Enterprise Search Center site and you should be able to see results from both the URLs as shown below.

sharepointcto

Our purpose is to provide refiners as in the following.
 
refiners

Steps

Step 1: Create Service

Create a new WCF service application project.

new WCF service application

Delete the existing Service files and create a new Service.
 
create a new Service

You can delete the interface file as well. The Solution Explorer looks as in the following.
 
Solution Explorer

Add Reference to the following assembly:

15HIVE\Search\Applications\External\Microsoft.Office.Server.Search.ContentProcessingEnrichment.dll

Step 2: Implement Interface

Open the SVC file and implement a pre-defined interface IContentProcessingEnrichmentService as shown below.

Implement Interface

The only method named ProcessItem passes an Item argument and returns a ProcessedItem argument.

The interface IContentProcessingEnrichmentService belongs to the ContentProcessingEnrichment assembly.

The Item type has a property named ItemProperties that contains pre-defined Name-Value pairs of:

ContentType Access the Content Type of the item
Name Access the Name of the item
Title Access the Title of the item
Path Access the Path of the item
ContentSource Access the ContentSource name

We can specify the input and output properties when registering the service. In our case we are using the Path property.

Step 3: Create Method

Replace the method with the following code. Right-click and Resolve type name errors.

public ProcessedItem ProcessItem(Item item)

{

    ProcessedItem processedItem = new ProcessedItem();

    processedItem.ItemProperties = new List<AbstractProperty>();

    var p = item.ItemProperties.Where(n => n.Name == "Path").First();

    string url = p.ObjectValue.ToString();

    if (!string.IsNullOrEmpty(url))

    if (url.StartsWith("http://www.sharepointcto.com"))

    {

        Property<string> property = new Property<string>()

        {

            Name = "WebType",

            Value = "Site"

        };

        processedItem.ItemProperties.Add(property);

    }

    else if (url.StartsWith("http://www.jeanpaulva.com"))

    {

        Property<string> property = new Property<string>()

        {

            Name = "WebType",

            Value = "Blog"

        };

        processedItem.ItemProperties.Add(property);

    }

    return processedItem;

} 

The preceding code performs the following:

  1. Get the Path property value
  2. If the Path value is of www.sharepointcto.com then create a Managed Property WebType and set the value to Site
  3. If a Path value is of www.jeanpaulva.com then create a Managed Property WebType and set the value to Blog
  4. The processedItem object will be returned by the method

Step 4: Run the Service

Save and Run the Service.

Run the Service

Step 5: Create Managed Property

Open Central Administration > Service Applications > Search Service Application > Search Schema > New Managed Property page. Enter the following details.

Managed Property page 
  • Name as WebType
  • Type as Text
  • Searchable enable
  • Queryable enable
  • Refinable yes-active

Step 6: Register Service using PowerShell

Open the PowerShell ISE editor and copy and paste the following code.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

# Get SearchServiceApplication

$ssa = Get-SPEnterpriseSearchServiceApplication
Remove-SPEnterpriseSearchContentEnrichmentConfiguration
–SearchApplication $ssa
$c
= New-SPEnterpriseSearchContentEnrichmentConfiguration
$c
.Endpoint = "http://localhost:52156/WebsiteEnrichmentService.svc"
$c
.DebugMode = $false
$c
.SendRawData = $false
$c
.InputProperties = "Path"
$c
.OutputProperties = "WebType"
Set-SPEnterpriseSearchContentEnrichmentConfiguration
–SearchApplication $ssa –ContentEnrichmentConfiguration $c

Please remember to use the correct URL for the service.

Save and Run. If no errors, your service is installed successfully.

Step 7: Perform Full Crawl

Open Central Administration and Perform a Full Crawl for the following content sources.

  1. Web content source for sharepointcto
  2. Web content source for jeanpaulva

Step 8: Add Refiners to the Result Page

Open the Enterprise Search Center result page, bring the page to edit mode, edit the refiner web part and add the WebType refiner. You can view the References section on how to add Search Refiners.

Step 9: Test Search

Open the Enterprise Search Center Site and try searching for content. You can see the Web Type refiner appearing and try clicking on the values.

Test Search

If the result is filtered based on Blog or Site values then it means the Refiners are working correctly.
 
Note

Since the Content Enrichment Web Service will be called for all the Content Sources, performance can become sluggish since there is a delay involved in the service invocation. It is recommended that we deploy the service closer to the SharePoint system. Alternatively, one can try Asynchronous mode too.

We can also debug the service. Ensure the Service is running and set breakpoints.

breakpoints 

References

Summary

In this article we have explored Content Enrichment Web Service. The source code and script is attached for download.

Up Next
    Ebook Download
    View all
    Learn
    View all