Sitecore Lucene.net how to index Users and Profiles from Core Database Part 1

Here We cheat and ask the crawler to index Home Item in the Core-Database

          <index id="users_index" type="Sitecore.Search.Index, Sitecore.Kernel">
            <param desc="name">$(id)</param>
            <param desc="folder">$(id)</param>
            <Analyzer ref="search/analyzer" />
            <locations hint="list:AddCrawler">
              <profiles type="Path.To.Your.NameSpace.UserCrawler, YourDLL">
                <Database>core</Database>
                <Root>/sitecore/content/Home</Root>
                <IndexAllFields>false</IndexAllFields>
                <include hint="list:IncludeTemplate">
                  <home>{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}</home>
                </include>
                <Tags>home</Tags>
                <Boost>2.0</Boost>
              </profiles>
            </locations>
          </index>

We ask the DatabaseCrawler to add the Item in this case Home Item from Core-Datase but then we start running our code to grab the Users.


    /// <summary>
    /// User Crawler
    /// </summary>
    public class UserCrawler: Sitecore.Search.Crawlers.DatabaseCrawler
    {
        protected override void AddItem(Item item, IndexUpdateContext context)
        {
            Domain extranet = Factory.GetDomain("extranet");

            List<User> extranetUser = extranet.GetUsers().ToList();

            if (extranetUser.Any())
            {
                using (Indexer indexer = new Indexer(Indexer.IndexerName("core")))
                {
                    BaseCrawler baseCrawler = new BaseCrawler();

                    foreach (User user in extranetUser)
                    {
                        using (IndexUpdateContext updateContext = indexer.Index.CreateUpdateContext())
                        {
                            baseCrawler.AddUser(user, updateContext);
                        }
                    }

                    using (IndexUpdateContext updateContext = indexer.Index.CreateUpdateContext())
                    {
                        updateContext.Optimize();
                    }
                }
            }
        }
    }

Final code and here you can add the other properties that you want to index


 /// <summary>
    /// Simple Base Crawler
    /// </summary>
    public class BaseCrawler
    {
        /// <summary>
        ///     Helper method to create fields.
        /// </summary>
        /// 
        ///     The name.
        /// 
        /// 
        ///     The value.
        /// 
        /// 
        ///     if set to true this instance is tokenize.
        /// 
        /// 
        ///     The boost.
        /// 
        /// 
        ///     The field.
        /// 
        private Field CreateField(string name, string value, bool tokenize, float boost)
        {
            Field field = new Field(name, tokenize ? value : value.ToLowerInvariant(), Field.Store.YES, tokenize ? Field.Index.TOKENIZED : Field.Index.UN_TOKENIZED);
            field.SetBoost(boost);
            return field;
        }

        /// <summary>
        ///     Creates the value field. Value fields are not stored, only indexed. Also, field value is interpreted as a whole, not as series of tokens.
        /// </summary>
        /// 
        ///     The name.
        /// 
        /// 
        ///     The value.
        /// 
        /// 
        ///     The value field.
        /// 
        private Field CreateValueField(string name, string value)
        {
            return CreateValueField(name, value, 1f);
        }

        /// <summary>
        ///     Creates the value field. Value fields are not stored, only indexed. Also, field value is interpreted as a whole, not as series of tokens.
        /// </summary>
        /// 
        ///     The name.
        /// 
        /// 
        ///     The value.
        /// 
        /// 
        ///     The boost.
        /// 
        /// 
        ///     The value field.
        /// 
        private Field CreateValueField(string name, string value, float boost)
        {
            return CreateField(name, value, false, boost);
        }

        /// <summary>
        /// Adds the user.
        /// </summary>
        /// The user.
        /// The context.
        public void AddUser(User user, IndexUpdateContext context)
        {
            Assert.ArgumentNotNull(user, "user");
            Assert.ArgumentNotNull(context, "context");
            MembershipUser membershipUser = Membership.GetUser(user.Name);

            Document document = new Document();

            if (user.Name != null)
                document.Add(CreateValueField("username", user.Name));

            if (user.LocalName != null)
                document.Add(CreateValueField("localname", user.LocalName));

            if (user.Profile.Email != null)
                document.Add(CreateValueField("email", user.Profile.Email));

            document.Add(CreateValueField("LastActivityDate", DateTools.DateToString(user.Profile.LastActivityDate, DateTools.Resolution.MINUTE)));
            document.Add(CreateValueField("LastUpdatedDate", DateTools.DateToString(user.Profile.LastUpdatedDate, DateTools.Resolution.MINUTE)));
            
            if (membershipUser != null)
            {
                document.Add(CreateValueField("LastLoginDate", DateTools.DateToString(membershipUser.LastLoginDate, DateTools.Resolution.MINUTE)));
                document.Add(CreateValueField("CreationDate", DateTools.DateToString(membershipUser.CreationDate, DateTools.Resolution.MINUTE)));
            }

            context.AddDocument(document);
            context.Commit();
        }
    }
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s