Custom Sitecore fields and master templates

Published by on
Tags:

For a client I had to write a custom Sitecore field. The field was to contain a number that incremented every time a new item using the field was created. I created the field and so that they could seed the number created a Sitecore item that contained the current highest field number.

This worked great for a few days but then during testing for some reason the field stopped incrementing and always used the same number. After a bit of investigation I realised that this was caused by a value being set on master template. My code worked by checking to see if the field had a value and if not setting a value using the seed:

 	    if (!Sitecore.Context.ClientPage.IsEvent)
            {
                this.isEvent = false;

                if (string.IsNullOrEmpty(this.Value))
                {                    
                    this.Value = GetNewValue();
                }                            
                
                this.Controls.Add(_SeedValue);

                _SeedValue.ID = GetID("SeedVal");
                _SeedValue.Enabled = false;
                _SeedValue.Disabled = true;
            }
            else
            {
                _SeedValue = FindControl(GetID("SeedVal")) as Edit;               
          
                this.Value = _SeedValue.Value;                
            }

What my code did not do is check that the current item being editted was not a master. To do this I had to add the following:

private bool IsMaster(Item item)
{	
	if (item == null) return false;
	else if (item.ID == new Sitecore.Data.ID(new Guid("{BAD98E0E-C1B5-4598-AC13-21B06218B30C}"))) return true;
	else if (item.Parent != null) return IsMaster(item.Parent);
	else return false;
}

This method checks to see if the current item being edited is a descendant of the Master item. The guid in the method is the guid of the Master item. Unfortunately you can not check the template of master of the item being edited because a master is based on the template that it is the master for and is not made from a master itself.

The main code then changes as follows so that only if the field is blank and not a master should a value be generated:

            if (!Sitecore.Context.ClientPage.IsEvent)
            {
                this.isEvent = false;
                
                Item edittingItem = Sitecore.Configuration.Factory.GetDatabase("master").GetItem(this.ItemID);

                if (string.IsNullOrEmpty(this.Value) && IsMaster(edittingItem) == false )
                {                    
                    this.Value = GetNewValue();
                }                            
                
                this.Controls.Add(_SeedValue);

                _SeedValue.ID = GetID("SeedVal");
                _SeedValue.Enabled = false;
                _SeedValue.Disabled = true;
            }
            else
            {
                _SeedValue = FindControl(GetID("SeedVal")) as Edit;                         
                this.Value = _SeedValue.Value;                
            }

Also be sure to use

Sitecore.Configuration.Factory.GetDatabase("master").GetItem(this.ItemID);
If you try to use
Sitecore.Context.Database.GetItem(this.ItemID);
You will have problems because the current context is set to the Core database.

Sitecore Sheer Tools

Published by on
Tags:

The other day I had to replace the value in a field, that was the easy part the hard part was that I had to do this 500 times. So I created a simple Sitecore XAML application to do this. It's very simple and quickly coded to solve the problem. Its pretty hacked but in the future I will look at improving it. I have ideas of other tools I might add to the application to make it more useful. You can download it here, also it saves me having to put in on a memory stick to take it to work.

ExperimentsInCode.Sitecore.Sheer.zip (8.74 kb)

This is a Sitecore package, so you should be able to install it using the Sitecore Install Wizard. I made this using Sitecore Xpress which is Sitecore 5.3.1, it won't work on earlier versions of Sitecore, it also needs .Net Framework 3.5

 

Sitecore Sheer XAML Applications - Part 2 - Input Popup Box

Published by on
Tags:

 

Today I wanted a popup to appear in my XAML application and ask the user for some info before disappearing. This seemed like a simple requirement but it took some hunting around to find the code to do this so here is the solution.

In this example the prompt pops up when the user clicks a button within the XAML application. This is handled by the ButtonClick method. Once clicked this calls a Pipeline method, notice how I have to define the method I want to call as a string, I wish Sitecore would use delegates.

Within the pipeline method you need to check the args.IsPostBack to see if the prompt has been opened yet.

 

   17         [HandleMessage("local:MyButtonClick")]

   18         public void ButtonClick(Message message)

   19         {

   20             Sitecore.Context.ClientPage.Start(this, "PopupTagPipeline");

   21         }

   22 

   23         public void PopupTagPipeline(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)

   24         {

   25             if (args.IsPostBack)

   26             {

   27                 // this event occurs after the user has entered some data

   28                 string result = args.Result;

   29 

   30                 //do some more work

   31             }

   32             else

   33             {

   34                 //prompt the user to enter a value and then wait for their response.

   35                 global::Sitecore.Context.ClientPage.ClientResponse.Input("Please enter a value", "");               

   36                 args.WaitForPostBack();

   37             }

   38         } 

 

The prompt takes two arguments, the first is the label to display to the user and the second is the default value.

Once this is complete you should end up with a nice prompt.

 

Sitecore Sheer XAML Applications - Part 1

Published by on
Tags:
This week I have been working on a new Sheer XAML application for Sitecore. The ability to add applications to the Sitecore client is a powerful tool,  however I noticed that there isn’t a huge amount of information on how to create these applications and this probably puts off a lot of developers. I hope to document some useful bit’s and pieces over the next few weeks but I thought I would start with a simple extension function.

If you are dynamically changing the UI of the Sheer application through code you need to send the new HTML to the browser. All the Sitecore controls descend form Sitecore.Web.UI.WebControl. This allows us to create a simple extension function that can be used by any of the Sitecore controls to send back their new HTML output.

   24     public static void RefreshHtml(this Sitecore.Web.UI.WebControl control)

   25     {

   26         Sitecore.Context.ClientPage.ClientResponse.SetOuterHtml(control.ID, control);

   27     }


Then to simply update any control in your XAML application you simply call the refresh method:

   15     Sitecore.Web.UI.HtmlControls.Listview view = new Sitecore.Web.UI.HtmlControls.Listview();

   16     view.RefreshHtml();

 

All blogs tagged with 'xaml'

Projects

Have you read?

Scala Operator Magic



Blogs by date