SharePoint

Display New Icon in SPGridView

This took waaaayyyy too long to figure out.  The requirement was to display the New icon on new items being displayed in an SPGridView according to the same rules used to display the icon in, say, a document library.  What took so long was to figure out where that damn property was stored!  Turns out it’s in the SPWebApplication.  Once I found it the rest was easy.  I chose to do it by creating a custom SPBoundField class, but you could just as easily use a ImageField or any other field you’d use in the grid for that matter.

My solution just displays the image, but you can add in the text where you want it as well if that’s needed.  It’s currently commented out.

    [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true),
    AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ShowNewIconSPBoundField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem,
                                                    MemberDescriptor dataFieldPropertyDescriptor)
        {
            PlaceHolder placeHolder = (PlaceHolder)childControl;
            //string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);

            //Get the DaysToShowNewIndicator value for the web app
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(SPContext.Current.Site.RootWeb.Url));
            int daysForNew = webApp.DaysToShowNewIndicator;

            //Get the created date
            string propertyValue = GetPropertyValue(dataItem, “CREATED_datetime”);
            if (propertyValue != “”)
            {
                DateTime createdDate;
                if(DateTime.TryParse(propertyValue, out createdDate))
                {
                    //Figure out how old it is
                    TimeSpan span = DateTime.Now createdDate;

                    //Check if it needs the New icon
                    if(span.Days <= daysForNew)
                    {
                        Image image = new Image();
                        image.ImageUrl = “/_layouts/” + System.Threading.Thread.CurrentThread.CurrentUICulture.LCID.ToString() +
                            “/images/new.gif”;
                        placeHolder.Controls.Add(image);
                    }
                }
            }

            //Paste this in above display to show text as well as image
            //        Label label = new Label();
            //        label.Text = SPHttpUtility.NoEncode(propertyValueAsHtml);
            //        placeHolder.Controls.Add(label);
        }

        protected override Control GetChildControlInstance()
        {
            return new PlaceHolder();
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *