cancel
Showing results for 
Search instead for 
Did you mean: 

ReindexProperties.AddKeyword adds the keyword but doesnt add the value

Ryan_Wilson2
Star Contributor
Star Contributor

I am adding new keywords via a ReindexProperties.AddKeyword object, this adds the keyword but the value of the keyword is not added.

 

I am doing it via this:

 

ReindexProperties.AddKeyword(keyword); //Where keyword is of type Hyland.Unity.Keyword

1 ACCEPTED ANSWER

Shane_Cook1
Star Contributor
Star Contributor

Below is a tested code example that I have confirmed.  Please feel free to follow this design and let me know how it works for you. In my case, I put this into a system task for testing. It will also work as-is in a workflow action if that is more relevant for you.

public void OnWorkflowScriptExecute(Hyland.Unity.Application app, Hyland.Unity.WorkflowEventArgs args)
{

  try
  { 
      Document theDoc = args.Document;
      DocumentType newDocType = app.Core.DocumentTypes.Find("the new doc type name");
      if (newDocType == null)
      {
          throw new Exception("New Document Type not found.");
      }

    Storage storage = app.Core.Storage;
    ReindexProperties reindexProps = storage.CreateReindexProperties(theDoc, newDocType);
    if (reindexProps == null)
    {
      throw new Exception("ReindexProperties not created.");
    }

    KeywordType newKeywordType = app.Core.KeywordTypes.Find("your new keyword type name");
    if (newKeywordType == null)
    {
      throw new Exception("New Keyword Type not found.");
    }

    Keyword newKeyword = newKeywordType.CreateKeyword("Smith");
    if (newKeyword == null)
    {
      throw new Exception("New Keyword not created.");
    }

    reindexProps.AddKeyword(newKeyword);

    Document newDoc = storage.ReindexDocument(reindexProps);

    app.Diagnostics.Write("Document Successfully ReIndexed, Document ID = {0}.", newDoc.ID);
    args.ScriptResult = true;
    }
    catch (Exception ex)
    {
      app.Diagnostics.Write("ERROR: {0}", ex);
      args.ScriptResult = false;
    }

}

 

Cheers!

View answer in original post

27 REPLIES 27

Correct.

Shane_Cook1
Star Contributor
Star Contributor

When you create the ReindexProperties object, the "yourDocType" in your code should be different than the document's current document type.  I don't believe you can ReIndex a document to the same document type.  Can you add a little more to your scenario/story to help give better advice?

Best Regards!

Shane_Cook1
Star Contributor
Star Contributor

Updated Comment:

When you create the ReindexProperties object, the "yourDocType" in your code should be different than the document's current document type.  You can't ReIndex a document to the same document type.  Can you add a little more to your scenario/story to help us provide better advice?

SDK Reference

Best Regards!

I'm not re-indexing to the same documentType, I pass the current document and the new document type when creating the ReindexProperties, this all works fine, it's when adding a new keyword which does not  exist on the old document type it adds the keyword but doesn't fill in the value when re-indexing and throws the above exception on the call to ReindexDocument().  The document gets moved to the new document type as expected even with the exception message but the new keyword is not populated with it's value. And I've read the entire SDK article you linked to, it doesn't give any kind of coding example so I am doing the best I can with this. I have no idea why it isn't working correctly.

Shane_Cook1
Star Contributor
Star Contributor

Below is a tested code example that I have confirmed.  Please feel free to follow this design and let me know how it works for you. In my case, I put this into a system task for testing. It will also work as-is in a workflow action if that is more relevant for you.

public void OnWorkflowScriptExecute(Hyland.Unity.Application app, Hyland.Unity.WorkflowEventArgs args)
{

  try
  { 
      Document theDoc = args.Document;
      DocumentType newDocType = app.Core.DocumentTypes.Find("the new doc type name");
      if (newDocType == null)
      {
          throw new Exception("New Document Type not found.");
      }

    Storage storage = app.Core.Storage;
    ReindexProperties reindexProps = storage.CreateReindexProperties(theDoc, newDocType);
    if (reindexProps == null)
    {
      throw new Exception("ReindexProperties not created.");
    }

    KeywordType newKeywordType = app.Core.KeywordTypes.Find("your new keyword type name");
    if (newKeywordType == null)
    {
      throw new Exception("New Keyword Type not found.");
    }

    Keyword newKeyword = newKeywordType.CreateKeyword("Smith");
    if (newKeyword == null)
    {
      throw new Exception("New Keyword not created.");
    }

    reindexProps.AddKeyword(newKeyword);

    Document newDoc = storage.ReindexDocument(reindexProps);

    app.Diagnostics.Write("Document Successfully ReIndexed, Document ID = {0}.", newDoc.ID);
    args.ScriptResult = true;
    }
    catch (Exception ex)
    {
      app.Diagnostics.Write("ERROR: {0}", ex);
      args.ScriptResult = false;
    }

}

 

Cheers!