|
using System; |
|
using System.Collections.Generic; |
|
using System.Globalization; |
|
using Telerik.Sitefinity.Publishing; |
|
using Telerik.Sitefinity.Services; |
|
using Telerik.Sitefinity.Services.Search; |
|
using Telerik.Sitefinity.Services.Search.Data; |
|
using Telerik.Sitefinity.Services.Search.Model; |
|
using Telerik.Sitefinity.Services.Search.Publishing; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class ExternalIndexer |
|
{ |
|
/// <summary>Creates a new search document and adds it to the search index.</summary> |
|
public void AddSearchDocument() |
|
{ |
|
var fields = new List<IField>(); |
|
|
|
// The identity field is mandatory. |
|
// The value of the identity field must be unique for each search document. |
|
// For example, Sitefinity CMS uses a combination of GUID and Language ID. |
|
var identityField = new Field |
|
{ |
|
Name = Document.IdentityFieldName, |
|
Value = Guid.NewGuid().ToString() // Use lowercase only |
|
}; |
|
fields.Add(identityField); |
|
|
|
// The title field is mandatory. It is used by Sitefinity CMS widgets |
|
var titleField = new Field |
|
{ |
|
Name = PublishingConstants.FieldTitle, |
|
Value = "Here goes the title" |
|
}; |
|
fields.Add(titleField); |
|
|
|
// The content field is mandatory. It is used by Sitefinity CMS widgets |
|
// You need to fetch the external content and add it as content here. |
|
string externalContent = ""; // supply the actual content here |
|
var externalContentField = new Field |
|
{ |
|
Name = PublishingConstants.FieldContent, |
|
Value = externalContent |
|
}; |
|
fields.Add(externalContentField); |
|
|
|
// The last modified field is mandatory. |
|
var lastModifiedField = new Field |
|
{ |
|
Name = PublishingConstants.FieldLastModified, |
|
Value = DateTime.UtcNow |
|
}; |
|
fields.Add(lastModifiedField); |
|
|
|
// We strongly recommend setting the language field. |
|
var languageField = new Field |
|
{ |
|
Name = PublishingConstants.LanguageField, |
|
Value = CultureInfo.CurrentCulture.Name // this is just a sample, you need to set up the correct language for the content |
|
}; |
|
fields.Add(languageField); |
|
|
|
// We recommend setting the link field. It enables site visitors to click on the link and go to the external content |
|
var linkField = new Field |
|
{ |
|
Name = PublishingConstants.FieldLink, |
|
Value = "https://url.to.external.content" |
|
}; |
|
fields.Add(linkField); |
|
|
|
// Create the search document containing the fields that we constructed |
|
var searchDocument = new Document(fields, identityField:identityField.Name); |
|
|
|
// Add the the created document to an existing search index |
|
// The name "index" is illustrative in this sample, you should use the actual search index name here. |
|
var searchDocumentsList = new List<IDocument>() { searchDocument }; |
|
ServiceBus.ResolveService<ISearchService>().UpdateIndex(name:"index", searchDocumentsList); |
|
} |
|
} |
|
} |