Custom PIM Studio actions

In a previous post we talked about the flexability of the linked items grid. But the linked items grid is not the only item that you can customize to match your specific environment.

All over the PIM Studio you can click on icons. These are called actions and can be configured so they are visible in almost any view in PIM Studio.

In this post we will create a custom action and configure it so that it is accessable in several views.

The custom action we will create is UnclassifyChildProductsAction. This class will load all products from the selected category and will remove the selected category from the classifications of the product.

Creating the action

For an action to be able to execute in the PIM Studio it should implement Adam.Pims.Web.Configuration.IAction. But for your (and our) convenience we have already create a base class that implements all members of that interface. So in most occasions you will prefer to override Adam.Pims.Web.Configuration.Action.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using Adam.Pims.Core;
using Adam.Pims.Web.UI;
using Action = Adam.Pims.Web.Configuration.Action;

namespace Adam.Pims.Blog
{
 public class UnclassifyChildProductsAction : Action
 {
  public override void Execute(ActionContext context)
  {
   Guid selectedCategory;
   if (!GuidConverter.TryParse(context.DataItemId, out selectedCategory))
   {
    string message = string.Format("The DataItemId {0} is not a valid {1}.", context.DataItemId, typeof(Guid).Name);
    throw new InvalidOperationException(message);
   }

   ProductCollection childProducts = LoadChildProducts(selectedCategory, context.App);
   UnclassifyChildProducts(selectedCategory, childProducts);
  }
 }
}

Notice the context parameter in the Execute method. It contains all the information you will need to execute the action. As you can see we use context to retrieve the ID of the selected category. If this string is not a GuidExecute will throw an exception. This should not occur, but when it does you will be better of throwing than ignoring the invalid context.

The LoadChildProducts and UnclassifyChildProducts methods are implemented like this:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private ProductCollection LoadChildProducts(Guid selectedCategory, Application application)
{
 var searchExpression = new SearchExpression(
  "DirectClassification = ?",
  new object[]
  {
   selectedCategory
  });

 var productCollection = new ProductCollection(application);
 productCollection.Load(searchExpression);
 return productCollection;
}

private void UnclassifyChildProducts(Guid selectedCategory, ProductCollection childProducts)
{
 foreach (Product product in childProducts)
 {
  if (product.Classifications.Count == 0)
  {
   continue;
  }

  product.Classifications.Remove(selectedCategory);
  product.Save();
 }
}

Add the action to the category tree view

First we need to create a setting that will configure this action. Just for this demo, we advise you to create a duplicate of the PIMS_DeleteCategory xml setting. This way you already have the basic things required for an action configuration xml. Before saving the duplicate of the setting make sure you change the following things:

  • set the name to PIMS_UnclassifyChildProducts
  • change the labels to something that will be clear to your users. These labels will show up in the UI.
  • change the default value to:
    XML
    1
    2
    
      <action type="Adam.Pims.Blog.UnclassifyChildProductsAction, Adam.Pims.Blog">
      </action>
    

Now let's add this action to the context menu of the category tree view. Find the value of the xml setting PIMS_CategoryTreeViewActions. Just below the last add node, insert the following xml:

XML
1
<add name="PIMS_UnclassifyChildProducts" />

Check out your PIM Studio and right click any node in your tree at the right hand side of the page. You will see you can click the action we created.

Adding the action to the category content view

As mentioned in the beginning of this post, you can configure action in many places. One of them is the category content view.

Configure the actions in the category content view by navigating to the xml setting called PIMS_DefaultCategoryContentViewTemplate. Insert the following xml right below the last add node in the actions node.

XML
1
<add name="PIMS_UnclassifyChildProducts" />

Check out the category content view and notice the a button has been added without icon. Adding an icon to the button is done via css. You should create a css class called PIMS_UnclassifyChildProducts and write the following content: background-image: url("UnclassifyProduct.png");

Is there a bug in the action?

When you navigate to the category content view and you click the action we created, it doesn't seem to work unless we refresh the page. What's going on here? The problem is that the category content view doesn't know that the action was executed. And it doesn't know that products were modified. To inform the view that things were changed, we need to send a message to PIMS that some products were changed. We will do that for every product we change, so in the foreach right below product.Save(); type in the following snippet:

C#
1
MessageBroker.Current.Publish(this, WebConfigurationManager.DefaultChannel, new ProductDeletedEvent(selectedCategory));

Notice that we are sending a ProductDeletedEvent. Obviously this is not correct, but it does the trick for what we are doing. In the next release of PIMS we will have a ProductUpdatedEvent that you can use and that will be more correct.

Conclusion

So now you know how to create your own custom action, you can try out all kinds of actions that might be useful to you.

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed